-
Notifications
You must be signed in to change notification settings - Fork 10
/
CurrencyFreaksApi.php
91 lines (82 loc) · 2.18 KB
/
CurrencyFreaksApi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* @link https://github.com/imanilchaudhari
* @copyright Copyright (c) 2024
* @license [MIT License](https://opensource.org/license/mit)
*/
namespace imanilchaudhari\CurrencyConverter\Provider;
use yii\httpclient\Client;
use yii\base\InvalidConfigException;
use imanilchaudhari\CurrencyConverter\Interface\RateProviderInterface;
/**
* CurrencyFreaks provides currency conversion, current and historical forex exchange rate
* and currency fluctuation data through REST API in json and xml formats compatible.
*
* To use CurrencyFreaksApi, configure your app component as below
*
* ```php
*
* 'components' => [
* 'currencyConverter' => [
* 'class' => 'imanilchaudhari\CurrencyConverter\CurrencyConverter',
* 'provider' => [
* 'class' => 'imanilchaudhari\CurrencyConverter\Provider\CurrencyFreaksApi',
* 'apiKey' => 'your-api-key',
* ],
* ],
* ],
* ```
*
* @see https://currencyfreaks.com
*
* @author Anil Chaudhari <[email protected]>
* @since 1.0
*/
class CurrencyFreaksApi implements RateProviderInterface
{
/**
* The Currency Freaks API KEY
*
* @var string
*/
public $apiKey;
/**
* Yii http client
*
* @var Client
*/
private $_client;
/**
* Create a new provider instance.
*
* @param string $apiKey
* @return void
*/
public function __construct($apiKey)
{
$this->apiKey = $apiKey;
$this->_client = new Client([
'baseUrl' => 'https://api.currencyfreaks.com',
'transport' => 'yii\httpclient\CurlTransport',
]);
}
/**
* {@inheritDoc}
*/
public function getRate($source, $target)
{
try {
$response = $this->_client->get('/v2.0/rates/latest', [
'apikey' => $this->apiKey,
'base' => $source,
])->send();
$content = $response->getData();
if ($response->isOk) {
return $content['rates'][$target];
}
throw new InvalidConfigException($content['message']);
} catch (\Exception $ex) {
throw $ex;
}
}
}