1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Sarnado\Converter\API; |
5
|
|
|
|
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use Sarnado\Converter\Exceptions\BadRequestException; |
8
|
|
|
use Sarnado\Converter\Exceptions\ConverterServerErrorException; |
9
|
|
|
use Sarnado\Converter\Exceptions\ReachedRateLimitException; |
10
|
|
|
use Sarnado\Converter\Exceptions\UnauthorizedException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class APIClient |
14
|
|
|
* @package Sarnado\Converter\API |
15
|
|
|
*/ |
16
|
|
|
class APIClient |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
const BASE_URL = 'https://converter.sarnado.club'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
const API_VERSION = 'v1'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Client |
30
|
|
|
*/ |
31
|
|
|
private $httpClient; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* APIClient constructor. |
35
|
|
|
* @param string $apiToken |
36
|
|
|
*/ |
37
|
48 |
|
public function __construct(string $apiToken) |
38
|
|
|
{ |
39
|
48 |
|
$this->httpClient = new Client(['base_uri' => self::BASE_URL . '/' . $apiToken . '/' . self::API_VERSION . '/']); |
40
|
48 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return Client |
44
|
|
|
*/ |
45
|
3 |
|
public function getHttpClient(): Client |
46
|
|
|
{ |
47
|
3 |
|
return $this->httpClient; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param $response |
52
|
|
|
* @return APIResponse |
53
|
|
|
* @throws BadRequestException |
54
|
|
|
* @throws ConverterServerErrorException |
55
|
|
|
* @throws ReachedRateLimitException |
56
|
|
|
* @throws UnauthorizedException |
57
|
|
|
* @throws \Sarnado\Converter\Exceptions\BadAPIResponseException |
58
|
|
|
* @throws \Sarnado\Converter\Exceptions\DefaultAPIException |
59
|
|
|
*/ |
60
|
27 |
|
private function transformResponse($response): APIResponse |
61
|
|
|
{ |
62
|
27 |
|
return new APIResponse($response->getStatusCode(), $response->getBody()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return APIResponse |
67
|
|
|
* @throws BadRequestException |
68
|
|
|
* @throws ConverterServerErrorException |
69
|
|
|
* @throws ReachedRateLimitException |
70
|
|
|
* @throws UnauthorizedException |
71
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
72
|
|
|
* @throws \Sarnado\Converter\Exceptions\BadAPIResponseException |
73
|
|
|
* @throws \Sarnado\Converter\Exceptions\DefaultAPIException |
74
|
|
|
*/ |
75
|
6 |
|
public function getAPIUsage(): APIResponse |
76
|
|
|
{ |
77
|
6 |
|
$response = $this->httpClient->request('GET', 'usage'); |
78
|
3 |
|
return $this->transformResponse($response); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return APIResponse |
83
|
|
|
* @throws BadRequestException |
84
|
|
|
* @throws ConverterServerErrorException |
85
|
|
|
* @throws ReachedRateLimitException |
86
|
|
|
* @throws UnauthorizedException |
87
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
88
|
|
|
* @throws \Sarnado\Converter\Exceptions\BadAPIResponseException |
89
|
|
|
* @throws \Sarnado\Converter\Exceptions\DefaultAPIException |
90
|
|
|
*/ |
91
|
6 |
|
public function getCryptoExchanges(): APIResponse |
92
|
|
|
{ |
93
|
6 |
|
$response = $this->httpClient->request('GET', 'exchanges'); |
94
|
3 |
|
return $this->transformResponse($response); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param null $crypto |
|
|
|
|
99
|
|
|
* @param null $fiat |
|
|
|
|
100
|
|
|
* @param null $exchange |
|
|
|
|
101
|
|
|
* @return APIResponse |
102
|
|
|
* @throws BadRequestException |
103
|
|
|
* @throws ConverterServerErrorException |
104
|
|
|
* @throws ReachedRateLimitException |
105
|
|
|
* @throws UnauthorizedException |
106
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
107
|
|
|
* @throws \Sarnado\Converter\Exceptions\BadAPIResponseException |
108
|
|
|
* @throws \Sarnado\Converter\Exceptions\DefaultAPIException |
109
|
|
|
*/ |
110
|
12 |
|
public function getCryptoRates($crypto = null, $fiat = null, $exchange = null): APIResponse |
111
|
|
|
{ |
112
|
12 |
|
$response = $this->httpClient->request('GET', 'rates/crypto', [ |
113
|
|
|
'query' => [ |
114
|
12 |
|
'crypto' => $crypto, |
115
|
12 |
|
'fiat' => $fiat, |
116
|
12 |
|
'exchange' => $exchange |
117
|
|
|
] |
118
|
|
|
]); |
119
|
9 |
|
return $this->transformResponse($response); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param null $from |
|
|
|
|
124
|
|
|
* @param null $to |
|
|
|
|
125
|
|
|
* @return APIResponse |
126
|
|
|
* @throws BadRequestException |
127
|
|
|
* @throws ConverterServerErrorException |
128
|
|
|
* @throws ReachedRateLimitException |
129
|
|
|
* @throws UnauthorizedException |
130
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
131
|
|
|
* @throws \Sarnado\Converter\Exceptions\BadAPIResponseException |
132
|
|
|
* @throws \Sarnado\Converter\Exceptions\DefaultAPIException |
133
|
|
|
*/ |
134
|
9 |
|
public function getFiatRates($from = null, $to = null): APIResponse |
135
|
|
|
{ |
136
|
9 |
|
$response = $this->httpClient->request('GET', 'rates/fiat', [ |
137
|
|
|
'query' => [ |
138
|
9 |
|
'from' => $from, |
139
|
9 |
|
'to' => $to |
140
|
|
|
] |
141
|
|
|
]); |
142
|
6 |
|
return $this->transformResponse($response); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return APIResponse |
147
|
|
|
* @throws BadRequestException |
148
|
|
|
* @throws ConverterServerErrorException |
149
|
|
|
* @throws ReachedRateLimitException |
150
|
|
|
* @throws UnauthorizedException |
151
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
152
|
|
|
* @throws \Sarnado\Converter\Exceptions\BadAPIResponseException |
153
|
|
|
* @throws \Sarnado\Converter\Exceptions\DefaultAPIException |
154
|
|
|
*/ |
155
|
6 |
|
public function getCryptoCurrencies(): APIResponse |
156
|
|
|
{ |
157
|
6 |
|
$response = $this->httpClient->request('GET', 'currencies/crypto'); |
158
|
3 |
|
return $this->transformResponse($response); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return APIResponse |
163
|
|
|
* @throws BadRequestException |
164
|
|
|
* @throws ConverterServerErrorException |
165
|
|
|
* @throws ReachedRateLimitException |
166
|
|
|
* @throws UnauthorizedException |
167
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
168
|
|
|
* @throws \Sarnado\Converter\Exceptions\BadAPIResponseException |
169
|
|
|
* @throws \Sarnado\Converter\Exceptions\DefaultAPIException |
170
|
|
|
*/ |
171
|
6 |
|
public function getFiatCurrencies(): APIResponse |
172
|
|
|
{ |
173
|
6 |
|
$response = $this->httpClient->request('GET', 'currencies/fiat'); |
174
|
3 |
|
return $this->transformResponse($response); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|