From c5f432b659a0d49042689c5aa7976f04997bc588 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Sun, 7 Apr 2019 01:29:16 +0200 Subject: [PATCH] Remove static create method and clear the constructor. Add CHANGELOG.md --- CHANGELOG.md | 26 ++++++++++++++++++++++++++ README.md | 4 ++-- src/Api.php | 37 +++++++++++++++++-------------------- tests/ApiTest.php | 17 ++++++++++++----- 4 files changed, 57 insertions(+), 27 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..07589a2 --- /dev/null +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 43be519..0f5e1f2 100644 --- a/README.md +++ b/README.md @@ -23,11 +23,11 @@ composer require ely/mojang-api ## 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 usernameToUUID('erickskrauch'); echo $response->getId(); ``` diff --git a/src/Api.php b/src/Api.php index fcb4fa5..085d996 100644 --- a/src/Api.php +++ b/src/Api.php @@ -20,29 +20,10 @@ class Api { */ private $client; - public function __construct(ClientInterface $client) { + public function setClient(ClientInterface $client): void { $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 int $atTime @@ -255,9 +236,25 @@ class Api { * @return ClientInterface */ protected function getClient(): ClientInterface { + if ($this->client === null) { + $this->client = $this->createDefaultClient(); + } + 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 { return json_decode($response, true); } diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 877356b..d5977ba 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -9,6 +9,7 @@ use Ely\Mojang\Middleware\ResponseConverterMiddleware; use Ely\Mojang\Middleware\RetryMiddleware; use Ely\Mojang\Response\Properties\TexturesProperty; use GuzzleHttp\Client; +use GuzzleHttp\ClientInterface; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Middleware; @@ -42,11 +43,8 @@ class ApiTest extends TestCase { $handlerStack->after('http_errors', ResponseConverterMiddleware::create(), 'mojang_responses'); $handlerStack->push(RetryMiddleware::create(), 'retry'); $client = new Client(['handler' => $handlerStack]); - $this->api = new Api($client); - } - - public function testCreate() { - $this->assertInstanceOf(Api::class, Api::create()); + $this->api = new Api(); + $this->api->setClient($client); } public function testUsernameToUuid() { @@ -340,6 +338,15 @@ class ApiTest extends TestCase { $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 { return new Response($statusCode, ['content-type' => 'json'], json_encode($response)); }