Skip to content

Commit

Permalink
Merge pull request #126 from spotwilliams/response-abstraction
Browse files Browse the repository at this point in the history
Response abstraction
  • Loading branch information
ash-jc-allen authored Mar 23, 2023
2 parents 1a92d84 + 87bf74c commit 4ce5d4f
Show file tree
Hide file tree
Showing 24 changed files with 277 additions and 161 deletions.
10 changes: 7 additions & 3 deletions src/Drivers/ExchangeRateHost/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace AshAllenDesign\LaravelExchangeRates\Drivers\ExchangeRateHost;

use AshAllenDesign\LaravelExchangeRates\Drivers\ExchangeRatesApiIo\Response;
use AshAllenDesign\LaravelExchangeRates\Interfaces\RequestSender;
use AshAllenDesign\LaravelExchangeRates\Interfaces\ResponseContract;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;

Expand All @@ -15,15 +17,17 @@ class RequestBuilder implements RequestSender
*
* @param string $path
* @param array<string, string> $queryParams
* @return mixed
* @return ResponseContract
*
* @throws RequestException
*/
public function makeRequest(string $path, array $queryParams = []): mixed
public function makeRequest(string $path, array $queryParams = []): ResponseContract
{
return Http::baseUrl(self::BASE_URL)
$rawResponse = Http::baseUrl(self::BASE_URL)
->get($path, $queryParams)
->throw()
->json();

return new Response($rawResponse);
}
}
27 changes: 27 additions & 0 deletions src/Drivers/ExchangeRateHost/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace AshAllenDesign\LaravelExchangeRates\Drivers\ExchangeRateHost;

use AshAllenDesign\LaravelExchangeRates\Interfaces\ResponseContract;

class Response implements ResponseContract
{
public function __construct(private array $rawResponse)
{
}

public function get(string $key): mixed
{
return $this->rawResponse[$key];
}

public function rates(): array
{
return $this->get('rates');
}

public function raw(): mixed
{
return $this->rawResponse;
}
}
9 changes: 6 additions & 3 deletions src/Drivers/ExchangeRatesApiIo/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AshAllenDesign\LaravelExchangeRates\Drivers\ExchangeRatesApiIo;

use AshAllenDesign\LaravelExchangeRates\Interfaces\RequestSender;
use AshAllenDesign\LaravelExchangeRates\Interfaces\ResponseContract;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;

Expand All @@ -22,18 +23,20 @@ public function __construct()
*
* @param string $path
* @param array<string, string> $queryParams
* @return mixed
* @return ResponseContract
*
* @throws RequestException
*/
public function makeRequest(string $path, array $queryParams = []): mixed
public function makeRequest(string $path, array $queryParams = []): ResponseContract
{
return Http::baseUrl(self::BASE_URL)
$rawResponse = Http::baseUrl(self::BASE_URL)
->get(
$path,
array_merge(['access_key' => $this->apiKey], $queryParams)
)
->throw()
->json();

return new Response($rawResponse);
}
}
27 changes: 27 additions & 0 deletions src/Drivers/ExchangeRatesApiIo/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace AshAllenDesign\LaravelExchangeRates\Drivers\ExchangeRatesApiIo;

use AshAllenDesign\LaravelExchangeRates\Interfaces\ResponseContract;

class Response implements ResponseContract
{
public function __construct(private array $rawResponse)
{
}

public function get(string $key): mixed
{
return $this->rawResponse[$key];
}

public function rates(): array
{
return $this->get('rates');
}

public function raw(): mixed
{
return $this->rawResponse;
}
}
9 changes: 6 additions & 3 deletions src/Drivers/ExchangeRatesDataApi/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace AshAllenDesign\LaravelExchangeRates\Drivers\ExchangeRatesDataApi;

use AshAllenDesign\LaravelExchangeRates\Interfaces\RequestSender;
use AshAllenDesign\LaravelExchangeRates\Interfaces\ResponseContract;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;

