1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace R3bers\BittrexApi; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use GuzzleHttp\HandlerStack; |
9
|
|
|
use R3bers\BittrexApi\Api\Account; |
10
|
|
|
use R3bers\BittrexApi\Api\Batch; |
11
|
|
|
use R3bers\BittrexApi\Api\Market; |
12
|
|
|
use R3bers\BittrexApi\Api\PublicApi; |
13
|
|
|
use R3bers\BittrexApi\Exception\InvalidCredentialException; |
14
|
|
|
use R3bers\BittrexApi\Middleware\Authentication; |
15
|
|
|
|
16
|
|
|
class BittrexClient |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Main URL to Bittrex Exchange |
20
|
|
|
*/ |
21
|
|
|
private const BASE_URI = 'https://api.bittrex.com'; |
22
|
|
|
|
23
|
|
|
private const CLIENT_HEADER = [ |
24
|
|
|
'User-Agent' => 'r3bers/bittrex-api/1.3.1', |
25
|
|
|
'Accept' => 'application/json', |
26
|
|
|
'Content-Type' => 'application/json' |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** @var Client */ |
30
|
|
|
private $publicClient; |
31
|
|
|
|
32
|
|
|
/** @var Client */ |
33
|
|
|
private $privateClient; |
34
|
|
|
|
35
|
|
|
/** @var string */ |
36
|
|
|
private $key = ''; |
37
|
|
|
|
38
|
|
|
/** @var string */ |
39
|
|
|
private $secret = ''; |
40
|
|
|
/** |
41
|
|
|
* @var Batch |
42
|
|
|
*/ |
43
|
1 |
|
private $batchClient; |
44
|
|
|
|
45
|
1 |
|
/** |
46
|
|
|
* @return PublicApi |
47
|
|
|
*/ |
48
|
|
|
public function public(): PublicApi |
49
|
|
|
{ |
50
|
|
|
return new PublicApi($this->getPublicClient()); |
51
|
1 |
|
} |
52
|
|
|
|
53
|
1 |
|
/** |
54
|
|
|
* @return Client |
55
|
|
|
*/ |
56
|
|
|
private function getPublicClient(): Client |
57
|
|
|
{ |
58
|
|
|
return $this->publicClient ?: $this->createPublicClient(); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
1 |
|
/** |
62
|
1 |
|
* @return Client |
63
|
1 |
|
*/ |
64
|
|
|
private function createPublicClient(): Client |
65
|
|
|
{ |
66
|
|
|
$this->publicClient = new Client([ |
67
|
|
|
'headers' => self::CLIENT_HEADER, |
68
|
|
|
'base_uri' => self::BASE_URI |
69
|
|
|
]); |
70
|
|
|
|
71
|
|
|
return $this->publicClient; |
72
|
4 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
4 |
|
* @param string $key |
76
|
1 |
|
* @param string $secret |
77
|
|
|
* @throws InvalidCredentialException |
78
|
3 |
|
*/ |
79
|
3 |
|
public function setCredential(string $key, string $secret): void |
80
|
3 |
|
{ |
81
|
|
|
|
82
|
|
|
if (!($this->isValidMd5($key) and $this->isValidMd5($secret))) |
83
|
|
|
throw new InvalidCredentialException('API Key and Secret have bad format'); |
84
|
|
|
|
85
|
|
|
$this->key = $key; |
86
|
4 |
|
$this->secret = $secret; |
87
|
|
|
} |
88
|
4 |
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $md5 |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function isValidMd5(string $md5 = ''): bool |
94
|
|
|
{ |
95
|
2 |
|
return (preg_match('/^[a-f0-9]{32}$/', $md5) === 1); |
96
|
|
|
} |
97
|
2 |
|
|
98
|
|
|
/** |
99
|
|
|
* @return Batch |
100
|
|
|
* @throws InvalidCredentialException |
101
|
|
|
*/ |
102
|
|
|
public function batch(): Batch |
103
|
|
|
{ |
104
|
4 |
|
|
105
|
|
|
return $this->batchClient ?: $this->createBatch(); |
106
|
4 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @throws InvalidCredentialException |
110
|
|
|
*/ |
111
|
|
|
private function createBatch(): Batch |
112
|
|
|
{ |
113
|
4 |
|
$this->batchClient = new Batch($this->getPrivateClient()); |
114
|
|
|
return $this->batchClient; |
115
|
4 |
|
} |
116
|
2 |
|
|
117
|
|
|
/** |
118
|
2 |
|
* @return Client |
119
|
2 |
|
* @throws InvalidCredentialException |
120
|
|
|
*/ |
121
|
2 |
|
private function getPrivateClient(): Client |
122
|
2 |
|
{ |
123
|
2 |
|
return $this->privateClient ?: $this->createPrivateClient(); |
124
|
2 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return Client |
128
|
|
|
* @throws InvalidCredentialException |
129
|
|
|
*/ |
130
|
|
|
private function createPrivateClient(): Client |
131
|
5 |
|
{ |
132
|
|
|
if (!$this->haveValidCredentials()) |
133
|
5 |
|
throw new InvalidCredentialException('Key and secret must be set before call Private API'); |
134
|
|
|
|
135
|
|
|
$stack = HandlerStack::create(); |
136
|
|
|
$stack->push(new Authentication($this->key, $this->secret)); |
137
|
|
|
|
138
|
|
|
$this->privateClient = new Client([ |
139
|
|
|
'headers' => self::CLIENT_HEADER, |
140
|
2 |
|
'handler' => $stack, |
141
|
|
|
'base_uri' => self::BASE_URI |
142
|
2 |
|
]); |
143
|
|
|
|
144
|
1 |
|
return $this->privateClient; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function haveValidCredentials(): bool |
151
|
|
|
{ |
152
|
|
|
return (!empty($this->key) and !empty($this->secret) and $this->isValidMd5($this->key) and $this->isValidMd5($this->secret)); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return Market |
157
|
|
|
* @throws InvalidCredentialException |
158
|
|
|
*/ |
159
|
|
|
public function market(): Market |
160
|
|
|
{ |
161
|
|
|
return new Market($this->getPrivateClient()); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return Account |
166
|
|
|
* @throws InvalidCredentialException |
167
|
|
|
*/ |
168
|
|
|
public function account(): Account |
169
|
|
|
{ |
170
|
|
|
return new Account($this->getPrivateClient()); |
171
|
|
|
} |
172
|
|
|
} |