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\Api; |
13
|
|
|
|
14
|
|
|
use GuzzleHttp\RequestOptions; |
15
|
|
|
use Poloniex\PoloniexClient; |
16
|
|
|
use Poloniex\Exception\PoloniexException; |
17
|
|
|
use Symfony\Component\Serializer\{Serializer, SerializerAwareInterface, SerializerInterface}; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class AbstractApi |
21
|
|
|
* |
22
|
|
|
* @author Grisha Chasovskih <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractApi implements ApiInterface, SerializerAwareInterface |
25
|
|
|
{ |
26
|
|
|
use Traits\ResponseFactoryTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Proxy address |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $proxy; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var PoloniexClient |
37
|
|
|
*/ |
38
|
|
|
protected $client; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $options = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* AbstractApi constructor. |
47
|
|
|
* |
48
|
|
|
* @param PoloniexClient $client |
49
|
|
|
* @param SerializerInterface|Serializer $serializer |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
PoloniexClient $client, |
53
|
|
|
SerializerInterface $serializer |
54
|
|
|
) { |
55
|
|
|
$this->setSerializer($serializer); |
56
|
|
|
$this->client = $client; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set proxy |
61
|
|
|
* |
62
|
|
|
* @param string $proxy |
63
|
|
|
*/ |
64
|
|
|
public function setProxy(string $proxy): void |
65
|
|
|
{ |
66
|
|
|
$this->proxy = $proxy; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Call request |
71
|
|
|
* |
72
|
|
|
* @param string $command |
73
|
|
|
* @param array $params |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
* @throws PoloniexException |
77
|
|
|
*/ |
78
|
|
|
public function request(string $command, array $params = []): array |
79
|
|
|
{ |
80
|
|
|
if (isset($params['currencyPair']) && $params['currencyPair'] !== 'all') { |
81
|
|
|
$this->checkPair($params['currencyPair'], $command); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($this->proxy !== null) { |
85
|
|
|
$this->options[RequestOptions::PROXY] = $this->proxy; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$contents = $this->client |
89
|
|
|
->request( |
90
|
|
|
$this->getRequestMethod(), |
91
|
|
|
$this->getRequestUri(), |
92
|
|
|
$this->options |
93
|
|
|
) |
94
|
|
|
->getBody() |
95
|
|
|
->getContents(); |
96
|
|
|
|
97
|
|
|
$this->proxy = null; |
98
|
|
|
$response = json_decode($contents ?: '{}', true) ?: []; |
99
|
|
|
$this->throwExceptionIf(isset($response['error']), $response['error'] ?? 'Poloniex API unknown error.'); |
100
|
|
|
|
101
|
|
|
return $response; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Check currency pair |
106
|
|
|
* |
107
|
|
|
* @param string $currencyPair |
108
|
|
|
* @param string $command |
109
|
|
|
*/ |
110
|
|
|
final protected function checkPair(string $currencyPair, string $command) |
111
|
|
|
{ |
112
|
|
|
$pair = explode('_', $currencyPair); |
113
|
|
|
|
114
|
|
|
$this->throwExceptionIf( |
115
|
|
|
$pair[0] === $pair[1], |
116
|
|
|
sprintf('Unable to call "%s" with currency pair %s.', $command, $currencyPair) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get request method |
122
|
|
|
* |
123
|
|
|
* @return string GET or POST |
124
|
|
|
*/ |
125
|
|
|
abstract protected function getRequestMethod(): string; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get request type |
129
|
|
|
* |
130
|
|
|
* @return string 'tradingApi' or 'public' |
131
|
|
|
*/ |
132
|
|
|
abstract protected function getRequestUri(): string; |
133
|
|
|
} |