From 527aaa1fc6ad0b066110a8ad22823c5c878998a2 Mon Sep 17 00:00:00 2001 From: TechTailor Date: Sat, 27 Nov 2021 20:22:57 +0000 Subject: [PATCH] Apply fixes from StyleCI --- config/wazirx-api.php | 6 +- src/Traits/HandlesResponseErrors.php | 62 +++++----- src/WazirxAPI.php | 174 +++++++++++++++------------ 3 files changed, 127 insertions(+), 115 deletions(-) diff --git a/config/wazirx-api.php b/config/wazirx-api.php index 0095794..f49d83c 100644 --- a/config/wazirx-api.php +++ b/config/wazirx-api.php @@ -13,7 +13,7 @@ 'auth' => [ 'key' => env('WAZIRX_KEY', ''), - 'secret' => env('WAZIRX_SECRET', '') + 'secret' => env('WAZIRX_SECRET', ''), ], /* @@ -26,7 +26,7 @@ */ 'urls' => [ - 'api' => 'https://api.wazirx.com', + 'api' => 'https://api.wazirx.com', 'sapi' => 'https://api.wazirx.com/sapi/', ], @@ -40,7 +40,7 @@ */ 'settings' => [ - 'timing' => env('WAZIRX_TIMING', 5000) + 'timing' => env('WAZIRX_TIMING', 5000), ], ]; diff --git a/src/Traits/HandlesResponseErrors.php b/src/Traits/HandlesResponseErrors.php index feccd6a..302b141 100644 --- a/src/Traits/HandlesResponseErrors.php +++ b/src/Traits/HandlesResponseErrors.php @@ -8,63 +8,57 @@ private function handleError($response) { // Set a default error. $error = [ - "code" => "1000", - "error" => "Invalid", - "message" => "Unable to identify the type of error." + 'code' => '1000', + 'error' => 'Invalid', + 'message' => 'Unable to identify the type of error.', ]; // Return server related errors (500 range). - if($response->serverError()) - { + if ($response->serverError()) { // TBA } // Return client related errors. - elseif($response->clientError()) - { + elseif ($response->clientError()) { // If client error has a response code. - if(isset($response['code'])) - { + if (isset($response['code'])) { // Switch between known Wazirx error codes. - switch($response['code']) - { - case('1999') : + switch ($response['code']) { + case '1999': $error = [ - "code" => "1999", - "error" => "Api Error", - "message" => "Symbol does not have a valid value." + 'code' => '1999', + 'error' => 'Api Error', + 'message' => 'Symbol does not have a valid value.', ]; break; - case('2000') : + case '2000': $error = [ - "code" => "2000", - "error" => "Api Connection Error", - "message" => "Something went wrong. Please contact support." + 'code' => '2000', + 'error' => 'Api Connection Error', + 'message' => 'Something went wrong. Please contact support.', ]; break; - case('2005') : + case '2005': $error = [ - "code" => "2005", - "error" => "Api Connection Error", - "message" => "API Signature/Secret is Invalid." + 'code' => '2005', + 'error' => 'Api Connection Error', + 'message' => 'API Signature/Secret is Invalid.', ]; break; - case('2112') : + case '2112': $error = [ - "code" => "2112", - "error" => "Api Connection Error", - "message" => "API Key is missing / Invalid API Key." + 'code' => '2112', + 'error' => 'Api Connection Error', + 'message' => 'API Key is missing / Invalid API Key.', ]; break; } - } - else { + } else { // If client error a response status. - if($response->status() === 403) - { + if ($response->status() === 403) { $error = [ - "code" => "403", - "error" => "Forbidden", - "message" => "You don't have permission to access this resouce." + 'code' => '403', + 'error' => 'Forbidden', + 'message' => "You don't have permission to access this resouce.", ]; } } diff --git a/src/WazirxAPI.php b/src/WazirxAPI.php index a114667..ad1bf8a 100644 --- a/src/WazirxAPI.php +++ b/src/WazirxAPI.php @@ -27,31 +27,32 @@ class WazirxAPI /** * Constructor for WazirxAPI. - * @param string $key API key - * @param string $secret API secret - * @param string $api_url API base URL (see config for example) - * @param integer $timing Wazirx API timing setting (default 10000) + * + * @param string $key API key + * @param string $secret API secret + * @param string $api_url API base URL (see config for example) + * @param int $timing Wazirx API timing setting (default 10000) */ public function __construct($api_key = null, $api_secret = null, $api_url = null, $timing = 10000) { - $this->api_key = (! empty($api_key)) ? $api_key : config('wazirx-api.auth.key'); - $this->api_secret = (! empty($api_secret)) ? $api_secret : config('wazirx-api.auth.secret'); - $this->api_url = (! empty($api_url)) ? $api_url : config('wazirx-api.urls.sapi'); - $this->recvWindow = (! empty($timing)) ? $timing : config('wazirx-api.settings.timing'); + $this->api_key = (!empty($api_key)) ? $api_key : config('wazirx-api.auth.key'); + $this->api_secret = (!empty($api_secret)) ? $api_secret : config('wazirx-api.auth.secret'); + $this->api_url = (!empty($api_url)) ? $api_url : config('wazirx-api.urls.sapi'); + $this->recvWindow = (!empty($timing)) ? $timing : config('wazirx-api.settings.timing'); } /** * API Key and Secret Key setter function. * It's required for USER_DATA endpoints. - * https://docs.wazirx.com/#endpoint-security-type + * https://docs.wazirx.com/#endpoint-security-type. * * @param string $key API Key * @param string $secret API Secret */ public function setAPI($api_key, $api_secret) { - $this->api_key = $api_key; - $this->api_secret = $api_secret; + $this->api_key = $api_key; + $this->api_secret = $api_secret; } //------ PUBLIC API CALLS -------- @@ -67,8 +68,9 @@ public function setAPI($api_key, $api_secret) /** * Get Wazirx Server Time. * - * @return mixed * @throws \Exception + * + * @return mixed */ public function getTime() { @@ -78,8 +80,9 @@ public function getTime() /** * Get Wazirx Server Status. * - * @return mixed * @throws \Exception + * + * @return mixed */ public function getServerStatus() { @@ -89,8 +92,9 @@ public function getServerStatus() /** * Get Wazirx Exchange Info. * - * @return mixed * @throws \Exception + * + * @return mixed */ public function getExchangeInfo() { @@ -100,8 +104,9 @@ public function getExchangeInfo() /** * Get Wazirx 24hr Tickers Price Change Statistics. * - * @return mixed * @throws \Exception + * + * @return mixed */ public function getTickers() { @@ -111,14 +116,16 @@ public function getTickers() /** * Get Wazirx 24hr Ticker of a particular symbol. * - * @param string $symbol Crypto Coin Pair Symbol - * @return mixed + * @param string $symbol Crypto Coin Pair Symbol + * * @throws \Exception + * + * @return mixed */ public function getTicker($symbol) { $data = [ - 'symbol' => $symbol + 'symbol' => $symbol, ]; return $this->publicRequest('v1/ticker/24hr', $data); @@ -135,12 +142,14 @@ public function getTicker($symbol) */ /** - * Get current account information + * Get current account information. + * + * @param int $iterator Number of times we've tried to get balances. + * @param int $max Max times to retry the call. * - * @param integer $iterator Number of times we've tried to get balances. - * @param integer $max Max times to retry the call. - * @return mixed * @throws \Exception + * + * @return mixed */ public function getAccountInfo($iterator = 1, $max = 3) { @@ -150,12 +159,14 @@ public function getAccountInfo($iterator = 1, $max = 3) } /** - * Get fund details for the current account + * Get fund details for the current account. + * + * @param int $iterator Number of times we've tried to get balances. + * @param int $max Max times to retry the call. * - * @param integer $iterator Number of times we've tried to get balances. - * @param integer $max Max times to retry the call. - * @return mixed * @throws \Exception + * + * @return mixed */ public function getFunds($iterator = 1, $max = 3) { @@ -167,16 +178,18 @@ public function getFunds($iterator = 1, $max = 3) /** * Get all account orders; "idle", "wait", "cancel" or "done". * - * @param integer $iterator Number of times we've tried to get balances. - * @param integer $max Max times to retry the call. - * @param string $symbol Crypto Coin Pair Symbol - * @return mixed + * @param int $iterator Number of times we've tried to get balances. + * @param int $max Max times to retry the call. + * @param string $symbol Crypto Coin Pair Symbol + * * @throws \Exception + * + * @return mixed */ public function getAllOrders($symbol, $iterator = 1, $max = 3) { $params = [ - 'symbol' => strtolower($symbol) + 'symbol' => strtolower($symbol), ]; $response = $this->privateRequest('v1/allOrders', $params); @@ -185,12 +198,14 @@ public function getAllOrders($symbol, $iterator = 1, $max = 3) } /** - * Get all open orders on a symbol for the current account + * Get all open orders on a symbol for the current account. + * + * @param int $iterator Number of times we've tried to get balances. + * @param int $max Max times to retry the call. * - * @param integer $iterator Number of times we've tried to get balances. - * @param integer $max Max times to retry the call. - * @return mixed * @throws \Exception + * + * @return mixed */ public function getOpenOrders($iterator = 1, $max = 3) { @@ -200,17 +215,19 @@ public function getOpenOrders($iterator = 1, $max = 3) } /** - * Check an order's status for the current account + * Check an order's status for the current account. + * + * @param int $iterator Number of times we've tried to get balances. + * @param int $max Max times to retry the call. * - * @param integer $iterator Number of times we've tried to get balances. - * @param integer $max Max times to retry the call. - * @return mixed * @throws \Exception + * + * @return mixed */ public function getOrderStatus($orderId, $iterator = 1, $max = 3) { $params = [ - 'orderId' => $orderId + 'orderId' => $orderId, ]; $response = $this->privateRequest('v1/order', $params); @@ -222,24 +239,25 @@ public function getOrderStatus($orderId, $iterator = 1, $max = 3) * Make public requests (Security Type: NONE). * * @param string $url URL Endpoint - * @param array $params Required and optional parameters + * @param array $params Required and optional parameters * @param string $method GET, POST, PUT, DELETE - * @return mixed + * * @throws \Exception + * + * @return mixed */ private function publicRequest($url, $params = [], $method = 'GET') { // Build the POST data string - if (! in_array($url, $this->no_time_needed)) - { - $params['timestamp'] = $this->milliseconds(); + if (!in_array($url, $this->no_time_needed)) { + $params['timestamp'] = $this->milliseconds(); $params['recvWindow'] = $this->recvWindow; } - $url = $this->api_url . $url; + $url = $this->api_url.$url; // Adding parameters to the url. - $url = $url . '?' . http_build_query($params); + $url = $url.'?'.http_build_query($params); return $this->sendApiRequest($url, $method); } @@ -248,25 +266,23 @@ private function publicRequest($url, $params = [], $method = 'GET') * Make public requests (Security Type: USER_DATA). * * @param string $url URL Endpoint - * @param array $params Required and optional parameters. - * + * @param array $params Required and optional parameters. */ private function privateRequest($url, $params = [], $method = 'GET') { // Build the POST data string - if (! in_array($url, $this->no_time_needed)) - { + if (!in_array($url, $this->no_time_needed)) { $params['recvWindow'] = $this->recvWindow; - $params['timestamp'] = $this->milliseconds(); + $params['timestamp'] = $this->milliseconds(); } // Build the query to pass through. - $query = http_build_query($params, '', '&'); + $query = http_build_query($params, '', '&'); // Set API key and sign the message - $signature = hash_hmac('sha256', $query, $this->api_secret); + $signature = hash_hmac('sha256', $query, $this->api_secret); - $url = $this->api_url . $url . '?' . $query . '&signature=' . $signature; + $url = $this->api_url.$url.'?'.$query.'&signature='.$signature; return $this->sendApiRequest($url, $method); } @@ -276,52 +292,54 @@ private function privateRequest($url, $params = [], $method = 'GET') * * @param string $url URL Endpoint with Query & Signature * @param string $method GET, POST, PUT, DELETE - * @return mixed + * * @throws \Exception + * + * @return mixed */ private function sendApiRequest($url, $method) { try { - if ($method == 'POST') + if ($method == 'POST') { $response = Http::withHeaders([ - 'X-API-KEY' => $this->api_key - ])->post($url); - - elseif ($method == 'GET') + 'X-API-KEY' => $this->api_key, + ])->post($url); + } elseif ($method == 'GET') { $response = Http::withHeaders([ - 'X-API-KEY' => $this->api_key - ])->get($url); - } - catch (ConnectionException $e) { + 'X-API-KEY' => $this->api_key, + ])->get($url); + } + } catch (ConnectionException $e) { return $error = [ - 'code' => $e->getCode(), - 'error' => 'Host Not Found', - 'message' => 'Could not resolve host: ' . $this->api_url + 'code' => $e->getCode(), + 'error' => 'Host Not Found', + 'message' => 'Could not resolve host: '.$this->api_url, ]; - } - catch (Exception $e) { + } catch (Exception $e) { return $error = [ - 'code' => $e->getCode(), - 'error' => 'cUrl Error', - 'message' => $e->getMessage() + 'code' => $e->getCode(), + 'error' => 'cUrl Error', + 'message' => $e->getMessage(), ]; } // If response if Ok. Return collection. - if($response->ok()) + if ($response->ok()) { return $response->collect(); - else + } else { return $this->handleError($response); + } } /** * Get the milliseconds from the system clock. - * @return integer + * + * @return int */ private function milliseconds() { - list ($msec, $sec) = explode (' ', microtime ()); + list($msec, $sec) = explode(' ', microtime()); - return $sec . substr ($msec, 2, 3); + return $sec.substr($msec, 2, 3); } }