mirror of
https://github.com/elyby/mojang-api.git
synced 2024-11-23 05:33:10 +05:30
Implemented API State endpoint
This commit is contained in:
parent
6cb975d2d3
commit
ddf3a07d1f
@ -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.
|
||||
|
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;
|
||||
}
|
||||
|
||||
}
|
@ -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',
|
||||
|
38
tests/Response/ApiStatusTest.php
Normal file
38
tests/Response/ApiStatusTest.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ely\Mojang\Test\Response;
|
||||
|
||||
use Ely\Mojang\Response\ApiStatus;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ApiStatusTest extends TestCase {
|
||||
|
||||
public function testGetters() {
|
||||
$response = new ApiStatus('minecraft.net', 'green');
|
||||
$this->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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user