generated from worksome/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Saifallak/main
Support api.getgeoapi.com.
- Loading branch information
Showing
7 changed files
with
218 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Worksome\Exchange\ExchangeRateProviders; | ||
|
||
use Illuminate\Http\Client\Factory; | ||
use Illuminate\Http\Client\PendingRequest; | ||
use Illuminate\Http\Client\RequestException; | ||
use Illuminate\Support\Collection; | ||
use Worksome\Exchange\Contracts\ExchangeRateProvider; | ||
use Worksome\Exchange\Support\Rates; | ||
|
||
final class CurrencyGEOProvider implements ExchangeRateProvider | ||
{ | ||
public function __construct( | ||
private Factory $client, | ||
private string $accessKey, | ||
private string $baseUrl = 'https://api.getgeoapi.com/v2', | ||
) { | ||
} | ||
|
||
/** | ||
* @throws RequestException | ||
*/ | ||
public function getRates(string $baseCurrency, array $currencies): Rates | ||
{ | ||
// If it's the same currency return it. | ||
if (count($currencies) === 1 && $baseCurrency === $currencies[0]) { | ||
return new Rates( | ||
$baseCurrency, | ||
[$baseCurrency => 1], | ||
now()->startOfDay(), | ||
); | ||
} | ||
|
||
$data = $this->makeRequest($baseCurrency, $currencies); | ||
|
||
return new Rates( | ||
$baseCurrency, | ||
// @phpstan-ignore-next-line | ||
collect($data->get('rates'))->map(fn (mixed $value) => floatval($value['rate']))->all(), | ||
now()->startOfDay(), | ||
); | ||
} | ||
|
||
/** | ||
* @param array<int, string> $currencies | ||
* | ||
* @return Collection<string, mixed> | ||
* | ||
* @throws RequestException | ||
*/ | ||
private function makeRequest(string $baseCurrency, array $currencies): Collection | ||
{ | ||
return $this->client() | ||
->get('/currency/convert', [ | ||
'api_key' => $this->accessKey, | ||
'from' => $baseCurrency, | ||
'to' => implode(',', $currencies), | ||
]) | ||
->throw() | ||
->collect(); | ||
} | ||
|
||
private function client(): PendingRequest | ||
{ | ||
return $this->client | ||
->baseUrl($this->baseUrl) | ||
->asJson() | ||
->acceptJson(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
tests/Unit/ExchangeRateProviders/CurrencyGEOProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
use Carbon\Carbon; | ||
use GuzzleHttp\Promise\Create; | ||
use GuzzleHttp\Psr7\Response; | ||
use Illuminate\Http\Client\Factory; | ||
use Illuminate\Http\Client\Request; | ||
use Illuminate\Http\Client\RequestException; | ||
use Worksome\Exchange\ExchangeRateProviders\CurrencyGEOProvider; | ||
use Worksome\Exchange\Support\Rates; | ||
|
||
it('is able to make a real call to the API', function () { | ||
$client = new Factory(); | ||
$currencyGeoProvider = new CurrencyGEOProvider($client, getenv('CURRENCY_GEO_ACCESS_KEY')); | ||
$rates = $currencyGeoProvider->getRates('EUR', currencies()); | ||
|
||
expect($rates)->toBeInstanceOf(Rates::class); | ||
}) | ||
->skip(getenv('CURRENCY_GEO_ACCESS_KEY') === false, 'No CURRENCY_GEO_ACCESS_KEY was defined.') | ||
->group('integration'); | ||
|
||
it('makes a HTTP request to the correct endpoint', function () { | ||
$client = new Factory(); | ||
$client->fake(['*' => [ | ||
'timestamp' => now()->subDay()->timestamp, | ||
'rates' => [ | ||
'EUR' => [ | ||
'currency_name' => 'Euro', | ||
'rate' => 1, | ||
'rate_for_amount' => 1, | ||
], | ||
'GBP' => [ | ||
'currency_name' => 'Pound sterling', | ||
'rate' => 2.5, | ||
'rate_for_amount' => 2.5, | ||
], | ||
], | ||
]]); | ||
|
||
$currencyGeoProvider = new CurrencyGEOProvider($client, 'password'); | ||
$currencyGeoProvider->getRates('EUR', currencies()); | ||
|
||
$client->assertSent(function (Request $request) { | ||
return str_starts_with($request->url(), 'https://api.getgeoapi.com/v2/currency/convert'); | ||
}); | ||
}); | ||
|
||
it('returns floats for all rates', function () { | ||
$client = new Factory(); | ||
$client->fake(['*' => [ | ||
'timestamp' => now()->subDay()->timestamp, | ||
'rates' => [ | ||
'EUR' => [ | ||
'currency_name' => 'Euro', | ||
'rate' => 1, | ||
'rate_for_amount' => 1, | ||
], | ||
'GBP' => [ | ||
'currency_name' => 'Pound sterling', | ||
'rate' => 2.5, | ||
'rate_for_amount' => 2.5, | ||
], | ||
], | ||
]]); | ||
|
||
$currencyGeoProvider = new CurrencyGEOProvider($client, 'password'); | ||
$rates = $currencyGeoProvider->getRates('EUR', currencies()); | ||
|
||
expect($rates->getRates())->each->toBeFloat(); | ||
}); | ||
|
||
it('sets the returned timestamp as the retrievedAt timestamp', function () { | ||
Carbon::setTestNow(now()); | ||
|
||
$client = new Factory(); | ||
$client->fake(['*' => [ | ||
'timestamp' => now()->subDay()->timestamp, | ||
'rates' => [], | ||
]]); | ||
|
||
$currencyGeoProvider = new CurrencyGEOProvider($client, 'password'); | ||
$rates = $currencyGeoProvider->getRates('EUR', currencies()); | ||
|
||
expect($rates->getRetrievedAt()->timestamp)->toBe(now()->startOfDay()->timestamp); | ||
}); | ||
|
||
it('throws a RequestException if a 500 error occurs', function () { | ||
$client = new Factory(); | ||
$client->fake(['*' => Create::promiseFor(new Response(500))]); | ||
|
||
$currencyGeoProvider = new CurrencyGEOProvider($client, 'password'); | ||
$currencyGeoProvider->getRates('EUR', currencies()); | ||
})->throws(RequestException::class); |