Repository navigation
Expand file tree
/
Copy pathclass-client.php
More file actions
286 lines (233 loc) · 6.83 KB
/
Copy pathclass-client.php
File metadata and controls
286 lines (233 loc) · 6.83 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
* Client class
*
* @package Cointokio\CoinGecko
*/
namespace Cointokio\CoinGecko;
/**
* The Client class exists for easier access to all individual request endpoint class methods.
*
* The constructor takes an API key and, optionally, an API endpoint. Callers
* must pair Pro keys with `https://pro-api.coingecko.com/api/v3` explicitly;
* demo (free) keys use the default `https://api.coingecko.com/api/v3` host.
* Each accessor forwards both values to the corresponding endpoint class so the
* correct auth header (`x-cg-pro-api-key` vs `x-cg-demo-api-key`) is picked
* automatically based on the configured host.
*
* Example — demo key (default host):
*
* $coingecko_client = new \Cointokio\CoinGecko\Client( 'your-demo-api-key' );
* $response = $coingecko_client->coins()->get_list();
*
* Example — pro key (paid host):
*
* $coingecko_client = new \Cointokio\CoinGecko\Client(
* 'your-pro-api-key',
* 'https://pro-api.coingecko.com/api/v3'
* );
* $response = $coingecko_client->coins()->get_list();
*
* Both values can also be supplied via the `COINGECKO_API_KEY` and
* `COINGECKO_API_ENDPOINT` constants, in which case `new Client()` with no
* arguments works.
*/
class Client {
/**
* CoinGecko API base URL.
*
* @var string
*/
private $endpoint = '';
/**
* CoinGecko API key.
*
* @var string
*/
private $api_key = '';
/**
* Initialize Client class.
*
* @param string $api_key CoinGecko API key. As an alternative to passing
* this parameter you can define the
* COINGECKO_API_KEY constant.
* @param string $endpoint CoinGecko API endpoint. Defaults to
* https://api.coingecko.com/api/v3 (Demo). Pass
* https://pro-api.coingecko.com/api/v3 for Pro
* keys. As an alternative to passing this
* parameter you can define the
* COINGECKO_API_ENDPOINT constant.
*/
public function __construct( $api_key = '', $endpoint = '' ) {
$this->api_key = $api_key;
$this->endpoint = $endpoint;
if ( ! class_exists( '\Cointokio\CoinGecko\Request' ) ) {
require_once dirname( __FILE__ ) . '/classes/class-request.php';
}
}
/**
* Get an instance of the AssetPlatforms class.
*
* @return AssetPlatforms
*/
public function asset_platforms() {
if ( ! class_exists( '\Cointokio\CoinGecko\AssetPlatforms' ) ) {
require_once dirname( __FILE__ ) . '/classes/class-assetplatforms.php';
}
return new AssetPlatforms( $this->api_key, $this->endpoint );
}
/**
* Get an instance of the Categories class.
*
* @return Categories
*/
public function categories() {
if ( ! class_exists( '\Cointokio\CoinGecko\Categories' ) ) {
require_once dirname( __FILE__ ) . '/classes/class-categories.php';
}
return new Categories( $this->api_key, $this->endpoint );
}
/**
* Get an instance of the Coins class.
*
* @return Coins
*/
public function coins() {
if ( ! class_exists( '\Cointokio\CoinGecko\Coins' ) ) {
require_once dirname( __FILE__ ) . '/classes/class-coins.php';
}
return new Coins( $this->api_key, $this->endpoint );
}
/**
* Get an instance of the Companies class.
*
* @return Companies
*/
public function companies() {
if ( ! class_exists( '\Cointokio\CoinGecko\Companies' ) ) {
require_once dirname( __FILE__ ) . '/classes/class-companies.php';
}
return new Companies( $this->api_key, $this->endpoint );
}
/**
* Get an instance of the Contract class.
*
* @return Contract
*/
public function contract() {
if ( ! class_exists( '\Cointokio\CoinGecko\Contract' ) ) {
require_once dirname( __FILE__ ) . '/classes/class-contract.php';
}
return new Contract( $this->api_key, $this->endpoint );
}
/**
* Get an instance of the Derivatives class.
*
* @return Derivatives
*/
public function derivatives() {
if ( ! class_exists( '\Cointokio\CoinGecko\Derivatives' ) ) {
require_once dirname( __FILE__ ) . '/classes/class-derivatives.php';
}
return new Derivatives( $this->api_key, $this->endpoint );
}
/**
* Get an instance of the Exchanges class.
*
* @return Exchanges
*/
public function exchanges() {
if ( ! class_exists( '\Cointokio\CoinGecko\Exchanges' ) ) {
require_once dirname( __FILE__ ) . '/classes/class-exchanges.php';
}
return new Exchanges( $this->api_key, $this->endpoint );
}
/**
* Get an instance of the ExchangeRates class.
*
* @return ExchangeRates
*/
public function exchange_rates() {
if ( ! class_exists( '\Cointokio\CoinGecko\ExchangeRates' ) ) {
require_once dirname( __FILE__ ) . '/classes/class-exchangerates.php';
}
return new ExchangeRates( $this->api_key, $this->endpoint );
}
/**
* Get an instance of the Finance class.
*
* @return Finance
*/
public function finance() {
if ( ! class_exists( '\Cointokio\CoinGecko\Finance' ) ) {
require_once dirname( __FILE__ ) . '/classes/class-finance.php';
}
return new Finance( $this->api_key, $this->endpoint );
}
/**
* Get an instance of the Globals class.
*
* @return Globals
*/
public function globals() {
if ( ! class_exists( '\Cointokio\CoinGecko\Globals' ) ) {
require_once dirname( __FILE__ ) . '/classes/class-globals.php';
}
return new Globals( $this->api_key, $this->endpoint );
}
/**
* Get an instance of the Indexes class.
*
* @return Indexes
*/
public function indexes() {
if ( ! class_exists( '\Cointokio\CoinGecko\Indexes' ) ) {
require_once dirname( __FILE__ ) . '/classes/class-indexes.php';
}
return new Indexes( $this->api_key, $this->endpoint );
}
/**
* Check API server status.
*
* @return array|\WP_Error
*/
public function ping() {
if ( ! class_exists( '\Cointokio\CoinGecko\Ping' ) ) {
require_once dirname( __FILE__ ) . '/classes/class-ping.php';
}
return ( new Ping( $this->api_key, $this->endpoint ) )->get_ping();
}
/**
* Get an instance of the Simple class.
*
* @return Simple
*/
public function simple() {
if ( ! class_exists( '\Cointokio\CoinGecko\Simple' ) ) {
require_once dirname( __FILE__ ) . '/classes/class-simple.php';
}
return new Simple( $this->api_key, $this->endpoint );
}
/**
* Get an instance of the StatusUpdates class.
*
* @return StatusUpdates
*/
public function status_updates() {
if ( ! class_exists( '\Cointokio\CoinGecko\StatusUpdates' ) ) {
require_once dirname( __FILE__ ) . '/classes/class-statusupdates.php';
}
return new StatusUpdates( $this->api_key, $this->endpoint );
}
/**
* Get an instance of the Trending class.
*
* @return Trending
*/
public function trending() {
if ( ! class_exists( '\Cointokio\CoinGecko\Trending' ) ) {
require_once dirname( __FILE__ ) . '/classes/class-trending.php';
}
return new Trending( $this->api_key, $this->endpoint );
}
}