Skip to content

Commit

Permalink
add ReferToken Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
vanthao03596 committed Jun 29, 2021
1 parent 1d5e29c commit 2459c38
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')

....
```

Expand Down
22 changes: 21 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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));
}
}
40 changes: 40 additions & 0 deletions src/HttpClient/Plugin/ReferToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Vanthao03596\GhtkSdk\HttpClient\Plugin;

use Http\Client\Common\Plugin;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;

final class ReferToken implements Plugin
{
/**
* The authorization token.
*
* @var string
*/
private $token;

/**
* Create a new authentication plugin instance.
*
* @param string $token
* @return void
*/
public function __construct(string $token)
{
$this->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);
}
}

0 comments on commit 2459c38

Please sign in to comment.