Skip to content

Commit

Permalink
Recycle access token.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsegura committed Mar 26, 2021
1 parent b959d54 commit 52f7d12
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/AccessTokenHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class AccessTokenHandler
{
private $client;
private $accessToken;

public function __construct(Client $client)
{
Expand All @@ -29,22 +30,27 @@ public function __invoke(callable $handler)

private function getAccessToken()
{
$base64Credentials = base64_encode(sprintf('%s:%s',
$this->client->getConfig('client_id'),
$this->client->getConfig('client_secret')));

$response = $this->client->post('/oauth2/token', [
'headers' => [
'Authorization' => sprintf('Basic %s', $base64Credentials)
],
'form_params' => [
'grant_type' => 'client_credentials',
'scope' => 'tasks deliveries'
]
]);

$data = json_decode((string) $response->getBody(), true);

return $data['access_token'];
if (null === $this->accessToken) {

$base64Credentials = base64_encode(sprintf('%s:%s',
$this->client->getConfig('client_id'),
$this->client->getConfig('client_secret')));

$response = $this->client->post('/oauth2/token', [
'headers' => [
'Authorization' => sprintf('Basic %s', $base64Credentials)
],
'form_params' => [
'grant_type' => 'client_credentials',
'scope' => 'tasks deliveries'
]
]);

$data = json_decode((string) $response->getBody(), true);

$this->accessToken = $data['access_token'];
}

return $this->accessToken;
}
}

0 comments on commit 52f7d12

Please sign in to comment.