|
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 Client $publicClient; |
|
31
|
|
|
|
|
32
|
|
|
/** @var Client */ |
|
33
|
|
|
private Client $privateClient; |
|
34
|
|
|
|
|
35
|
|
|
/** @var string */ |
|
36
|
|
|
private string $key = ''; |
|
37
|
|
|
|
|
38
|
|
|
/** @var string */ |
|
39
|
|
|
private string $secret = ''; |
|
40
|
|
|
/** |
|
41
|
|
|
* @var Batch |
|
42
|
|
|
*/ |
|
43
|
|
|
private Batch $batchClient; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return PublicApi |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function public(): PublicApi |
|
49
|
|
|
{ |
|
50
|
1 |
|
return new PublicApi($this->getPublicClient()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return Client |
|
55
|
|
|
*/ |
|
56
|
1 |
|
private function getPublicClient(): Client |
|
57
|
|
|
{ |
|
58
|
1 |
|
return $this->publicClient ?? $this->createPublicClient(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return Client |
|
63
|
|
|
*/ |
|
64
|
1 |
|
private function createPublicClient(): Client |
|
65
|
|
|
{ |
|
66
|
1 |
|
$this->publicClient = new Client([ |
|
67
|
1 |
|
'headers' => self::CLIENT_HEADER, |
|
68
|
1 |
|
'base_uri' => self::BASE_URI |
|
69
|
|
|
]); |
|
70
|
|
|
|
|
71
|
1 |
|
return $this->publicClient; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $key |
|
76
|
|
|
* @param string $secret |
|
77
|
|
|
* @throws InvalidCredentialException |
|
78
|
|
|
*/ |
|
79
|
4 |
|
public function setCredential(string $key, string $secret): void |
|
80
|
|
|
{ |
|
81
|
|
|
|
|
82
|
4 |
|
if (!($this->isValidMd5($key) and $this->isValidMd5($secret))) |
|
83
|
1 |
|
throw new InvalidCredentialException('API Key and Secret have bad format'); |
|
84
|
|
|
|
|
85
|
3 |
|
$this->key = $key; |
|
86
|
3 |
|
$this->secret = $secret; |
|
87
|
3 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param string $md5 |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
4 |
|
public function isValidMd5(string $md5 = ''): bool |
|
94
|
|
|
{ |
|
95
|
4 |
|
return (preg_match('/^[a-f0-9]{32}$/', $md5) === 1); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return Batch |
|
100
|
|
|
* @throws InvalidCredentialException |
|
101
|
|
|
*/ |
|
102
|
|
|
public function batch(): Batch |
|
103
|
|
|
{ |
|
104
|
|
|
|
|
105
|
|
|
return $this->batchClient ?? $this->createBatch(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @throws InvalidCredentialException |
|
110
|
|
|
*/ |
|
111
|
|
|
private function createBatch(): Batch |
|
112
|
|
|
{ |
|
113
|
|
|
$this->batchClient = new Batch($this->getPrivateClient()); |
|
114
|
|
|
return $this->batchClient; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return Client |
|
119
|
|
|
* @throws InvalidCredentialException |
|
120
|
|
|
*/ |
|
121
|
4 |
|
private function getPrivateClient(): Client |
|
122
|
|
|
{ |
|
123
|
4 |
|
return $this->privateClient ?? $this->createPrivateClient(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return Client |
|
128
|
|
|
* @throws InvalidCredentialException |
|
129
|
|
|
*/ |
|
130
|
4 |
|
private function createPrivateClient(): Client |
|
131
|
|
|
{ |
|
132
|
4 |
|
if (!$this->haveValidCredentials()) |
|
133
|
2 |
|
throw new InvalidCredentialException('Key and secret must be set before call Private API'); |
|
134
|
|
|
|
|
135
|
2 |
|
$stack = HandlerStack::create(); |
|
136
|
2 |
|
$stack->push(new Authentication($this->key, $this->secret)); |
|
137
|
|
|
|
|
138
|
2 |
|
$this->privateClient = new Client([ |
|
139
|
2 |
|
'headers' => self::CLIENT_HEADER, |
|
140
|
2 |
|
'handler' => $stack, |
|
141
|
2 |
|
'base_uri' => self::BASE_URI |
|
142
|
|
|
]); |
|
143
|
|
|
|
|
144
|
2 |
|
return $this->privateClient; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return bool |
|
149
|
|
|
*/ |
|
150
|
5 |
|
public function haveValidCredentials(): bool |
|
151
|
|
|
{ |
|
152
|
5 |
|
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
|
2 |
|
public function market(): Market |
|
160
|
|
|
{ |
|
161
|
2 |
|
return new Market($this->getPrivateClient()); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return Account |
|
166
|
|
|
* @throws InvalidCredentialException |
|
167
|
|
|
*/ |
|
168
|
2 |
|
public function account(): Account |
|
169
|
|
|
{ |
|
170
|
2 |
|
return new Account($this->getPrivateClient()); |
|
171
|
|
|
} |
|
172
|
|
|
} |