Remove static create method and clear the constructor. Add CHANGELOG.md

This commit is contained in:
ErickSkrauch 2019-04-07 01:29:16 +02:00
parent e55c5f3b14
commit c5f432b659
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
4 changed files with 57 additions and 27 deletions

26
CHANGELOG.md Normal file
View File

@ -0,0 +1,26 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
## [0.2.0] - no date
### Added
- This CHANGELOG.md file.
- `\Ely\Mojang\Api::setClient()` method to override default HTTP client.
### Changed
- The constructor no longer has arguments.
### Removed
- `\Ely\Mojang\Api::create()` static method. Use constructor instead.
## [0.1.0] - 2019-04-01
### Added
- Initial implementation
[Unreleased]: https://github.com/elyby/mojang-api/compare/v0.2.0...HEAD
[0.2.0]: https://github.com/elyby/mojang-api/compare/v0.1.0...v0.2.0
[0.1.0]: https://github.com/elyby/mojang-api/releases/tag/0.1.0

View File

@ -23,11 +23,11 @@ composer require ely/mojang-api
## Usage ## Usage
To get the configured `Api` object right away, just use the static `create()` method: To start using this library just create a new `Api` class instance and call the necessary endpoint:
```php ```php
<?php <?php
$api = \Ely\Mojang\Api::create(); $api = new \Ely\Mojang\Api();
$response = $api->usernameToUUID('erickskrauch'); $response = $api->usernameToUUID('erickskrauch');
echo $response->getId(); echo $response->getId();
``` ```

View File

@ -20,29 +20,10 @@ class Api {
*/ */
private $client; private $client;
public function __construct(ClientInterface $client) { public function setClient(ClientInterface $client): void {
$this->client = $client; $this->client = $client;
} }
/**
* @param callable $handler HTTP handler function to use with the stack. If no
* handler is provided, the best handler for your
* system will be utilized.
*
* @return static
*/
public static function create(callable $handler = null): self {
$stack = HandlerStack::create($handler);
// use after method because middleware executes in reverse order
$stack->after('http_errors', ResponseConverterMiddleware::create(), 'mojang_response_converter');
$stack->push(RetryMiddleware::create(), 'retry');
return new static(new GuzzleClient([
'handler' => $stack,
'timeout' => 10,
]));
}
/** /**
* @param string $username * @param string $username
* @param int $atTime * @param int $atTime
@ -255,9 +236,25 @@ class Api {
* @return ClientInterface * @return ClientInterface
*/ */
protected function getClient(): ClientInterface { protected function getClient(): ClientInterface {
if ($this->client === null) {
$this->client = $this->createDefaultClient();
}
return $this->client; return $this->client;
} }
private function createDefaultClient(): ClientInterface {
$stack = HandlerStack::create();
// use after method because middleware executes in reverse order
$stack->after('http_errors', ResponseConverterMiddleware::create(), 'mojang_response_converter');
$stack->push(RetryMiddleware::create(), 'retry');
return new GuzzleClient([
'handler' => $stack,
'timeout' => 10,
]);
}
private function decode(string $response): array { private function decode(string $response): array {
return json_decode($response, true); return json_decode($response, true);
} }

View File

@ -9,6 +9,7 @@ use Ely\Mojang\Middleware\ResponseConverterMiddleware;
use Ely\Mojang\Middleware\RetryMiddleware; use Ely\Mojang\Middleware\RetryMiddleware;
use Ely\Mojang\Response\Properties\TexturesProperty; use Ely\Mojang\Response\Properties\TexturesProperty;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack; use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware; use GuzzleHttp\Middleware;
@ -42,11 +43,8 @@ class ApiTest extends TestCase {
$handlerStack->after('http_errors', ResponseConverterMiddleware::create(), 'mojang_responses'); $handlerStack->after('http_errors', ResponseConverterMiddleware::create(), 'mojang_responses');
$handlerStack->push(RetryMiddleware::create(), 'retry'); $handlerStack->push(RetryMiddleware::create(), 'retry');
$client = new Client(['handler' => $handlerStack]); $client = new Client(['handler' => $handlerStack]);
$this->api = new Api($client); $this->api = new Api();
} $this->api->setClient($client);
public function testCreate() {
$this->assertInstanceOf(Api::class, Api::create());
} }
public function testUsernameToUuid() { public function testUsernameToUuid() {
@ -340,6 +338,15 @@ class ApiTest extends TestCase {
$this->api->hasJoinedServer('MockedUsername', 'ad72fe1efe364e6eb78c644a9fba1d30'); $this->api->hasJoinedServer('MockedUsername', 'ad72fe1efe364e6eb78c644a9fba1d30');
} }
public function testGetClient() {
$child = new class extends Api {
public function getDefaultClient() {
return $this->getClient();
}
};
$this->assertInstanceOf(ClientInterface::class, $child->getDefaultClient());
}
private function createResponse(int $statusCode, array $response): ResponseInterface { private function createResponse(int $statusCode, array $response): ResponseInterface {
return new Response($statusCode, ['content-type' => 'json'], json_encode($response)); return new Response($statusCode, ['content-type' => 'json'], json_encode($response));
} }