Skip to content

Commit

Permalink
Merge branch 'token-renewal' into 'master'
Browse files Browse the repository at this point in the history
Access token auto renewal

See merge request transip/restapi-php-library!137
  • Loading branch information
samihsoylu committed Jun 7, 2021
2 parents 502ddb6 + 629e07c commit b2cd27f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 48 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
CHANGELOG
=========

6.7.2
-----
* Access token automatically renews when it is revoked

6.7.1
-----
Added getRdata function in DnsEntry
* Added getRdata function in DnsEntry

6.7.0
-----
Expand Down
9 changes: 9 additions & 0 deletions src/Exception/HttpBadResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use RuntimeException;
use Psr\Http\Message\ResponseInterface;
use Transip\Api\Library\Exception\HttpRequest\AccessTokenException;
use Transip\Api\Library\Exception\HttpRequest\BadResponseException;
use Transip\Api\Library\Exception\HttpRequest\ConflictException;
use Transip\Api\Library\Exception\HttpRequest\ForbiddenException;
Expand All @@ -31,6 +32,11 @@ class HttpBadResponseException extends RuntimeException
*/
private $response;

private const ACCESS_TOKEN_EXCEPTION_MESSAGES = [
'Your access token has been revoked.',
'Your access token has expired.',
];

/**
* @param string $message
* @param int $code
Expand All @@ -54,6 +60,9 @@ public static function badResponseException(Exception $innerException, ResponseI
case BadResponseException::STATUS_CODE:
return new BadResponseException($errorMessage, $response->getStatusCode(), $innerException, $response);
case UnauthorizedException::STATUS_CODE:
if (in_array($errorMessage, self::ACCESS_TOKEN_EXCEPTION_MESSAGES, true)) {
return new AccessTokenException($errorMessage, $response->getStatusCode(), $innerException, $response);
}
return new UnauthorizedException($errorMessage, $response->getStatusCode(), $innerException, $response);
case ForbiddenException::STATUS_CODE:
return new ForbiddenException($errorMessage, $response->getStatusCode(), $innerException, $response);
Expand Down
7 changes: 7 additions & 0 deletions src/Exception/HttpRequest/AccessTokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Transip\Api\Library\Exception\HttpRequest;

class AccessTokenException extends UnauthorizedException
{
}
77 changes: 31 additions & 46 deletions src/HttpClient/GuzzleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
use Transip\Api\Library\Exception\ApiException;
use Transip\Api\Library\Exception\HttpClientException;
use Transip\Api\Library\Exception\HttpRequest\AccessTokenException;
use Transip\Api\Library\Exception\HttpRequestException;
use Transip\Api\Library\Exception\HttpBadResponseException;
use Exception;
Expand Down Expand Up @@ -36,21 +38,32 @@ public function setToken(string $token): void
$this->token = $token;
}

public function get(string $url, array $query = []): array
private function sendRequest(string $method, string $uri, array $options = []): ResponseInterface
{
$options = [];
if (count($query) > 0) {
$options['query'] = $query;
try {
return $this->request($method, $uri, $options);
} catch (AccessTokenException $exception) {
$this->clearCache();
$this->setToken('');
return $this->request($method, $uri, $options);
}
}

private function request(string $method, string $uri, array $options = []): ResponseInterface
{
$this->checkAndRenewToken();
$options = $this->checkAndSetTestModeToOptions($options);

try {
$response = $this->client->get("{$this->endpoint}{$url}", $options);
return $this->client->request($method, "{$this->endpoint}{$uri}", $options);
} catch (Exception $exception) {
$this->exceptionHandler($exception);
$this->handleException($exception);
}
}

public function get(string $uri, array $query = []): array
{
$response = $this->sendRequest('GET', $uri, ['query' => $query]);

if ($response->getStatusCode() !== 200) {
throw ApiException::unexpectedStatusCode($response);
Expand All @@ -71,18 +84,11 @@ public function get(string $url, array $query = []): array
return $responseBody;
}

public function post(string $url, array $body = []): void
public function post(string $uri, array $body = []): void
{
$options['body'] = json_encode($body);

$this->checkAndRenewToken();
$options = $this->checkAndSetTestModeToOptions($options);

try {
$response = $this->client->post("{$this->endpoint}{$url}", $options);
} catch (Exception $exception) {
$this->exceptionHandler($exception);
}
$response = $this->sendRequest('POST', $uri, $options);

if ($response->getStatusCode() !== 201) {
throw ApiException::unexpectedStatusCode($response);
Expand All @@ -91,15 +97,15 @@ public function post(string $url, array $body = []): void
$this->parseResponseHeaders($response);
}

public function postAuthentication(string $url, string $signature, array $body): array
public function postAuthentication(string $uri, string $signature, array $body): array
{
$options['headers'] = ['Signature' => $signature];
$options['body'] = json_encode($body);

try {
$response = $this->client->post("{$this->endpoint}{$url}", $options);
$response = $this->client->post("{$this->endpoint}{$uri}", $options);
} catch (Exception $exception) {
$this->exceptionHandler($exception);
$this->handleException($exception);
}

if ($response->getStatusCode() !== 201) {
Expand All @@ -119,18 +125,11 @@ public function postAuthentication(string $url, string $signature, array $body):
return $responseBody;
}

public function put(string $url, array $body): void
public function put(string $uri, array $body): void
{
$options['body'] = json_encode($body);

$this->checkAndRenewToken();
$options = $this->checkAndSetTestModeToOptions($options);

try {
$response = $this->client->put("{$this->endpoint}{$url}", $options);
} catch (Exception $exception) {
$this->exceptionHandler($exception);
}
$response = $this->sendRequest('PUT', $uri, $options);

if ($response->getStatusCode() !== 204) {
throw ApiException::unexpectedStatusCode($response);
Expand All @@ -139,18 +138,11 @@ public function put(string $url, array $body): void
$this->parseResponseHeaders($response);
}

public function patch(string $url, array $body): void
public function patch(string $uri, array $body): void
{
$options['body'] = json_encode($body);

$this->checkAndRenewToken();
$options = $this->checkAndSetTestModeToOptions($options);

try {
$response = $this->client->patch("{$this->endpoint}{$url}", $options);
} catch (Exception $exception) {
$this->exceptionHandler($exception);
}
$response = $this->sendRequest('PATCH', $uri, $options);

if ($response->getStatusCode() !== 204) {
throw ApiException::unexpectedStatusCode($response);
Expand All @@ -159,18 +151,11 @@ public function patch(string $url, array $body): void
$this->parseResponseHeaders($response);
}

public function delete(string $url, array $body = []): void
public function delete(string $uri, array $body = []): void
{
$options['body'] = json_encode($body);

$this->checkAndRenewToken();
$options = $this->checkAndSetTestModeToOptions($options);

try {
$response = $this->client->delete("{$this->endpoint}{$url}", $options);
} catch (Exception $exception) {
$this->exceptionHandler($exception);
}
$response = $this->sendRequest('DELETE', $uri, $options);

if ($response->getStatusCode() !== 204) {
throw ApiException::unexpectedStatusCode($response);
Expand All @@ -179,7 +164,7 @@ public function delete(string $url, array $body = []): void
$this->parseResponseHeaders($response);
}

private function exceptionHandler(Exception $exception): void
private function handleException(Exception $exception): void
{
if ($exception instanceof BadResponseException) {
if ($exception->hasResponse()) {
Expand Down
2 changes: 1 addition & 1 deletion src/TransipAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
class TransipAPI
{
public const TRANSIP_API_ENDPOINT = "https://api.transip.nl/v6";
public const TRANSIP_API_LIBRARY_VERSION = "6.7.1";
public const TRANSIP_API_LIBRARY_VERSION = "6.7.2";
public const TRANSIP_API_DEMO_TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImN3MiFSbDU2eDNoUnkjelM4YmdOIn0.eyJpc3MiOiJhcGkudHJhbnNpcC5ubCIsImF1ZCI6ImFwaS50cmFuc2lwLm5sIiwianRpIjoiY3cyIVJsNTZ4M2hSeSN6UzhiZ04iLCJpYXQiOjE1ODIyMDE1NTAsIm5iZiI6MTU4MjIwMTU1MCwiZXhwIjoyMTE4NzQ1NTUwLCJjaWQiOiI2MDQ0OSIsInJvIjpmYWxzZSwiZ2siOmZhbHNlLCJrdiI6dHJ1ZX0.fYBWV4O5WPXxGuWG-vcrFWqmRHBm9yp0PHiYh_oAWxWxCaZX2Rf6WJfc13AxEeZ67-lY0TA2kSaOCp0PggBb_MGj73t4cH8gdwDJzANVxkiPL1Saqiw2NgZ3IHASJnisUWNnZp8HnrhLLe5ficvb1D9WOUOItmFC2ZgfGObNhlL2y-AMNLT4X7oNgrNTGm-mespo0jD_qH9dK5_evSzS3K8o03gu6p19jxfsnIh8TIVRvNdluYC2wo4qDl5EW5BEZ8OSuJ121ncOT1oRpzXB0cVZ9e5_UVAEr9X3f26_Eomg52-PjrgcRJ_jPIUYbrlo06KjjX2h0fzMr21ZE023Gw";

/**
Expand Down

0 comments on commit b2cd27f

Please sign in to comment.