From 2459c38c906de034def7889718a438f02e43be69 Mon Sep 17 00:00:00 2001 From: phamthao Date: Tue, 29 Jun 2021 09:03:36 +0700 Subject: [PATCH] add ReferToken Plugin --- CHANGELOG.md | 4 +++ README.md | 7 +++++ src/Client.php | 22 ++++++++++++++- src/HttpClient/Plugin/ReferToken.php | 40 ++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/HttpClient/Plugin/ReferToken.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 10e7635..fd459a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `ghtk-sdk` will be documented in this file. +## 0.0.2 - 2021-06-29 + +- initial release + ## 0.0.1 - 2021-06-24 - initial release diff --git a/README.md b/README.md index ee0ff60..841e4d3 100644 --- a/README.md +++ b/README.md @@ -292,6 +292,13 @@ $client = new \Vanthao03596\GhtkSdk\Client( $client->authenticate('APITokenSample-ca441e70288cB0515F310742'); +// Or use ReferToken plugin + +$client = Vanthao03596\GhtkSdk\Client::createWithHttpClient(new Http\Adapter\Guzzle7\Client, true); // true is production mode + +$client->authenticate('APITokenSample-ca441e70288cB0515F310742'); +$client->setReferToken('B2CToken-hlsheiwquhrksadlfkjahsdfjaaljh') + .... ``` diff --git a/src/Client.php b/src/Client.php index c125750..d2e91a9 100644 --- a/src/Client.php +++ b/src/Client.php @@ -16,6 +16,7 @@ use Vanthao03596\GhtkSdk\HttpClient\Builder; use Vanthao03596\GhtkSdk\HttpClient\Plugin\Authentication; use Vanthao03596\GhtkSdk\HttpClient\Plugin\History; +use Vanthao03596\GhtkSdk\HttpClient\Plugin\ReferToken; /** * PHP GHTK client. @@ -129,7 +130,7 @@ public function api($name): AbstractApi /** * Authenticate a user for all next requests. * - * @param string $token GitHub private token + * @param string $token GHTK private token * * @throws InvalidArgumentException If no token * @@ -214,4 +215,23 @@ protected function getHttpClientBuilder(): Builder { return $this->httpClientBuilder; } + + /** + * Set Refer Token for all next requests. + * + * @param string $token GHTK private token + * + * @throws InvalidArgumentException If no token + * + * @return void + */ + public function setReferToken(string $token): void + { + if (empty($token)) { + throw new InvalidArgumentException('You need token!'); + } + + $this->getHttpClientBuilder()->removePlugin(ReferToken::class); + $this->getHttpClientBuilder()->addPlugin(new ReferToken($token)); + } } diff --git a/src/HttpClient/Plugin/ReferToken.php b/src/HttpClient/Plugin/ReferToken.php new file mode 100644 index 0000000..56ba506 --- /dev/null +++ b/src/HttpClient/Plugin/ReferToken.php @@ -0,0 +1,40 @@ +token = $token; + } + + /** + * @return Promise + */ + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise + { + $request = $request->withHeader('X-Refer-Token', $this->token); + + return $next($request); + } +}