Skip to content

Commit 18ef23c

Browse files
authored
Merge pull request #1 from toooni/master
guzzle 6
2 parents 484a893 + ba5d336 commit 18ef23c

File tree

5 files changed

+79
-65
lines changed

5 files changed

+79
-65
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
],
1212
"require": {
13-
"guzzlehttp/guzzle": "5.x"
13+
"guzzlehttp/guzzle": "6.x"
1414
},
1515
"autoload": {
1616
"psr-0": {"Hitbtc": "src/"}

src/Hitbtc/AuthMiddleware.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Hitbtc;
4+
5+
6+
use Psr\Http\Message\RequestInterface;
7+
8+
class AuthMiddleware
9+
{
10+
protected $publicKey;
11+
protected $secretKey;
12+
13+
public function __construct($publicKey, $secretKey)
14+
{
15+
$this->publicKey = $publicKey;
16+
$this->secretKey = $secretKey;
17+
}
18+
19+
/**
20+
* @param callable $handler
21+
*
22+
* @return callable
23+
*/
24+
public function __invoke(callable $handler)
25+
{
26+
return function (RequestInterface $request, array $options) use (&$handler) {
27+
$queryString = $request->getUri()->getQuery();
28+
$queryParts = \GuzzleHttp\Psr7\parse_query($queryString);
29+
30+
$queryParts['apikey'] = $this->publicKey;
31+
$queryParts['nonce'] = $this->getNonce();
32+
33+
$queryString = \GuzzleHttp\Psr7\build_query($queryParts);
34+
$request = $request->withUri($request->getUri()->withQuery($queryString));
35+
36+
$message = $request->getUri()->getPath(). '?' . $queryString . $request->getBody();
37+
$sign = strtolower(hash_hmac('sha512', $message, $this->secretKey));
38+
39+
$request = $request
40+
->withAddedHeader('Api-Signature', $sign)
41+
->withAddedHeader('User-Agent', 'Hitbtc PHP Client')
42+
;
43+
44+
return $handler($request, $options);
45+
};
46+
}
47+
48+
protected function getNonce()
49+
{
50+
return intval(microtime(true) * 1000);
51+
}
52+
}

src/Hitbtc/AuthSubscriber.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/Hitbtc/ProtectedClient.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Hitbtc;
44

55
use GuzzleHttp\Client as HttpClient;
6+
use GuzzleHttp\Handler\CurlHandler;
7+
use GuzzleHttp\HandlerStack;
68
use GuzzleHttp\Message\Response;
79
use Hitbtc\Exception\InvalidRequestException;
810
use Hitbtc\Exception\RejectException;
@@ -45,11 +47,13 @@ public function __construct($publicKey, $secretKey, $demo = false)
4547
public function getHttpClient()
4648
{
4749
if (!$this->httpClient) {
50+
$stack = HandlerStack::create();
51+
$stack->push(new AuthMiddleware($this->publicKey, $this->secretKey));
52+
4853
$this->httpClient = new HttpClient([
49-
'base_url' => $this->host,
54+
'handler' => $stack,
55+
'base_uri' => $this->host,
5056
]);
51-
$this->httpClient->getEmitter()->attach(new AuthSubscriber($this->publicKey, $this->secretKey));
52-
5357
}
5458

5559
return $this->httpClient;
@@ -69,7 +73,7 @@ public function newOrder(NewOrder $order)
6973
'body' => $order->asArray(),
7074
'exceptions' => false,
7175
));
72-
$document = $response->json();
76+
$document = json_decode($response->getBody(), true);
7377

