Skip to content

Commit

Permalink
Merge pull request #14 from seatplus/develop
Browse files Browse the repository at this point in the history
Throw Error On UpdateRequests and Workflow Rename
  • Loading branch information
herpaderpaldent authored Nov 26, 2021
2 parents 078888e + a625846 commit 08569ac
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Laravel
name: run-tests

on:
push:
Expand Down
39 changes: 30 additions & 9 deletions src/Services/UpdateRefreshTokenService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
use Firebase\JWT\JWK;
use Firebase\JWT\JWT;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\RequestOptions;
use Seatplus\EsiClient\DataTransferObjects\EsiAuthentication;
use Seatplus\EsiClient\DataTransferObjects\EsiResponse;
use Seatplus\EsiClient\Exceptions\RequestFailedException;
use UnexpectedValueException;

class UpdateRefreshTokenService
Expand All @@ -19,19 +23,36 @@ class UpdateRefreshTokenService
public const TRANQUILITY_ENDPOINT = 'https://login.eveonline.com';
private Client $client;

/**
* @throws RequestFailedException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getRefreshTokenResponse(EsiAuthentication $authentication) : array
{
$authorization = 'Basic '.base64_encode($authentication->client_id.':'.$authentication->secret);

$response = $this->getClient()->post($this->getTokenUrl(), [
RequestOptions::HEADERS => [
'Authorization' => $authorization,
],
RequestOptions::FORM_PARAMS => [
'grant_type' => 'refresh_token',
'refresh_token' => $authentication->refresh_token,
],
]);
try {
$response = $this->getClient()->post($this->getTokenUrl(), [
RequestOptions::HEADERS => [
'Authorization' => $authorization,
],
RequestOptions::FORM_PARAMS => [
'grant_type' => 'refresh_token',
'refresh_token' => $authentication->refresh_token,
],
]);
} catch (ClientException | ServerException $exception) {
// Raise the exception that should be handled by the caller
throw new RequestFailedException(
$exception,
new EsiResponse(
$exception->getResponse()->getBody()->getContents(),
$exception->getResponse()->getHeaders(),
'now',
$exception->getResponse()->getStatusCode()
)
);
}

// Values are access_token // expires_in // token_type // refresh_token
$payload = json_decode((string) $response->getBody(), true);
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/Services/RefreshTokenServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,23 @@
->toHaveKey('access_token', $jwt_token)
->toHaveKey('foo', 'bar');
});

it('throws RequestFailedException if an exception occurs', function () {

// create the client mock and responses from said client
$mock = new \GuzzleHttp\Handler\MockHandler([
new Response(400, [], 'Error'),
]);

$client = new Client([
'handler' => HandlerStack::create($mock),
]);

// construct the service
$service = new \Seatplus\EsiClient\Services\UpdateRefreshTokenService();

// set the client
$service->setClient($client);

$service->getRefreshTokenResponse(buildEsiAuthentication());
})->throws(\Seatplus\EsiClient\Exceptions\RequestFailedException::class);

0 comments on commit 08569ac

Please sign in to comment.