Skip to content

Commit

Permalink
Merge pull request #1 from srishaharidas/XOL-5931
Browse files Browse the repository at this point in the history
XOL-5931 Add access key option for exchange rates API
  • Loading branch information
srishaharidas authored Apr 2, 2021
2 parents 00f7d4f + 58b10f4 commit 5876184
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
27 changes: 23 additions & 4 deletions src/Service/ExchangeRatesApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,26 @@ final class ExchangeRatesApi extends HttpService
{
use SupportsHistoricalQueries;

const LATEST_URL = 'https://api.exchangeratesapi.io/latest?base=%s';
const LATEST_URL = 'https://api.exchangeratesapi.io/latest?base=%s&access_key=%s';

const HISTORICAL_URL = 'https://api.exchangeratesapi.io/%s?base=%s&access_key=%s';

const ACCESS_KEY_OPTION = 'access_key';

/**
* {@inheritdoc}
*/
public function processOptions(array &$options): void
{
if (!isset($options[self::ACCESS_KEY_OPTION])) {
throw new \InvalidArgumentException('The "access_key" option must be provided to use exchangeratesapi.io');
}

if (!isset($options['enterprise'])) {
$options['enterprise'] = false;
}
}

const HISTORICAL_URL = 'https://api.exchangeratesapi.io/%s?base=%s';

/**
* {@inheritdoc}
Expand All @@ -44,7 +61,8 @@ protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery): Exch

$url = sprintf(
self::LATEST_URL,
$currencyPair->getBaseCurrency()
$currencyPair->getBaseCurrency(),
$this->options[self::ACCESS_KEY_OPTION]
);

return $this->doCreateRate($url, $currencyPair);
Expand All @@ -60,7 +78,8 @@ protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchan
$url = sprintf(
self::HISTORICAL_URL,
$exchangeQuery->getDate()->format('Y-m-d'),
$exchangeQuery->getCurrencyPair()->getBaseCurrency()
$exchangeQuery->getCurrencyPair()->getBaseCurrency(),
$this->options[self::ACCESS_KEY_OPTION]
);

return $this->doCreateRate($url, $currencyPair);
Expand Down
20 changes: 10 additions & 10 deletions tests/Tests/Service/ExchangeRatesApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ExchangeRatesApiTest extends ServiceTestCase
*/
public function it_does_support_all_queries()
{
$service = new ExchangeRatesApi($this->createMock('Http\Client\HttpClient'));
$service = new ExchangeRatesApi($this->createMock('Http\Client\HttpClient'), null, ['access_key' => 'x']);

$this->assertTrue($service->supportQuery(new ExchangeRateQuery(CurrencyPair::createFromString('USD/EUR'))));
}
Expand All @@ -39,7 +39,7 @@ public function it_does_support_all_queries()
*/
public function it_supports_eur_base()
{
$service = new ExchangeRatesApi($this->createMock('Http\Client\HttpClient'));
$service = new ExchangeRatesApi($this->createMock('Http\Client\HttpClient'), null, ['access_key' => 'x']);
$this->assertTrue($service->supportQuery(new ExchangeRateQuery(CurrencyPair::createFromString('EUR/CAD'))));
}

Expand All @@ -48,7 +48,7 @@ public function it_supports_eur_base()
*/
public function it_does_support_other_than_eur()
{
$service = new ExchangeRatesApi($this->createMock('Http\Client\HttpClient'));
$service = new ExchangeRatesApi($this->createMock('Http\Client\HttpClient'), null, ['access_key' => 'x']);
$this->assertTrue($service->supportQuery(new ExchangeRateQuery(CurrencyPair::createFromString('USD/CAD'))));
}

Expand All @@ -60,10 +60,10 @@ public function it_throws_an_exception_with_error_response()
$this->expectException(Exception::class);
$this->expectExceptionMessage('Base \'FOO\' is not supported.');

$uri = 'https://api.exchangeratesapi.io/latest?base=FOO';
$uri = 'https://api.exchangeratesapi.io/latest?base=FOO&access_key=x';
$content = file_get_contents(__DIR__.'/../../Fixtures/Service/ExchangeRatesApi/error.json');

$service = new ExchangeRatesApi($this->getHttpAdapterMock($uri, $content));
$service = new ExchangeRatesApi($this->getHttpAdapterMock($uri, $content), null, ['access_key' => 'x']);
$service->getExchangeRate(new ExchangeRateQuery(CurrencyPair::createFromString('FOO/EUR')));
}

Expand All @@ -73,10 +73,10 @@ public function it_throws_an_exception_with_error_response()
public function it_fetches_a_rate()
{
$pair = CurrencyPair::createFromString('EUR/CHF');
$uri = 'https://api.exchangeratesapi.io/latest?base=EUR';
$uri = 'https://api.exchangeratesapi.io/latest?base=EUR&access_key=x';
$content = file_get_contents(__DIR__.'/../../Fixtures/Service/ExchangeRatesApi/latest.json');

$service = new ExchangeRatesApi($this->getHttpAdapterMock($uri, $content));
$service = new ExchangeRatesApi($this->getHttpAdapterMock($uri, $content), null, ['access_key' => 'x']);
$rate = $service->getExchangeRate(new ExchangeRateQuery($pair));

$this->assertEquals(1.0933, $rate->getValue());
Expand All @@ -91,11 +91,11 @@ public function it_fetches_a_rate()
public function it_fetches_a_historical_rate()
{
$pair = CurrencyPair::createFromString('EUR/AUD');
$uri = 'https://api.exchangeratesapi.io/2000-01-03?base=EUR';
$uri = 'https://api.exchangeratesapi.io/2000-01-03?base=EUR&access_key=x';
$content = file_get_contents(__DIR__.'/../../Fixtures/Service/ExchangeRatesApi/historical.json');
$date = new \DateTime('2000-01-03');

$service = new ExchangeRatesApi($this->getHttpAdapterMock($uri, $content));
$service = new ExchangeRatesApi($this->getHttpAdapterMock($uri, $content), null, ['access_key' => 'x']);
$rate = $service->getExchangeRate(new HistoricalExchangeRateQuery($pair, $date));

$this->assertEquals(1.5209, $rate->getValue());
Expand All @@ -109,7 +109,7 @@ public function it_fetches_a_historical_rate()
*/
public function it_has_a_name()
{
$service = new ExchangeRatesApi($this->createMock('Http\Client\HttpClient'));
$service = new ExchangeRatesApi($this->createMock('Http\Client\HttpClient'), null, ['access_key' => 'x']);

$this->assertSame('exchange_rates_api', $service->getName());
}
Expand Down

0 comments on commit 5876184

Please sign in to comment.