7478
if (isset($document['ExecutionReport'])) {
7579
if ($document['ExecutionReport']['execReportType'] == 'rejected') {
@@ -105,7 +109,7 @@ public function cancelOrder(Order $order, $cancelRequestId = null)
105109
),
106110
'exceptions' => false,
107111
));
108-
$document = $response->json();
112+
$document = json_decode($response->getBody(), true);
109113
if (isset($document['ExecutionReport'])) {
110114
return new Order($document['ExecutionReport']);
111115
} elseif (isset($document['CancelReject'])) {
@@ -127,7 +131,7 @@ public function getActiveOrders($symbols = null)
127131
$params['query']['symbols'] = implode(',', (array) $symbols);
128132
}
129133
$response = $this->getHttpClient()->get('/api/1/trading/orders/active', $params);
130-
$document = $response->json();
134+
$document = json_decode($response->getBody(), true);
131135
if (isset($document['orders'])) {
132136
$orders = [];
133137
foreach ($document['orders'] as $orderData) {
@@ -164,7 +168,7 @@ public function getRecentOrders($symbols = null, $sort = 'asc', $statuses = null
164168
}
165169

166170
$response = $this->getHttpClient()->get('/api/1/trading/orders/recent', array('query' => $query, 'exceptions' => false));
167-
$document = $response->json();
171+
$document = json_decode($response->getBody(), true);
168172
if (isset($document['orders'])) {
169173
$orders = [];
170174
foreach ($document['orders'] as $orderData) {
@@ -208,7 +212,7 @@ public function getTrades($symbols = null, $by = 'trade_id', $sort = 'ask', $fro
208212
}
209213

210214
$response = $this->getHttpClient()->get('/api/1/trading/trades', array('query' => $query, 'exceptions' => false));
211-
$document = $response->json();
215+
$document = json_decode($response->getBody(), true);
212216
if (isset($document['trades'])) {
213217
$trades = [];
214218
foreach ($document['trades'] as $tradeData) {
@@ -232,7 +236,7 @@ public function getTradesByOrder($clientOrderId)
232236
);
233237

234238
$response = $this->getHttpClient()->get('/api/1/trading/trades/by/order', array('query' => $query, 'exceptions' => false));
235-
$document = $response->json();
239+
$document = json_decode($response->getBody(), true);
236240
if (isset($document['trades'])) {
237241
$trades = [];
238242
foreach ($document['trades'] as $tradeData) {
@@ -251,7 +255,7 @@ public function getTradesByOrder($clientOrderId)
251255
public function getBalanceTrading()
252256
{
253257
$response = $this->getHttpClient()->get('/api/1/trading/balance', array('exceptions' => false));
254-
$document = $response->json();
258+
$document = json_decode($response->getBody(), true);
255259
if (isset($document['balance'])) {
256260
$balances = [];
257261
foreach ($document['balance'] as $balanceData) {
@@ -270,7 +274,7 @@ public function getBalanceTrading()
270274
public function getBalanceMain()
271275
{
272276
$response = $this->getHttpClient()->get('/api/1/payment/balance', array('exceptions' => false));
273-
$document = $response->json();
277+
$document = json_decode($response->getBody(), true);
274278
if (isset($document['balance'])) {
275279
$balances = [];
276280
foreach ($document['balance'] as $balanceData) {
@@ -295,7 +299,7 @@ public function getPaymentAddress($currency, $new = false)
295299
} else {
296300
$response = $this->getHttpClient()->get('/api/1/payment/address/' . $currency, array('exceptions' => false));
297301
}
298-
$document = $response->json();
302+
$document = json_decode($response->getBody(), true);
299303
if (isset($document['address'])) {
300304
return $document['address'];
301305
}
@@ -320,7 +324,7 @@ protected function _transferAmount($currency, $amount, $trading)
320324
),
321325
'exceptions' => false
322326
));
323-
$document = $response->json();
327+
$document = json_decode($response->getBody(), true);
324328
if (isset($document['transaction'])) {
325329
return $document['transaction'];
326330
} elseif (isset($document['message'])) {
@@ -376,7 +380,7 @@ public function payout($currency, $amount, $address, $paymentId = null)
376380
),
377381
'exceptions' => false
378382
));
379-
$document = $response->json();
383+
$document = json_decode($response->getBody(), true);
380384
if (isset($document['transaction'])) {
381385
return $document['transaction'];
382386
} elseif (isset($document['message'])) {
@@ -394,7 +398,7 @@ public function getTransactions($sort = 'asc', $offset = 0, $limit = 1000)
394398
);
395399

396400
$response = $this->getHttpClient()->get('/api/1/payment/transactions', array('query' => $query, 'exceptions' => false));
397-
$document = $response->json();
401+
$document = json_decode($response->getBody(), true);
398402
if (isset($document['transactions'])) {
399403
$transactions = [];
400404
foreach ($document['transactions'] as $txn) {

src/Hitbtc/PublicClient.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function getHttpClient()
2525
{
2626
if (!$this->httpClient) {
2727
$this->httpClient = new HttpClient([
28-
'base_url' => $this->host,
28+
'base_uri' => $this->host,
2929
]);
3030
}
3131

@@ -34,7 +34,12 @@ public function getHttpClient()
3434

3535
public function getTicker($ticker)
3636
{
37-
return $this->getHttpClient()->get('/api/1/public/'.$ticker.'/ticker')->json();
37+
return json_decode($this->getHttpClient()->get('/api/1/public/'.$ticker.'/ticker')->getBody(), true);
3838
}
3939

40+
41+
public function getTickers()
42+
{
43+
return json_decode($this->getHttpClient()->get('/api/1/public/ticker')->getBody(), true);
44+
}
4045
}

0 commit comments

Comments
 (0)