From 19f728c5925f195c535aab36980cfbfcedb7895c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9opold=20Gu=C3=A9rin?= Date: Thu, 19 Mar 2020 12:04:59 +0100 Subject: [PATCH 1/8] Add page size parameter --- .idea/.gitignore | 2 ++ .idea/laravel-antpool-api.iml | 12 ++++++++++++ .idea/misc.xml | 6 ++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ src/Antpool.php | 11 +++++++++-- src/AntpoolServiceProvider.php | 2 +- src/config/antpool.php | 6 +++++- 8 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/laravel-antpool-api.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..e7e9d11 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml diff --git a/.idea/laravel-antpool-api.iml b/.idea/laravel-antpool-api.iml new file mode 100644 index 0000000..240abeb --- /dev/null +++ b/.idea/laravel-antpool-api.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..28a804d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..740b70c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/Antpool.php b/src/Antpool.php index 05f4b67..a558041 100644 --- a/src/Antpool.php +++ b/src/Antpool.php @@ -31,6 +31,10 @@ class Antpool * @var string */ protected $secret; + /** + * @var int + */ + protected $page_size; /** * Constructor @@ -39,9 +43,10 @@ class Antpool * @param string $username * @param string $key * @param string $secret + * @param int $page_size * @throws \Exception */ - public function __construct($username, $key, $secret) + public function __construct($username, $key, $secret, $page_size = 10) { if (!function_exists('curl_exec')) { throw new \Exception("Error: Please install PHP curl extension to use this lib."); @@ -50,6 +55,7 @@ public function __construct($username, $key, $secret) $this->username = $username; $this->key = $key; $this->secret = $secret; + $this->page_size = $page_size; } /** @@ -70,7 +76,8 @@ public function get($type, $coin = 'BTC') 'key' => $this->key, 'nonce' => $nonce, 'signature' => $hmac, - 'coin' => $coin + 'coin' => $coin, + 'pageSize' => $this->page_size ); $post_data = ''; diff --git a/src/AntpoolServiceProvider.php b/src/AntpoolServiceProvider.php index 715b430..6be3a92 100644 --- a/src/AntpoolServiceProvider.php +++ b/src/AntpoolServiceProvider.php @@ -28,7 +28,7 @@ public function boot() public function register() { $this->app->bind('antpool', function () { - return new Antpool(config('antpool.username'), config('antpool.api_key'), config('antpool.api_secret')); + return new Antpool(config('antpool.username'), config('antpool.api_key'), config('antpool.api_secret'), config('antpool.page_size')); }); } diff --git a/src/config/antpool.php b/src/config/antpool.php index 07d12a8..b036c92 100644 --- a/src/config/antpool.php +++ b/src/config/antpool.php @@ -16,5 +16,9 @@ /* * Api key */ - 'api_secret' => env('ANTPOOL_SECRET') + 'api_secret' => env('ANTPOOL_SECRET'), + /* + * Page Size + */ + 'page_size' => env('ANTPOOL_PAGE_SIZE') ]; \ No newline at end of file From 79c3b0ca06144c87864cb5a78865e5951c0e31a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9opold=20Gu=C3=A9rin?= Date: Sat, 21 Mar 2020 13:14:24 +0100 Subject: [PATCH 2/8] Parameter pageSize Put the optionnal parameter pageSize on the get function only Remove from config and constructor --- src/Antpool.php | 25 ++++++++++++++++--------- src/AntpoolServiceProvider.php | 2 +- src/config/antpool.php | 6 +----- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/Antpool.php b/src/Antpool.php index a558041..6442b22 100644 --- a/src/Antpool.php +++ b/src/Antpool.php @@ -31,10 +31,6 @@ class Antpool * @var string */ protected $secret; - /** - * @var int - */ - protected $page_size; /** * Constructor @@ -43,10 +39,9 @@ class Antpool * @param string $username * @param string $key * @param string $secret - * @param int $page_size * @throws \Exception */ - public function __construct($username, $key, $secret, $page_size = 10) + public function __construct($username, $key, $secret) { if (!function_exists('curl_exec')) { throw new \Exception("Error: Please install PHP curl extension to use this lib."); @@ -55,7 +50,16 @@ public function __construct($username, $key, $secret, $page_size = 10) $this->username = $username; $this->key = $key; $this->secret = $secret; - $this->page_size = $page_size; + } + + /** + * Test if the type can support the pageSize parameter + * + * @param $type + * @return bool + */ + function hasPageSizeParameter($type) { + return $type === 'workers' || $type === 'paymentHistory'; } /** @@ -63,10 +67,11 @@ public function __construct($username, $key, $secret, $page_size = 10) * * @param string $type * @param string $coin BTC, LTC, ETH, ZEC, DAS + * @param int $page_size default 10 * @return mixed * @throws \Exception */ - public function get($type, $coin = 'BTC') + public function get($type, $coin = 'BTC', $page_size = 10) { $nonce = time(); $hmac_message = $this->username . $this->key . $nonce; @@ -77,9 +82,11 @@ public function get($type, $coin = 'BTC') 'nonce' => $nonce, 'signature' => $hmac, 'coin' => $coin, - 'pageSize' => $this->page_size ); + if($this->hasPageSizeParameter($type)) + array_push ( $post_fields, ['pageSize' => $page_size]); + $post_data = ''; foreach ($post_fields as $key => $value) { $post_data .= $key . '=' . $value . '&'; diff --git a/src/AntpoolServiceProvider.php b/src/AntpoolServiceProvider.php index 6be3a92..715b430 100644 --- a/src/AntpoolServiceProvider.php +++ b/src/AntpoolServiceProvider.php @@ -28,7 +28,7 @@ public function boot() public function register() { $this->app->bind('antpool', function () { - return new Antpool(config('antpool.username'), config('antpool.api_key'), config('antpool.api_secret'), config('antpool.page_size')); + return new Antpool(config('antpool.username'), config('antpool.api_key'), config('antpool.api_secret')); }); } diff --git a/src/config/antpool.php b/src/config/antpool.php index b036c92..07d12a8 100644 --- a/src/config/antpool.php +++ b/src/config/antpool.php @@ -16,9 +16,5 @@ /* * Api key */ - 'api_secret' => env('ANTPOOL_SECRET'), - /* - * Page Size - */ - 'page_size' => env('ANTPOOL_PAGE_SIZE') + 'api_secret' => env('ANTPOOL_SECRET') ]; \ No newline at end of file From ba9d6eb8bc386047a4c0bc59cd5964b2859161ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9opold=20Gu=C3=A9rin?= Date: Sat, 21 Mar 2020 13:26:46 +0100 Subject: [PATCH 3/8] Oups array_merge fix --- src/Antpool.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Antpool.php b/src/Antpool.php index 6442b22..91be72f 100644 --- a/src/Antpool.php +++ b/src/Antpool.php @@ -85,7 +85,7 @@ public function get($type, $coin = 'BTC', $page_size = 10) ); if($this->hasPageSizeParameter($type)) - array_push ( $post_fields, ['pageSize' => $page_size]); + array_merge( $post_fields, array('pageSize' => $page_size)); $post_data = ''; foreach ($post_fields as $key => $value) { From d5385adea8385c99b7acbc687ce2f86069b5977b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9opold=20Gu=C3=A9rin?= Date: Sat, 21 Mar 2020 13:29:20 +0100 Subject: [PATCH 4/8] Oups array_merge fix 2 --- src/Antpool.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Antpool.php b/src/Antpool.php index 91be72f..ee01f1e 100644 --- a/src/Antpool.php +++ b/src/Antpool.php @@ -85,7 +85,7 @@ public function get($type, $coin = 'BTC', $page_size = 10) ); if($this->hasPageSizeParameter($type)) - array_merge( $post_fields, array('pageSize' => $page_size)); + $post_fields = array_merge( $post_fields, array('pageSize' => $page_size)); $post_data = ''; foreach ($post_fields as $key => $value) { From d0617a29014ed494763ce61752dc01425cf0af0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9opold=20Gu=C3=A9rin?= Date: Sat, 21 Mar 2020 17:29:44 +0100 Subject: [PATCH 5/8] Add Git ignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file From bcdd2f4ab9e68086e4ea0bb904d48ecd9bfc46f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9opold=20Gu=C3=A9rin?= Date: Sat, 21 Mar 2020 17:30:50 +0100 Subject: [PATCH 6/8] Remove .idea --- .idea/.gitignore | 2 -- .idea/laravel-antpool-api.iml | 12 ------------ .idea/misc.xml | 6 ------ .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ 5 files changed, 34 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/laravel-antpool-api.iml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index e7e9d11..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# Default ignored files -/workspace.xml diff --git a/.idea/laravel-antpool-api.iml b/.idea/laravel-antpool-api.iml deleted file mode 100644 index 240abeb..0000000 --- a/.idea/laravel-antpool-api.iml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 28a804d..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 740b70c..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 5931af25503e246ab2ace76bb66d1bed661ed036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9opold=20Gu=C3=A9rin?= Date: Mon, 23 Nov 2020 19:44:01 +0000 Subject: [PATCH 7/8] Add page parameter in get method --- src/Antpool.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Antpool.php b/src/Antpool.php index ee01f1e..3f2946b 100644 --- a/src/Antpool.php +++ b/src/Antpool.php @@ -68,10 +68,11 @@ function hasPageSizeParameter($type) { * @param string $type * @param string $coin BTC, LTC, ETH, ZEC, DAS * @param int $page_size default 10 + * @param int $page * @return mixed * @throws \Exception */ - public function get($type, $coin = 'BTC', $page_size = 10) + public function get($type, $coin = 'BTC', $page_size = 10, $page = 1) { $nonce = time(); $hmac_message = $this->username . $this->key . $nonce; @@ -82,6 +83,7 @@ public function get($type, $coin = 'BTC', $page_size = 10) 'nonce' => $nonce, 'signature' => $hmac, 'coin' => $coin, + 'page' => $page, ); if($this->hasPageSizeParameter($type)) From 5549e9128aefcf02a36a5ab35b24c94292231bdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9opold=20Gu=C3=A9rin?= Date: Fri, 28 Jan 2022 18:08:59 +0100 Subject: [PATCH 8/8] Add Guzzle --- composer.json | 4 +++- src/Antpool.php | 44 ++++++++++++++++---------------------------- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/composer.json b/composer.json index 57098c5..9c98809 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,9 @@ ], "minimum-stability": "dev", "require": { - "php": ">=5.6" + "php": ">=5.6", + "ext-json": "*", + "guzzlehttp/guzzle": "^7.3" }, "autoload": { "psr-4": { diff --git a/src/Antpool.php b/src/Antpool.php index 3f2946b..26eec57 100644 --- a/src/Antpool.php +++ b/src/Antpool.php @@ -17,6 +17,9 @@ */ namespace Aburakovskiy\LaravelAntpoolApi; +use GuzzleHttp\Client; +use GuzzleHttp\RequestOptions; + class Antpool { /** @@ -95,8 +98,7 @@ public function get($type, $coin = 'BTC', $page_size = 10, $page = 1) } rtrim($post_data, '&'); - $apiData = $this->api_get($type, $post_fields, $post_data); - return $apiData; + return $this->api_get($type, $post_fields, $post_data); } /* @@ -110,37 +112,23 @@ public function get($type, $coin = 'BTC', $page_size = 10, $page = 1) * @param array $post_fields * @param string $post_data * @return mixed - * @throws \Exception + * @throws \Exception|\GuzzleHttp\Exception\GuzzleException */ public function api_get($type, $post_fields, $post_data) { - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, 'https://antpool.com/api/' . $type . '.htm'); - // todo: switch to public cert - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch, CURLOPT_POST, count($post_fields)); - curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - // set large timeout because API lak sometimes - curl_setopt($ch, CURLOPT_TIMEOUT, 60); - $result = curl_exec($ch); - curl_close($ch); - - // check if curl was timed out - if ($result === false) { - throw new \Exception('Error: No API connect'); - } - - // validate JSON - $result_json = json_decode($result, true); - if (json_last_error() != JSON_ERROR_NONE) { - throw new \Exception('Error: read broken JSON from API - JSON Error: ' . json_last_error() . ' (' . $result . ')'); - } + $client = new Client(); + $response = json_decode($client->request( + 'POST', + 'https://antpool.com/api/' . $type . '.htm?'.$post_data, + ["headers" => [ + 'Accept' => 'application/json', + ]] + )->getBody()->getContents()); - if ($result_json['message'] == 'ok') { - return $result_json['data']; + if ($response->message === 'ok') { + return $response->data; } else { - throw new \Exception('API Error: ' . print_r($result_json, true)); + throw new \Exception('API Error: ' . print_r($response, true)); } } } \ No newline at end of file