From 5647ce28ea53aff6804a3888f065ea905bbdddbb Mon Sep 17 00:00:00 2001 From: Dick van der Heiden Date: Sat, 9 Dec 2023 21:29:13 +0100 Subject: [PATCH 1/3] Allow a custom Guzzle client to be provided --- readme.md | 8 ++++++++ src/Client.php | 6 ++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index aca992a..b20076e 100644 --- a/readme.md +++ b/readme.md @@ -71,6 +71,14 @@ require 'vendor/autoload.php'; $client = new Xolphin\Client('', ''); ``` +When needed, you are able to provide a custom GuzzleHttp client when initializing the Xolphin client: + +```php +$client = new Xolphin\Client('', '', true, new GuzzleHttp\Client([ + ... +])); +``` + ### Rate Limiting #### Current limit diff --git a/src/Client.php b/src/Client.php index 11e2465..7afd8ca 100644 --- a/src/Client.php +++ b/src/Client.php @@ -4,6 +4,7 @@ namespace Xolphin; +use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\RequestException; use Psr\Http\Message\StreamInterface; @@ -52,8 +53,9 @@ class Client * @param string|null $username * @param string|null $password * @param bool $test + * @param ClientInterface|null $httpClient */ - function __construct(?string $username = null, ?string $password = null, bool $test = false) + function __construct(?string $username = null, ?string $password = null, bool $test = false, ClientInterface $httpClient = null) { $this->username = $username; $this->password = $password; @@ -76,7 +78,7 @@ function __construct(?string $username = null, ?string $password = null, bool $t $options['verify'] = false; } - $this->guzzle = new \GuzzleHttp\Client($options); + $this->guzzle = $httpClient ?? new \GuzzleHttp\Client($options); $this->initializeEndpoints(); } From d9b09d4312796e9de2e5cab7f3ed864e6319a36e Mon Sep 17 00:00:00 2001 From: Dick van der Heiden Date: Sat, 9 Dec 2023 21:31:53 +0100 Subject: [PATCH 2/3] Improve readme --- readme.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index b20076e..00cd129 100644 --- a/readme.md +++ b/readme.md @@ -74,11 +74,13 @@ $client = new Xolphin\Client('', ''); When needed, you are able to provide a custom GuzzleHttp client when initializing the Xolphin client: ```php -$client = new Xolphin\Client('', '', true, new GuzzleHttp\Client([ +$client = new Xolphin\Client(httpClient: new GuzzleHttp\Client([ ... ])); ``` +While this feature can be useful for applying your own middleware, make sure to also implement the authentication yourself. + ### Rate Limiting #### Current limit From 4e54f175854c7a22d78a2dffd6277c23dc732f3d Mon Sep 17 00:00:00 2001 From: Dick van der Heiden <90568118+dvdheiden@users.noreply.github.com> Date: Fri, 26 Apr 2024 20:32:13 +0200 Subject: [PATCH 3/3] Properly handle streams when using custom middleware --- src/Client.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Client.php b/src/Client.php index 7afd8ca..5a2f82c 100644 --- a/src/Client.php +++ b/src/Client.php @@ -125,7 +125,7 @@ public function get(string $uri, array $data = []) ); return json_decode( - $result->getBody()->getContents() + (string)$result->getBody() ); } catch (RequestException $e) { @@ -167,7 +167,7 @@ public function post(string $uri, array $data = []) ); return json_decode( - $result->getBody()->getContents() + (string)$result->getBody() ); } catch (RequestException $e) { throw XolphinRequestException::createFromRequestException($e); @@ -190,7 +190,7 @@ public function download(string $uri, array $data = []): StreamInterface (int) $result->getHeader('X-RateLimit-Remaining')[0] ); - return $result->getBody(); + return $result->getBody()->rewind(); } catch (RequestException $e) { throw XolphinRequestException::createFromRequestException($e); }