Skip to content
This repository has been archived by the owner on Nov 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from cbzink/fix-token-usage
Browse files Browse the repository at this point in the history
Fix API tokens not working correctly
  • Loading branch information
cbzink authored Aug 6, 2022
2 parents c11dd39 + 12fbf30 commit 06a9def
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/Exceptions/InvalidTokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace cbzink\LaunchLibrary\Exceptions;

use Exception;

class InvalidTokenException extends Exception
{
/**
* Create a new exception instance.
*/
public function __construct()
{
parent::__construct('Invalid token.');
}
}
15 changes: 10 additions & 5 deletions src/LL2.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@ public function __construct($apiToken = null, $apiEndpoint = null, $guzzle = nul
$this->apiToken = $apiToken;
$this->apiEndpoint = $apiEndpoint ?: 'https://ll.thespacedevs.com/2.2.0/';

$headers = [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
];

if ($this->apiToken) {
$headers['Authorization'] = "Token {$this->apiToken}";
}

$this->guzzle = $guzzle ?: new HttpClient([
'base_uri' => 'https://ll.thespacedevs.com/2.2.0/',
'http_errors' => false,
'headers' => [
'Token' => $this->apiToken,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
'headers' => $headers,
]);
}

Expand Down
5 changes: 5 additions & 0 deletions src/MakesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GuzzleHttp\Psr7\Response;
use cbzink\LaunchLibrary\Exceptions\NotFoundException;
use cbzink\LaunchLibrary\Exceptions\RateLimitException;
use cbzink\LaunchLibrary\Exceptions\InvalidTokenException;

trait MakesHttpRequests
{
Expand Down Expand Up @@ -78,6 +79,10 @@ protected function request(string $method, string $uri, array $params = [])
*/
protected function handleRequestError(Response $response): void
{
if ($response->getStatusCode() === 401) {
throw new InvalidTokenException();
}

if ($response->getStatusCode() === 429) {
$body = json_decode((string) $response->getBody(), true);

Expand Down
17 changes: 17 additions & 0 deletions tests/LL2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use cbzink\LaunchLibrary\Api\SpaceStation;
use cbzink\LaunchLibrary\Api\Launch\Launch;
use cbzink\LaunchLibrary\Api\Spacecraft\Spacecraft;
use cbzink\LaunchLibrary\Exceptions\InvalidTokenException;
use cbzink\LaunchLibrary\Exceptions\NotFoundException;
use cbzink\LaunchLibrary\Exceptions\RateLimitException;
use cbzink\LaunchLibrary\Exceptions\UnknownApiException;
Expand Down Expand Up @@ -79,6 +80,22 @@ public function testMakesGetRequests()
]);
}

public function testHandlesInvalidTokenErrors()
{
$this->expectException(InvalidTokenException::class);

$guzzle = Mockery::mock(HttpClient::class);
$client = new LL2(null, null, $guzzle);

$guzzle->shouldReceive('request')->once()->with('GET', 'agencies', [
'query' => ['limit' => $client->getPaginationLimit()],
])->andReturn(
new Response(401, [], '{"detail": "Invalid token."}')
);

$client->get('agencies');
}

public function testHandlesRateLimitErrors()
{
$this->expectException(RateLimitException::class);
Expand Down

0 comments on commit 06a9def

Please sign in to comment.