1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Codenixsv\BittrexApi; |
6
|
|
|
|
7
|
|
|
use Codenixsv\BittrexApi\Api\Account; |
8
|
|
|
use Codenixsv\BittrexApi\Api\Market; |
9
|
|
|
use Codenixsv\BittrexApi\Api\PublicApi; |
10
|
|
|
use Codenixsv\BittrexApi\Exception\InvalidCredentialException; |
11
|
|
|
use Codenixsv\BittrexApi\Middleware\Authentication; |
12
|
|
|
use GuzzleHttp\Client; |
13
|
|
|
use GuzzleHttp\HandlerStack; |
14
|
|
|
|
15
|
|
|
class BittrexClient |
16
|
|
|
{ |
17
|
|
|
private const BASE_URI = 'https://api.bittrex.com'; |
18
|
|
|
|
19
|
|
|
/** @var Client */ |
20
|
|
|
private $publicClient; |
21
|
|
|
|
22
|
|
|
/** @var Client */ |
23
|
|
|
private $privateClient; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $key = ''; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
private $secret = ''; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $key |
33
|
|
|
* @param string $secret |
34
|
|
|
*/ |
35
|
3 |
|
public function setCredential(string $key, string $secret): void |
36
|
|
|
{ |
37
|
3 |
|
$this->key = $key; |
38
|
3 |
|
$this->secret = $secret; |
39
|
3 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
4 |
|
public function getKey(): string |
45
|
|
|
{ |
46
|
4 |
|
return $this->key; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
4 |
|
public function getSecret(): string |
53
|
|
|
{ |
54
|
4 |
|
return $this->secret; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return PublicApi |
59
|
|
|
*/ |
60
|
1 |
|
public function public(): PublicApi |
61
|
|
|
{ |
62
|
1 |
|
return new PublicApi($this->getPublicClient()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return Market |
67
|
|
|
* @throws InvalidCredentialException |
68
|
|
|
*/ |
69
|
2 |
|
public function market(): Market |
70
|
|
|
{ |
71
|
2 |
|
return new Market($this->getPrivateClient()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return Account |
76
|
|
|
* @throws InvalidCredentialException |
77
|
|
|
*/ |
78
|
2 |
|
public function account(): Account |
79
|
|
|
{ |
80
|
2 |
|
return new Account($this->getPrivateClient()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return Client |
85
|
|
|
*/ |
86
|
1 |
|
private function createPublicClient(): Client |
87
|
|
|
{ |
88
|
1 |
|
return new Client(['base_uri' => self::BASE_URI]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return Client |
93
|
|
|
* @throws InvalidCredentialException |
94
|
|
|
*/ |
95
|
4 |
|
private function createPrivateClient(): Client |
96
|
|
|
{ |
97
|
4 |
|
if (empty($this->key) || empty($this->secret)) { |
98
|
2 |
|
throw new InvalidCredentialException('Key and secret must be set for authenticated API'); |
99
|
|
|
} |
100
|
2 |
|
$stack = HandlerStack::create(); |
101
|
2 |
|
$stack->push(new Authentication($this->getKey(), $this->getSecret(), (string)time())); |
102
|
|
|
|
103
|
2 |
|
return new Client(['handler' => $stack, 'base_uri' => self::BASE_URI]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return Client |
108
|
|
|
* @throws InvalidCredentialException |
109
|
|
|
*/ |
110
|
4 |
|
private function getPrivateClient(): Client |
111
|
|
|
{ |
112
|
4 |
|
return $this->privateClient ?: $this->createPrivateClient(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return Client |
117
|
|
|
*/ |
118
|
1 |
|
private function getPublicClient(): Client |
119
|
|
|
{ |
120
|
1 |
|
return $this->publicClient ?: $this->createPublicClient(); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|