From ddf3a07d1f833f2760762d1394ab16413a452d8f Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Sun, 7 Apr 2019 02:14:52 +0200 Subject: [PATCH] Implemented API State endpoint --- CHANGELOG.md | 1 + src/Api.php | 20 +++++++++++ src/Response/ApiStatus.php | 62 ++++++++++++++++++++++++++++++++ tests/ApiTest.php | 35 ++++++++++++++++++ tests/Response/ApiStatusTest.php | 38 ++++++++++++++++++++ 5 files changed, 156 insertions(+) create mode 100644 src/Response/ApiStatus.php create mode 100644 tests/Response/ApiStatusTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ad024e..162bcbd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - This CHANGELOG.md file. - `\Ely\Mojang\Api::setClient()` method to override default HTTP client. +- [API Status](https://wiki.vg/Mojang_API#API_Status) endpoint. ### Changed - The constructor no longer has arguments. diff --git a/src/Api.php b/src/Api.php index 085d996..c35b49b 100644 --- a/src/Api.php +++ b/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 diff --git a/src/Response/ApiStatus.php b/src/Response/ApiStatus.php new file mode 100644 index 0000000..be166bc --- /dev/null +++ b/src/Response/ApiStatus.php @@ -0,0 +1,62 @@ +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; + } + +} diff --git a/tests/ApiTest.php b/tests/ApiTest.php index d5977ba..afd2baf 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -7,6 +7,7 @@ use Ely\Mojang\Api; use Ely\Mojang\Exception\NoContentException; use Ely\Mojang\Middleware\ResponseConverterMiddleware; use Ely\Mojang\Middleware\RetryMiddleware; +use Ely\Mojang\Response\ApiStatus; use Ely\Mojang\Response\Properties\TexturesProperty; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; @@ -47,6 +48,40 @@ class ApiTest extends TestCase { $this->api->setClient($client); } + public function testApiStatus() { + $this->mockHandler->append($this->createResponse(200, [ + [ + 'minecraft.net' => 'yellow', + ], + [ + 'session.minecraft.net' => 'green', + ], + [ + 'textures.minecraft.net' => 'red', + ], + ])); + + $result = $this->api->apiStatus(); + + /** @var \Psr\Http\Message\RequestInterface $request */ + $request = $this->history[0]['request']; + $this->assertSame('https://status.mojang.com/check', (string)$request->getUri()); + + $this->assertCount(3, $result); + $this->assertContainsOnlyInstancesOf(ApiStatus::class, $result); + $this->assertArrayHasKey('minecraft.net', $result); + $this->assertSame('minecraft.net', $result['minecraft.net']->getServiceName()); + $this->assertSame('yellow', $result['minecraft.net']->getStatus()); + + $this->assertArrayHasKey('session.minecraft.net', $result); + $this->assertSame('session.minecraft.net', $result['session.minecraft.net']->getServiceName()); + $this->assertSame('green', $result['session.minecraft.net']->getStatus()); + + $this->assertArrayHasKey('textures.minecraft.net', $result); + $this->assertSame('textures.minecraft.net', $result['textures.minecraft.net']->getServiceName()); + $this->assertSame('red', $result['textures.minecraft.net']->getStatus()); + } + public function testUsernameToUuid() { $this->mockHandler->append($this->createResponse(200, [ 'id' => '86f6e3695b764412a29820cac1d4d0d6', diff --git a/tests/Response/ApiStatusTest.php b/tests/Response/ApiStatusTest.php new file mode 100644 index 0000000..d307efd --- /dev/null +++ b/tests/Response/ApiStatusTest.php @@ -0,0 +1,38 @@ +assertSame('minecraft.net', $response->getServiceName()); + $this->assertSame('green', $response->getStatus()); + } + + public function testIsGreen() { + $response = new ApiStatus('minecraft.net', 'green'); + $this->assertTrue($response->isGreen()); + $this->assertFalse($response->isYellow()); + $this->assertFalse($response->isRed()); + } + + public function testIsYellow() { + $response = new ApiStatus('minecraft.net', 'yellow'); + $this->assertFalse($response->isGreen()); + $this->assertTrue($response->isYellow()); + $this->assertFalse($response->isRed()); + } + + public function testIsRed() { + $response = new ApiStatus('minecraft.net', 'red'); + $this->assertFalse($response->isGreen()); + $this->assertFalse($response->isYellow()); + $this->assertTrue($response->isRed()); + } + +}