Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve issues with ExchangeRate.host #23

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ jobs:
test:
runs-on: ${{ matrix.os }}
env:
# Fixer and ExchangeRate.host use the same API token
FIXER_ACCESS_KEY: ${{ secrets.FIXER_ACCESS_KEY }}
EXCHANGE_RATE_ACCESS_KEY: ${{ secrets.FIXER_ACCESS_KEY }}
strategy:
fail-fast: true
matrix:
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,21 @@ Fixer for up-to-date exchange rates.

### ExchangeRate.host

[exchangerate.host](https://exchangerate.host/) is a free alternative to Fixer with an identical API spec. You
don't even need an API key!
[exchangerate.host](https://exchangerate.host) is an alternative to Fixer with an identical API spec.

In your `exchange.php` config file, set `default` to `exchange_rate`, or set `EXCHANGE_DRIVER` to `exchange_rate` in your `.env` file.
Set `EXCHANGE_RATE_ACCESS_KEY` to your provided access key from exchangerate.host.

With that task completed, you're ready to start using [exchangerate.host](https://exchangerate.host/) for retrieving up-to-date
With that task completed, you're ready to start using [exchangerate.host](https://exchangerate.host) for retrieving up-to-date
exchange rates.

### Frankfurter.app

[frankfurter.app](https://frankfurter.app/) is an open-source API for current and historical foreign exchange rates published by the European Central Bank, which can be used without an API key.
[frankfurter.app](https://frankfurter.app) is an open-source API for current and historical foreign exchange rates published by the European Central Bank, which can be used without an API key.

In your `exchange.php` config file, set `default` to `frankfurter`, or set `EXCHANGE_DRIVER` to `frankfurter` in your `.env` file.

With that task completed, you're ready to start using [frankfurter.app](https://frankfurter.app/) for retrieving up-to-date
With that task completed, you're ready to start using [frankfurter.app](https://frankfurter.app) for retrieving up-to-date
exchange rates.

### Cache
Expand Down
15 changes: 15 additions & 0 deletions config/exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@
'access_key' => env('FIXER_ACCESS_KEY'),
],

/*
|--------------------------------------------------------------------------
| ExchangeRate.host
|--------------------------------------------------------------------------
|
| ExchangeRate is a paid service for converting currency codes. To use ExchangeRate, you'll
| need an API Access Key from the ExchangeRate dashboard. Set that here, and then change
| the 'default' to 'exchange_rate' or set EXCHANGE_DRIVER to 'exchange_rate'.
|
*/

'exchange_rate' => [
'access_key' => env('EXCHANGE_RATE_ACCESS_KEY'),
],

/*
|--------------------------------------------------------------------------
| Cache
Expand Down
4 changes: 2 additions & 2 deletions src/ExchangeRateProviders/ExchangeRateHostProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ final class ExchangeRateHostProvider implements ExchangeRateProvider
{
private FixerProvider $fixerProvider;

public function __construct(private Factory $client)
public function __construct(private Factory $client, private string $accessKey)
{
$this->fixerProvider = new FixerProvider($this->client, '', 'https://api.exchangerate.host');
$this->fixerProvider = new FixerProvider($this->client, $this->accessKey, 'https://api.exchangerate.host');
}

public function getRates(string $baseCurrency, array $currencies): Rates
Expand Down
7 changes: 7 additions & 0 deletions src/Support/ExchangeRateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,15 @@ public function createFixerDriver(): FixerProvider

public function createExchangeRateDriver(): ExchangeRateHostProvider
{
$apiKey = $this->config->get('exchange.services.exchange_rate.access_key');

throw_unless(is_string($apiKey), new InvalidConfigurationException(
'You haven\'t set up an API key for ExchangeRate!'
));

return new ExchangeRateHostProvider(
$this->container->make(Factory::class),
$apiKey,
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Commands/ViewLatestRatesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
->assertFailed();
});

it('retrieves rates using the ExchangeRateProvider', function () {
it('retrieves rates using the default provider', function () {
Exchange::fake(['GBP' => 1.2]);

$this
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Support/ExchangeRateManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

beforeEach(function () {
config()->set('exchange.services.fixer.access_key', 'password');
config()->set('exchange.services.exchange_rate.access_key', 'password');
});

it('can instantiate all drivers', function (string $driver, string $expectedClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

it('is able to make a real call to the API', function () {
$client = new Factory();
$fixerProvider = new ExchangeRateHostProvider($client);
$fixerProvider = new ExchangeRateHostProvider($client, getenv('EXCHANGE_RATE_ACCESS_KEY'));
$rates = $fixerProvider->getRates('EUR', currencies());

expect($rates)->toBeInstanceOf(Rates::class);
})->group('integration');
})
->skip(getenv('EXCHANGE_RATE_ACCESS_KEY') === false, 'No EXCHANGE_RATE_ACCESS_KEY was defined.')
->group('integration');

it('makes a HTTP request to the correct endpoint', function () {
$client = new Factory();
Expand All @@ -27,7 +29,7 @@
],
]]);

$fixerProvider = new ExchangeRateHostProvider($client);
$fixerProvider = new ExchangeRateHostProvider($client, 'password');
$fixerProvider->getRates('EUR', currencies());

$client->assertSent(function (Request $request) {
Expand All @@ -45,7 +47,7 @@
],
]]);

$fixerProvider = new ExchangeRateHostProvider($client);
$fixerProvider = new ExchangeRateHostProvider($client, 'password');
$rates = $fixerProvider->getRates('EUR', currencies());

expect($rates->getRates())->each->toBeFloat();
Expand All @@ -60,7 +62,7 @@
'rates' => [],
]]);

$fixerProvider = new ExchangeRateHostProvider($client);
$fixerProvider = new ExchangeRateHostProvider($client, 'password');
$rates = $fixerProvider->getRates('EUR', currencies());

expect($rates->getRetrievedAt()->timestamp)->toBe(now()->subDay()->timestamp);
Expand All @@ -70,6 +72,6 @@
$client = new Factory();
$client->fake(['*' => Create::promiseFor(new Response(500))]);

$fixerProvider = new ExchangeRateHostProvider($client);
$fixerProvider = new ExchangeRateHostProvider($client, 'password');
$fixerProvider->getRates('EUR', currencies());
})->throws(RequestException::class);