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