Skip to content

Commit

Permalink
Merge pull request hitbtc-com#1 from toooni/master
Browse files Browse the repository at this point in the history
guzzle 6
  • Loading branch information
hitbtc-com authored Sep 4, 2017
2 parents 484a893 + ba5d336 commit 18ef23c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 65 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"guzzlehttp/guzzle": "5.x"
"guzzlehttp/guzzle": "6.x"
},
"autoload": {
"psr-0": {"Hitbtc": "src/"}
Expand Down
52 changes: 52 additions & 0 deletions src/Hitbtc/AuthMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Hitbtc;


use Psr\Http\Message\RequestInterface;

class AuthMiddleware
{
protected $publicKey;
protected $secretKey;

public function __construct($publicKey, $secretKey)
{
$this->publicKey = $publicKey;
$this->secretKey = $secretKey;
}

/**
* @param callable $handler
*
* @return callable
*/
public function __invoke(callable $handler)
{
return function (RequestInterface $request, array $options) use (&$handler) {
$queryString = $request->getUri()->getQuery();
$queryParts = \GuzzleHttp\Psr7\parse_query($queryString);

$queryParts['apikey'] = $this->publicKey;
$queryParts['nonce'] = $this->getNonce();

$queryString = \GuzzleHttp\Psr7\build_query($queryParts);
$request = $request->withUri($request->getUri()->withQuery($queryString));

$message = $request->getUri()->getPath(). '?' . $queryString . $request->getBody();
$sign = strtolower(hash_hmac('sha512', $message, $this->secretKey));

$request = $request
->withAddedHeader('Api-Signature', $sign)
->withAddedHeader('User-Agent', 'Hitbtc PHP Client')
;

return $handler($request, $options);
};
}

protected function getNonce()
{
return intval(microtime(true) * 1000);
}
}
47 changes: 0 additions & 47 deletions src/Hitbtc/AuthSubscriber.php

This file was deleted.

34 changes: 19 additions & 15 deletions src/Hitbtc/ProtectedClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Hitbtc;

use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Message\Response;
use Hitbtc\Exception\InvalidRequestException;
use Hitbtc\Exception\RejectException;
Expand Down Expand Up @@ -45,11 +47,13 @@ public function __construct($publicKey, $secretKey, $demo = false)
public function getHttpClient()
{
if (!$this->httpClient) {
$stack = HandlerStack::create();
$stack->push(new AuthMiddleware($this->publicKey, $this->secretKey));

$this->httpClient = new HttpClient([
'base_url' => $this->host,
'handler' => $stack,
'base_uri' => $this->host,
]);
$this->httpClient->getEmitter()->attach(new AuthSubscriber($this->publicKey, $this->secretKey));

}

return $this->httpClient;
Expand All @@ -69,7 +73,7 @@ public function newOrder(NewOrder $order)
'body' => $order->asArray(),
'exceptions' => false,
));
$document = $response->json();
$document = json_decode($response->getBody(), true);

if (isset($document['ExecutionReport'])) {
if ($document['ExecutionReport']['execReportType'] == 'rejected') {
Expand Down Expand Up @@ -105,7 +109,7 @@ public function cancelOrder(Order $order, $cancelRequestId = null)
),
'exceptions' => false,
));
$document = $response->json();
$document = json_decode($response->getBody(), true);
if (isset($document['ExecutionReport'])) {
return new Order($document['ExecutionReport']);
} elseif (isset($document['CancelReject'])) {
Expand All @@ -127,7 +131,7 @@ public function getActiveOrders($symbols = null)
$params['query']['symbols'] = implode(',', (array) $symbols);
}
$response = $this->getHttpClient()->get('/api/1/trading/orders/active', $params);
$document = $response->json();
$document = json_decode($response->getBody(), true);
if (isset($document['orders'])) {
$orders = [];
foreach ($document['orders'] as $orderData) {
Expand Down Expand Up @@ -164,7 +168,7 @@ public function getRecentOrders($symbols = null, $sort = 'asc', $statuses = null
}

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

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

$response = $this->getHttpClient()->get('/api/1/trading/trades/by/order', array('query' => $query, 'exceptions' => false));
$document = $response->json();
$document = json_decode($response->getBody(), true);
if (isset($document['trades'])) {
$trades = [];
foreach ($document['trades'] as $tradeData) {
Expand All @@ -251,7 +255,7 @@ public function getTradesByOrder($clientOrderId)
public function getBalanceTrading()
{
$response = $this->getHttpClient()->get('/api/1/trading/balance', array('exceptions' => false));
$document = $response->json();
$document = json_decode($response->getBody(), true);
if (isset($document['balance'])) {
$balances = [];
foreach ($document['balance'] as $balanceData) {
Expand All @@ -270,7 +274,7 @@ public function getBalanceTrading()
public function getBalanceMain()
{
$response = $this->getHttpClient()->get('/api/1/payment/balance', array('exceptions' => false));
$document = $response->json();
$document = json_decode($response->getBody(), true);
if (isset($document['balance'])) {
$balances = [];
foreach ($document['balance'] as $balanceData) {
Expand All @@ -295,7 +299,7 @@ public function getPaymentAddress($currency, $new = false)
} else {
$response = $this->getHttpClient()->get('/api/1/payment/address/' . $currency, array('exceptions' => false));
}
$document = $response->json();
$document = json_decode($response->getBody(), true);
if (isset($document['address'])) {
return $document['address'];
}
Expand All @@ -320,7 +324,7 @@ protected function _transferAmount($currency, $amount, $trading)
),
'exceptions' => false
));
$document = $response->json();
$document = json_decode($response->getBody(), true);
if (isset($document['transaction'])) {
return $document['transaction'];
} elseif (isset($document['message'])) {
Expand Down Expand Up @@ -376,7 +380,7 @@ public function payout($currency, $amount, $address, $paymentId = null)
),
'exceptions' => false
));
$document = $response->json();
$document = json_decode($response->getBody(), true);
if (isset($document['transaction'])) {
return $document['transaction'];
} elseif (isset($document['message'])) {
Expand All @@ -394,7 +398,7 @@ public function getTransactions($sort = 'asc', $offset = 0, $limit = 1000)
);

$response = $this->getHttpClient()->get('/api/1/payment/transactions', array('query' => $query, 'exceptions' => false));
$document = $response->json();
$document = json_decode($response->getBody(), true);
if (isset($document['transactions'])) {
$transactions = [];
foreach ($document['transactions'] as $txn) {
Expand Down
9 changes: 7 additions & 2 deletions src/Hitbtc/PublicClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function getHttpClient()
{
if (!$this->httpClient) {
$this->httpClient = new HttpClient([
'base_url' => $this->host,
'base_uri' => $this->host,
]);
}

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

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


public function getTickers()
{
return json_decode($this->getHttpClient()->get('/api/1/public/ticker')->getBody(), true);
}
}

0 comments on commit 18ef23c

Please sign in to comment.