diff --git a/composer.json b/composer.json index 470b0d5..aaab936 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "require": { - "guzzlehttp/guzzle": "5.x" + "guzzlehttp/guzzle": "6.x" }, "autoload": { "psr-0": {"Hitbtc": "src/"} diff --git a/src/Hitbtc/AuthMiddleware.php b/src/Hitbtc/AuthMiddleware.php new file mode 100644 index 0000000..b49396d --- /dev/null +++ b/src/Hitbtc/AuthMiddleware.php @@ -0,0 +1,52 @@ +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); + } +} diff --git a/src/Hitbtc/AuthSubscriber.php b/src/Hitbtc/AuthSubscriber.php deleted file mode 100644 index 3d39316..0000000 --- a/src/Hitbtc/AuthSubscriber.php +++ /dev/null @@ -1,47 +0,0 @@ -publicKey = $publicKey; - $this->secretKey = $secretKey; - } - - /** - * @inheritdoc - */ - public function getEvents() - { - return [ - 'before' => ['onBefore', RequestEvents::SIGN_REQUEST] - ]; - } - - public function onBefore(BeforeEvent $event) - { - $event->getRequest()->getQuery()->add('apikey', $this->publicKey); - $event->getRequest()->getQuery()->add('nonce', $this->getNonce()); - - $message = $event->getRequest()->getPath() . '?' . $event->getRequest()->getQuery() . $event->getRequest()->getBody(); - $sign = strtolower(hash_hmac('sha512', $message, $this->secretKey)); - - $event->getRequest()->addHeader('Api-Signature', $sign); - $event->getRequest()->addHeader('User-Agent', 'Hitbtc PHP Client'); - } - - protected function getNonce() - { - return intval(microtime(true) * 1000); - } - -} diff --git a/src/Hitbtc/ProtectedClient.php b/src/Hitbtc/ProtectedClient.php index 29a1658..8257988 100644 --- a/src/Hitbtc/ProtectedClient.php +++ b/src/Hitbtc/ProtectedClient.php @@ -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; @@ -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; @@ -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') { @@ -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'])) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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']; } @@ -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'])) { @@ -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'])) { @@ -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) { diff --git a/src/Hitbtc/PublicClient.php b/src/Hitbtc/PublicClient.php index 31f66c1..4ac08e3 100644 --- a/src/Hitbtc/PublicClient.php +++ b/src/Hitbtc/PublicClient.php @@ -25,7 +25,7 @@ public function getHttpClient() { if (!$this->httpClient) { $this->httpClient = new HttpClient([ - 'base_url' => $this->host, + 'base_uri' => $this->host, ]); } @@ -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); + } }