|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Poloniex PHP SDK. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright 2017-2018 Chasovskih Grisha <[email protected]> |
|
9
|
|
|
* @license https://github.com/signulls/poloniex-php-sdk/blob/master/LICENSE MIT |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Poloniex; |
|
13
|
|
|
|
|
14
|
|
|
use GuzzleHttp\{Client, RequestOptions}; |
|
15
|
|
|
use Poloniex\CallHistory\CallHistoryInterface; |
|
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class PoloniexClient |
|
20
|
|
|
* |
|
21
|
|
|
* NOTE: Currency pairs are reverse of what most exchanges use. |
|
22
|
|
|
* For instance, instead of XPM_BTC, use BTC_XPM |
|
23
|
|
|
* |
|
24
|
|
|
* @link https://poloniex.com/support/api/ |
|
25
|
|
|
* @author Grisha Chasovskih <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class PoloniexClient extends Client |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var CallHistoryInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $callHistory; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Timeout in seconds |
|
36
|
|
|
* |
|
37
|
|
|
* @var int |
|
38
|
|
|
*/ |
|
39
|
|
|
private $timeout; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* PoloniexClient constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param CallHistoryInterface $callHistory |
|
45
|
|
|
* @param string $baseUri |
|
46
|
|
|
* @param int $timeout |
|
47
|
|
|
* @param string|null $proxy |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct( |
|
50
|
|
|
CallHistoryInterface $callHistory, |
|
51
|
|
|
string $baseUri = 'https://poloniex.com/', |
|
52
|
|
|
int $timeout = 0, |
|
53
|
|
|
string $proxy = null |
|
54
|
|
|
) { |
|
55
|
|
|
$this->callHistory = $callHistory; |
|
56
|
|
|
$this->timeout = $timeout; |
|
57
|
|
|
|
|
58
|
|
|
parent::__construct([ |
|
59
|
|
|
'base_uri' => $baseUri, |
|
60
|
|
|
RequestOptions::ALLOW_REDIRECTS => false, |
|
61
|
|
|
RequestOptions::PROXY => $proxy, |
|
62
|
|
|
]); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function request($method, $uri = '', array $options = []): ResponseInterface |
|
69
|
|
|
{ |
|
70
|
|
|
$wait = false; |
|
71
|
|
|
$proxy = $options[RequestOptions::PROXY] ?? $this->getConfig(RequestOptions::PROXY) ?: null; |
|
72
|
|
|
|
|
73
|
|
|
do { |
|
74
|
|
|
if ($wait) { |
|
75
|
|
|
sleep(1); |
|
76
|
|
|
} |
|
77
|
|
|
$wait = true; |
|
78
|
|
|
} while ($this->callHistory->isIncreased($proxy)); |
|
79
|
|
|
|
|
80
|
|
|
$this->callHistory->create($proxy); |
|
81
|
|
|
|
|
82
|
|
|
return parent::request($method, $uri, array_merge(['timeout' => $this->timeout], $options)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Check whether service is accessible |
|
87
|
|
|
* |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
public function isInaccessible(): bool |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->get('/')->getStatusCode() !== 200; |
|
93
|
|
|
} |
|
94
|
|
|
} |