Expand All @@ -24,18 +25,20 @@ public function __construct()
*
* @param string $path
* @param array<string, string> $queryParams
* @return mixed
* @return ResponseContract
*
* @throws RequestException
*/
public function makeRequest(string $path, array $queryParams = []): mixed
public function makeRequest(string $path, array $queryParams = []): ResponseContract
{
return Http::baseUrl(self::BASE_URL)
$rawResponse = Http::baseUrl(self::BASE_URL)
->withHeaders([
'apiKey' => $this->apiKey,
])
->get($path, $queryParams)
->throw()
->json();

return new Response($rawResponse);
}
}
27 changes: 27 additions & 0 deletions src/Drivers/ExchangeRatesDataApi/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace AshAllenDesign\LaravelExchangeRates\Drivers\ExchangeRatesDataApi;

use AshAllenDesign\LaravelExchangeRates\Interfaces\ResponseContract;

class Response implements ResponseContract
{
public function __construct(private array $rawResponse)
{
}

public function get(string $key): mixed
{
return $this->rawResponse[$key];
}

public function rates(): array
{
return $this->get('rates');
}

public function raw(): mixed
{
return $this->rawResponse;
}
}
8 changes: 4 additions & 4 deletions src/Drivers/Support/SharedDriverLogicHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public function currencies(): array

$response = $this->requestBuilder->makeRequest('/latest', []);

$currencies = [$response['base']];
$currencies = [$response->get('base')];

