mirror of
https://github.com/elyby/mojang-api.git
synced 2025-05-31 14:11:44 +05:30
Implemented API State endpoint
This commit is contained in:
20
src/Api.php
20
src/Api.php
@@ -24,6 +24,26 @@ class Api {
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Ely\Mojang\Response\ApiStatus[]
|
||||
*
|
||||
* @throws GuzzleException
|
||||
*
|
||||
* @url https://wiki.vg/Mojang_API#API_Status
|
||||
*/
|
||||
public function apiStatus(): array {
|
||||
$response = $this->getClient()->request('GET', 'https://status.mojang.com/check');
|
||||
$body = $this->decode($response->getBody()->getContents());
|
||||
|
||||
$result = [];
|
||||
foreach ($body as $serviceDeclaration) {
|
||||
$serviceName = array_keys($serviceDeclaration)[0];
|
||||
$result[$serviceName] = new Response\ApiStatus($serviceName, $serviceDeclaration[$serviceName]);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $username
|
||||
* @param int $atTime
|
||||
|
||||
62
src/Response/ApiStatus.php
Normal file
62
src/Response/ApiStatus.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ely\Mojang\Response;
|
||||
|
||||
class ApiStatus {
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $serviceName;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $status;
|
||||
|
||||
public function __construct(string $serviceName, string $status) {
|
||||
$this->serviceName = $serviceName;
|
||||
$this->status = $status;
|
||||
}
|
||||
|
||||
public function getServiceName(): string {
|
||||
return $this->serviceName;
|
||||
}
|
||||
|
||||
public function getStatus(): string {
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that current service has no issues.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isGreen(): bool {
|
||||
return $this->assertStatusIs('green');
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that current service has some issues.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isYellow(): bool {
|
||||
return $this->assertStatusIs('yellow');
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that current service is unavailable.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isRed(): bool {
|
||||
return $this->assertStatusIs('red');
|
||||
}
|
||||
|
||||
private function assertStatusIs(string $expectedStatus): bool {
|
||||
return $this->getStatus() === $expectedStatus;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user