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; |
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
|
|
|
* Base url for poloniex api requests |
31
|
|
|
*/ |
32
|
|
|
public const BASE_URI = 'https://poloniex.com/'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var CallHistoryInterface |
36
|
|
|
*/ |
37
|
|
|
private $callHistory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Timeout in seconds |
41
|
|
|
* |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
private $timeout; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* PoloniexClient constructor. |
48
|
|
|
* |
49
|
|
|
* @param CallHistoryInterface $callHistory |
50
|
|
|
* @param string $baseUri |
51
|
|
|
* @param int $timeout |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
CallHistoryInterface $callHistory = null, |
55
|
|
|
string $baseUri = self::BASE_URI, |
56
|
|
|
int $timeout = 0 |
57
|
|
|
) { |
58
|
|
|
$this->callHistory = $callHistory; |
59
|
|
|
$this->timeout = $timeout; |
60
|
|
|
|
61
|
|
|
parent::__construct([ |
62
|
|
|
'base_uri' => $baseUri, |
63
|
|
|
'allow_redirects' => false, |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function request($method, $uri = '', array $options = []): ResponseInterface |
71
|
|
|
{ |
72
|
|
|
if ($this->callHistory !== null) { |
73
|
|
|
if ($this->callHistory->isIncreased()) { |
74
|
|
|
sleep(1); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->callHistory->create(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return parent::request($method, $uri, array_merge(['timeout' => $this->timeout], $options)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Check whether service is accessible |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
public function isInaccessible(): bool |
89
|
|
|
{ |
90
|
|
|
return $this->get('/')->getStatusCode() !== 200; |
91
|
|
|
} |
92
|
|
|
} |