foreach ($response['rates'] as $currency => $rate) {
foreach ($response->rates() as $currency => $rate) {
$currencies[] = $currency;
}

Expand Down Expand Up @@ -122,7 +122,7 @@ public function exchangeRate(string $from, string|array $to, Carbon $date = null
? '/'.$date->format('Y-m-d')
: '/latest';

$response = $this->requestBuilder->makeRequest($url, $queryParams)['rates'];
$response = $this->requestBuilder->makeRequest($url, $queryParams)->rates();

$exchangeRate = is_string($to) ? $response[$to] : $response;

Expand Down Expand Up @@ -199,7 +199,7 @@ private function makeRequestForExchangeRates(string $from, string|array $to, Car
'symbols' => $symbols,
]);

$conversions = $result['rates'];
$conversions = $result->rates();

if (is_string($to)) {
foreach ($conversions as $date => $rate) {
Expand Down
4 changes: 2 additions & 2 deletions src/Interfaces/RequestSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ interface RequestSender
*
* @param string $path
* @param string[] $queryParams
* @return mixed
* @return ResponseContract
*
* @throws RequestException
*/
public function makeRequest(string $path, array $queryParams = []): mixed;
public function makeRequest(string $path, array $queryParams = []): ResponseContract;
}
14 changes: 14 additions & 0 deletions src/Interfaces/ResponseContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace AshAllenDesign\LaravelExchangeRates\Interfaces;

interface ResponseContract
{
public function rates(): array;

public function raw(): mixed;

public function get(string $key): mixed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use AshAllenDesign\LaravelExchangeRates\Drivers\ExchangeRateHost\ExchangeRateHostDriver;
use AshAllenDesign\LaravelExchangeRates\Drivers\ExchangeRateHost\RequestBuilder;
use AshAllenDesign\LaravelExchangeRates\Drivers\ExchangeRateHost\Response;
use AshAllenDesign\LaravelExchangeRates\Exceptions\InvalidCurrencyException;
use AshAllenDesign\LaravelExchangeRates\Exceptions\InvalidDateException;
use AshAllenDesign\LaravelExchangeRates\Tests\Unit\TestCase;
Expand Down Expand Up @@ -268,9 +269,9 @@ public function exception_is_thrown_if_the_to_parameter_is_invalid(): void
$exchangeRate->convertBetweenDateRange(100, 'GBP', 'INVALID', now()->subWeek(), now()->subDay());
}

private function mockResponseForOneSymbol(): array
private function mockResponseForOneSymbol(): Response
{
return [
return new Response([
'rates' => [
'2019-11-08' => [
'EUR' => 1.1606583254,
Expand All @@ -291,12 +292,12 @@ private function mockResponseForOneSymbol(): array
'start_date' => '2019-11-03',
'base' => 'GBP',
'end_date' => '2019-11-10',
];
]);
}

private function mockResponseForMultipleSymbols(): array
private function mockResponseForMultipleSymbols(): Response
{
return [
return new Response([
'rates' => [
'2019-11-08' => [
'EUR' => 1.1606583254,
Expand All @@ -322,6 +323,6 @@ private function mockResponseForMultipleSymbols(): array
'start_date' => '2019-11-03',
'base' => 'GBP',
'end_date' => '2019-11-10',
];
]);
}
}
20 changes: 10 additions & 10 deletions tests/Unit/Drivers/ExchangeRateHost/ConvertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use AshAllenDesign\LaravelExchangeRates\Drivers\ExchangeRateHost\ExchangeRateHostDriver;
use AshAllenDesign\LaravelExchangeRates\Drivers\ExchangeRateHost\RequestBuilder;
use AshAllenDesign\LaravelExchangeRates\Drivers\ExchangeRateHost\Response;
use AshAllenDesign\LaravelExchangeRates\Exceptions\InvalidCurrencyException;
use AshAllenDesign\LaravelExchangeRates\Exceptions\InvalidDateException;
use AshAllenDesign\LaravelExchangeRates\Tests\Unit\TestCase;
Expand Down Expand Up @@ -142,9 +143,9 @@ public function exception_is_thrown_if_the_to_parameter_is_invalid(): void
$exchangeRate->convert(100, 'GBP', 'INVALID', now()->subMinute());
}

private function mockResponseForCurrentDateAndOneSymbol(): array
private function mockResponseForCurrentDateAndOneSymbol(): Response
{
return [
return new Response([
'rates' => [
'CAD' => 1.4561,
'HKD' => 8.6372,
Expand Down Expand Up @@ -181,13 +182,12 @@ private function mockResponseForCurrentDateAndOneSymbol(): array
],
'base' => 'EUR',
'date' => '2019-11-08',
];
]);
}

private function mockResponseForPastDateAndOneSymbol(): array
private function mockResponseForPastDateAndOneSymbol(): Response
{
return
[
return new Response([
'rates' => [
'CAD' => 1.4969,
'HKD' => 8.8843,
Expand Down Expand Up @@ -224,19 +224,19 @@ private function mockResponseForPastDateAndOneSymbol(): array
],
'base' => 'EUR',
'date' => '2018-11-09',
];
]);
}

private function mockResponseForCurrentDateAndMultipleSymbols(): array
private function mockResponseForCurrentDateAndMultipleSymbols(): Response
{
return [
return new Response([
'rates' => [
'CAD' => 1.4561,
'USD' => 1.1034,
'GBP' => 0.86158,
],
'base' => 'EUR',
'date' => '2019-11-08',
];
]);
}
}
7 changes: 4 additions & 3 deletions tests/Unit/Drivers/ExchangeRateHost/CurrenciesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use AshAllenDesign\LaravelExchangeRates\Drivers\ExchangeRateHost\ExchangeRateHostDriver;
use AshAllenDesign\LaravelExchangeRates\Drivers\ExchangeRateHost\RequestBuilder;
use AshAllenDesign\LaravelExchangeRates\Drivers\ExchangeRateHost\Response;
use AshAllenDesign\LaravelExchangeRates\Tests\Unit\TestCase;
use Illuminate\Support\Facades\Cache;
use Mockery;
Expand Down Expand Up @@ -77,9 +78,9 @@ public function currencies_are_not_cached_if_the_shouldCache_option_is_false():
$this->assertNull(Cache::get('laravel_xr_currencies'));
}

private function mockResponse(): array
private function mockResponse(): Response
{
return [
return new Response([
'rates' => [
'CAD' => 1.4682,
'HKD' => 8.7298,
Expand Down Expand Up @@ -116,7 +117,7 @@ private function mockResponse(): array
],
'base' => 'EUR',
'date' => '2019-11-01',
];
]);
}

private function expectedResponse(): array
Expand Down
Loading

0 comments on commit 4ce5d4f

Please sign in to comment.