Skip to content

Commit

Permalink
fix: conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
florianv committed May 15, 2021
2 parents 4c64fea + 082894c commit 10265d5
Show file tree
Hide file tree
Showing 14 changed files with 279 additions and 113 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Here is the complete list of the currently implemented services:
|---------------------------------------------------------------------------|----------------------|----------------|----------------|
| [Fixer](https://fixer.io) | EUR (free, no SSL), * (paid) | * | Yes |
| [currencylayer](https://currencylayer.com) | USD (free), * (paid) | * | Yes |
| [exchangeratesapi](https://exchangeratesapi.io) | USD (free), * (paid) | * | Yes |
| [exchangeratesapi](https://exchangeratesapi.io) | EUR (free), * (paid) | * | Yes |
| [Abstract](https://www.abstractapi.com) | * | * | No |
| [coinlayer](https://coinlayer.com) | * Crypto (Limited standard currencies) | * Crypto (Limited standard currencies) | Yes |
| [European Central Bank](https://www.ecb.europa.eu/home/html/index.en.html) | EUR | * | Yes |
Expand Down
6 changes: 3 additions & 3 deletions doc/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ $service = new Fixer($client, null, ['access_key' => 'YOUR_KEY']);
$service = new CurrencyLayer($client, null, ['access_key' => 'access_key', 'enterprise' => false]);

// Or use the exchangeratesapi.io service
$service = new ExchangeRatesApi($client, null, ['access_key' => 'access_key']);
$service = new ExchangeRatesApi($client, null, ['access_key' => 'access_key', 'enterprise' => false]);

// Or use the abstractapi.com service
$service = new AbstractApi($client, null, ['api_key' => 'api_key']);
Expand Down Expand Up @@ -148,7 +148,7 @@ use Exchanger\Service\AbstractApi;
$service = new Chain([
new Fixer($client, null, ['access_key' => 'YOUR_KEY']),
new CurrencyLayer($client, null, ['access_key' => 'access_key', 'enterprise' => false]),
new ExchangeRatesApi($client, null, ['access_key' => 'access_key']),
new ExchangeRatesApi($client, null, ['access_key' => 'access_key', 'enterprise' => false]),
new AbstractApi($client, null, ['api_key' => 'api_key'])
]);
```
Expand Down Expand Up @@ -415,7 +415,7 @@ use Exchanger\Service\AbstractApi;
$service = new Chain([
new Fixer($client, null, ['access_key' => 'YOUR_KEY']),
new CurrencyLayer($client, null, ['access_key' => 'access_key', 'enterprise' => false]),
new ExchangeRatesApi($client, null, ['access_key' => 'access_key']),
new ExchangeRatesApi($client, null, ['access_key' => 'access_key', 'enterprise' => false]),
new AbstractApi($client, null, ['api_key' => 'api_key']),
new CoinLayer($client, null, ['access_key' => 'access_key', 'paid' => false]),
new EuropeanCentralBank(),
Expand Down
81 changes: 60 additions & 21 deletions src/Service/ExchangeRatesApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,21 @@
/**
* ExchangeRatesApi Service.
*
* @see https://exchangeratesapi.io/documentation/
*
* @author Arjan Westdorp <[email protected]>
*/
final class ExchangeRatesApi extends HttpService
{
use SupportsHistoricalQueries;

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

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

const FREE_LATEST_URL = 'http://api.exchangeratesapi.io/latest?access_key=%s&symbols=%s';

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

const ACCESS_KEY_OPTION = 'access_key';

Expand All @@ -46,6 +52,10 @@ public function processOptions(array &$options): void
if (!isset($options[self::ACCESS_KEY_OPTION])) {
throw new NonBreakingInvalidArgumentException('The "access_key" option must be provided to use exchangeratesapi.io');
}

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

/**
Expand All @@ -54,12 +64,20 @@ public function processOptions(array &$options): void
protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery): ExchangeRateContract
{
$currencyPair = $exchangeQuery->getCurrencyPair();

$url = sprintf(
self::LATEST_URL,
$currencyPair->getBaseCurrency(),
$this->options[self::ACCESS_KEY_OPTION]
);
if ($this->options['enterprise']) {
$url = sprintf(
self::LATEST_URL,
$currencyPair->getBaseCurrency(),
$this->options[self::ACCESS_KEY_OPTION],
$currencyPair->getQuoteCurrency()
);
} else {
$url = sprintf(
self::FREE_LATEST_URL,
$this->options[self::ACCESS_KEY_OPTION],
$currencyPair->getQuoteCurrency()
);
}

return $this->doCreateRate($url, $currencyPair);
}
Expand All @@ -70,13 +88,22 @@ protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery): Exch
protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchangeQuery): ExchangeRateContract
{
$currencyPair = $exchangeQuery->getCurrencyPair();

$url = sprintf(
self::HISTORICAL_URL,
$exchangeQuery->getDate()->format('Y-m-d'),
$exchangeQuery->getCurrencyPair()->getBaseCurrency(),
$this->options[self::ACCESS_KEY_OPTION]
);
if ($this->options['enterprise']) {
$url = sprintf(
self::HISTORICAL_URL,
$exchangeQuery->getDate()->format('Y-m-d'),
$exchangeQuery->getCurrencyPair()->getBaseCurrency(),
$this->options[self::ACCESS_KEY_OPTION],
$currencyPair->getQuoteCurrency()
);
} else {
$url = sprintf(
self::FREE_HISTORICAL_URL,
$exchangeQuery->getDate()->format('Y-m-d'),
$this->options[self::ACCESS_KEY_OPTION],
$exchangeQuery->getCurrencyPair()->getQuoteCurrency()
);
}

return $this->doCreateRate($url, $currencyPair);
}
Expand All @@ -86,16 +113,13 @@ protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchan
*/
public function supportQuery(ExchangeRateQuery $exchangeQuery): bool
{
return true;
return $this->options['enterprise'] || 'EUR' === $exchangeQuery->getCurrencyPair()->getBaseCurrency();
}

/**
* Creates a rate.
*
* @param string $url
* @param CurrencyPair $currencyPair
*
* @return ExchangeRate
* @param string $url
*
* @throws Exception
*/
Expand All @@ -105,7 +129,22 @@ private function doCreateRate($url, CurrencyPair $currencyPair): ExchangeRate
$data = StringUtil::jsonToArray($content);

if (isset($data['error'])) {
throw new Exception($data['error']);
if (isset($data['error']['code'])) {
if (\in_array($data['error']['code'], [
'invalid_currency_codes',
'invalid_base_currency',
'no_rates_available',
], true)) {
throw new UnsupportedCurrencyPairException($currencyPair, $this);
}
if (isset($data['error']['message'])) {
throw new Exception($data['error']['message']);
} else {
throw new Exception('Service return error code: '.$data['error']['code']);
}
} else {
throw new Exception('Service return unhandled error');
}
}

if (isset($data['rates'][$currencyPair->getQuoteCurrency()])) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"error":{
"code":"base_currency_access_restricted",
"message":"An unexpected error ocurred. [Technical Support: [email protected]]"
}
}
1 change: 0 additions & 1 deletion tests/Fixtures/Service/ExchangeRatesApi/error.json

This file was deleted.

39 changes: 8 additions & 31 deletions tests/Fixtures/Service/ExchangeRatesApi/historical.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
{
"base": "USD",
"date": "2000-01-03",
"rates": {
"AUD": 1.5209,
"CAD": 1.4447,
"CHF": 1.59,
"CYP": 0.57156,
"CZK": 35.741,
"DKK": 7.374,
"EEK": 15.507,
"GBP": 0.61903,
"HKD": 7.7923,
"HUF": 252.26,
"ISK": 72.379,
"JPY": 101.83,
"KRW": 1129.9,
"LTL": 4.0093,
"LVL": 0.58632,
"MTL": 0.4114,
"NOK": 7.9901,
"NZD": 1.9159,
"PLN": 4.1462,
"ROL": 18110.0,
"SEK": 8.4757,
"SGD": 1.6619,
"SIT": 197.12,
"SKK": 41.94,
"TRL": 541260.0,
"ZAR": 6.146,
"EUR": 0.99108
}
"success":true,
"timestamp":1618531199,
"historical":true,
"base":"EUR",
"date":"2021-04-15",
"rates":{
"USD":1.196953
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"error":{
"code":"https_access_restricted",
"message":"Access Restricted - Your current Subscription Plan does not support HTTPS Encryption."
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"error":{
"code":"invalid_access_key",
"message":"You have not supplied a valid API Access Key. [Technical Support: [email protected]]"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"error":{
"code":"invalid_base_currency",
"message":"An unexpected error ocurred. [Technical Support: [email protected]]"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"error":{
"code":"invalid_currency_codes",
"message":"You have provided one or more invalid Currency Codes. [Required format: currencies=EUR,USD,GBP,...]"
}
}
6 changes: 6 additions & 0 deletions tests/Fixtures/Service/ExchangeRatesApi/invalid_date.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"error":{
"code":"invalid_date",
"message":"You have entered an invalid date. [Required format: date=YYYY-MM-DD]"
}
}
42 changes: 7 additions & 35 deletions tests/Fixtures/Service/ExchangeRatesApi/latest.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,9 @@
{
"base": "EUR",
"date": "2016-08-26",
"rates": {
"AUD": 1.4771,
"BGN": 1.9558,
"BRL": 3.6441,
"CAD": 1.4546,
"CHF": 1.0933,
"CNY": 7.5318,
"CZK": 27.024,
"DKK": 7.4462,
"GBP": 0.8545,
"HKD": 8.7555,
"HRK": 7.4893,
"HUF": 308.5,
"IDR": 14906.0,
"ILS": 4.2444,
"INR": 75.647,
"JPY": 113.33,
"KRW": 1258.02,
"MXN": 20.7197,
"MYR": 4.5349,
"NOK": 9.2501,
"NZD": 1.5418,
"PHP": 52.29,
"PLN": 4.3244,
"RON": 4.4578,
"RUB": 73.1392,
"SEK": 9.4903,
"SGD": 1.5269,
"THB": 39.004,
"TRY": 3.316,
"USD": 1.129,
"ZAR": 15.8622
}
"success":true,
"timestamp":1619171643,
"base":"EUR",
"date":"2021-04-23",
"rates":{
"USD":1.20555
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"error":{
"code":"no_rates_available",
"message":"Your query did not return any results. Please try again."
}
}
Loading

0 comments on commit 10265d5

Please sign in to comment.