diff --git a/composer.json b/composer.json index ac37a61..879befb 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "minter/minter-php-bip-44": "1.0.0", "bitwasp/bitcoin-lib": "1.0.4", "simplito/elliptic-php": "1.0.3", - "minter/php-rlp": "^0.2.4" + "minter/php-rlp": "^0.2.4", + "ext-json": "*" }, "minimum-stability": "dev", "prefer-stable": true, diff --git a/src/Minter/Library/Http.php b/src/Minter/Library/Http.php index 1158c6b..96742b5 100644 --- a/src/Minter/Library/Http.php +++ b/src/Minter/Library/Http.php @@ -1,7 +1,9 @@ $url, - 'connect_timeout' => 15.0, - 'timeout' => 30.0, - ]; - - if ($this->client instanceof \GuzzleHttp\Client) - { - $config = $this->client->getConfig(); - $config['base_uri'] = $url; - } - - $this->setClient(new \GuzzleHttp\Client($config)); + $this->client = $client; } - /** - * Set client - * - * @param Client $client - */ - protected function setClient(\GuzzleHttp\Client $client): void - { - $this->client = $client; - } - /** * http get request * - * @param string $url + * @param string $url * @param array|null $parameters * @return mixed * @throws \Exception + * @throws GuzzleException */ protected function get(string $url, array $parameters = null) { @@ -74,9 +55,10 @@ protected function get(string $url, array $parameters = null) * http post request * * @param string $url - * @param array $parameters + * @param array $parameters * @return mixed * @throws \Exception + * @throws GuzzleException */ protected function post(string $url, array $parameters = []) { diff --git a/src/Minter/MinterAPI.php b/src/Minter/MinterAPI.php index 565a1f5..c9dbc51 100644 --- a/src/Minter/MinterAPI.php +++ b/src/Minter/MinterAPI.php @@ -3,6 +3,8 @@ namespace Minter; use Exception; +use GuzzleHttp\Client; +use GuzzleHttp\Exception\GuzzleException; use Minter\Library\Http; /** @@ -16,25 +18,45 @@ class MinterAPI */ use Http; + /** @var float */ + const HTTP_DEFAULT_CONNECT_TIMEOUT = 15.0; + + /** @var float */ + const HTTP_DEFAULT_TIMEOUT = 30.0; + /** * MinterAPI constructor. - * @param bool $nodeUrl + * @param $node */ - public function __construct($nodeUrl) + public function __construct($node) { - if ($nodeUrl instanceof \GuzzleHttp\Client) { - $this->setClient($nodeUrl); - } - else { - $this->setApiUrl($nodeUrl); + if ($node instanceof Client) { + $this->setClient($node); + } else { + $client = $this->createDefaultHttpClient($node); + $this->setClient($client); } } + /** + * @param string $baseUri + * @return Client + */ + public function createDefaultHttpClient(string $baseUri): Client + { + return new Client([ + 'base_uri' => $baseUri, + 'connect_timeout' => self::HTTP_DEFAULT_CONNECT_TIMEOUT, + 'timeout' => self::HTTP_DEFAULT_TIMEOUT, + ]); + } + /** * Get status of node * * @return \stdClass * @throws Exception + * @throws GuzzleException */ public function getStatus(): \stdClass { @@ -45,16 +67,17 @@ public function getStatus(): \stdClass * This endpoint shows candidate’s info by provided public_key. * It will respond with 404 code if candidate is not found. * - * @param string $publicKey + * @param string $publicKey * @param null|int $height * @return \stdClass * @throws Exception + * @throws GuzzleException */ public function getCandidate(string $publicKey, ?int $height = null): \stdClass { $params = ['pub_key' => $publicKey]; - if($height) { + if ($height) { $params['height'] = $height; } @@ -67,6 +90,7 @@ public function getCandidate(string $publicKey, ?int $height = null): \stdClass * @param null|int $height * @return \stdClass * @throws Exception + * @throws GuzzleException */ public function getValidators(?int $height = null): \stdClass { @@ -76,16 +100,17 @@ public function getValidators(?int $height = null): \stdClass /** * Returns the balance of given account and the number of outgoing transaction. * - * @param string $address + * @param string $address * @param null|int $height * @return \stdClass * @throws Exception + * @throws GuzzleException */ public function getBalance(string $address, ?int $height = null): \stdClass { $params = ['address' => $address]; - if($height) { + if ($height) { $params['height'] = $height; } @@ -98,6 +123,7 @@ public function getBalance(string $address, ?int $height = null): \stdClass * @param string $address * @return int * @throws Exception + * @throws GuzzleException */ public function getNonce(string $address): int { @@ -110,6 +136,7 @@ public function getNonce(string $address): int * @param string $tx * @return \stdClass * @throws Exception + * @throws GuzzleException */ public function send(string $tx): \stdClass { @@ -122,10 +149,11 @@ public function send(string $tx): \stdClass * @param string $hash * @return \stdClass * @throws Exception + * @throws GuzzleException */ public function getTransaction(string $hash): \stdClass { - return $this->get('transaction', ['hash' => $hash]); + return $this->get('transaction', ['hash' => $hash]); } /** @@ -134,6 +162,7 @@ public function getTransaction(string $hash): \stdClass * @param int $height * @return \stdClass * @throws Exception + * @throws GuzzleException */ public function getBlock(int $height): \stdClass { @@ -146,6 +175,7 @@ public function getBlock(int $height): \stdClass * @param int $height * @return \stdClass * @throws Exception + * @throws GuzzleException */ public function getEvents(int $height): \stdClass { @@ -159,16 +189,17 @@ public function getEvents(int $height): \stdClass * @param bool|null $includeStakes * @return \stdClass * @throws Exception + * @throws GuzzleException */ public function getCandidates(?int $height = null, ?bool $includeStakes = false): \stdClass { $params = []; - if($includeStakes) { + if ($includeStakes) { $params['include_stakes'] = 'true'; } - if($height) { + if ($height) { $params['height'] = $height; } @@ -183,12 +214,13 @@ public function getCandidates(?int $height = null, ?bool $includeStakes = false) * @param string $symbol * @return \stdClass * @throws Exception + * @throws GuzzleException */ public function getCoinInfo(string $symbol, ?int $height = null): \stdClass { $params = ['symbol' => $symbol]; - if($height) { + if ($height) { $params['height'] = $height; } @@ -198,22 +230,27 @@ public function getCoinInfo(string $symbol, ?int $height = null): \stdClass /** * Return estimate of sell coin transaction. * - * @param string $coinToSell - * @param string $valueToSell - * @param string $coinToBuy + * @param string $coinToSell + * @param string $valueToSell + * @param string $coinToBuy * @param null|int $height * @return \stdClass * @throws Exception + * @throws GuzzleException */ - public function estimateCoinSell(string $coinToSell, string $valueToSell, string $coinToBuy, ?int $height = null): \stdClass - { + public function estimateCoinSell( + string $coinToSell, + string $valueToSell, + string $coinToBuy, + ?int $height = null + ): \stdClass { $params = [ - 'coin_to_sell' => $coinToSell, + 'coin_to_sell' => $coinToSell, 'value_to_sell' => $valueToSell, - 'coin_to_buy' => $coinToBuy + 'coin_to_buy' => $coinToBuy ]; - if($height) { + if ($height) { $params['height'] = $height; } @@ -223,22 +260,27 @@ public function estimateCoinSell(string $coinToSell, string $valueToSell, string /** * Return estimate of buy coin transaction. * - * @param string $coinToSell - * @param string $valueToBuy - * @param string $coinToBuy + * @param string $coinToSell + * @param string $valueToBuy + * @param string $coinToBuy * @param null|int $height * @return \stdClass * @throws Exception + * @throws GuzzleException */ - public function estimateCoinBuy(string $coinToSell, string $valueToBuy, string $coinToBuy, ?int $height = null): \stdClass - { + public function estimateCoinBuy( + string $coinToSell, + string $valueToBuy, + string $coinToBuy, + ?int $height = null + ): \stdClass { $params = [ 'coin_to_sell' => $coinToSell, 'value_to_buy' => $valueToBuy, - 'coin_to_buy' => $coinToBuy + 'coin_to_buy' => $coinToBuy ]; - if($height) { + if ($height) { $params['height'] = $height; } @@ -251,6 +293,7 @@ public function estimateCoinBuy(string $coinToSell, string $valueToBuy, string $ * @param string $tx * @return \stdClass * @throws Exception + * @throws GuzzleException */ public function estimateTxCommission(string $tx): \stdClass { @@ -265,16 +308,17 @@ public function estimateTxCommission(string $tx): \stdClass * @param int|null $perPage * @return \stdClass * @throws Exception + * @throws GuzzleException */ public function getTransactions(string $query, ?int $page = null, ?int $perPage = null): \stdClass { $params = ['query' => $query]; - if($page) { + if ($page) { $params['page'] = $page; } - if($perPage) { + if ($perPage) { $params['perPage'] = $perPage; } @@ -288,6 +332,7 @@ public function getTransactions(string $query, ?int $page = null, ?int $perPage * @param int|null $limit * @return \stdClass * @throws Exception + * @throws GuzzleException */ public function getUnconfirmedTxs(?int $limit = null): \stdClass { @@ -300,6 +345,7 @@ public function getUnconfirmedTxs(?int $limit = null): \stdClass * @param int|null $height * @return \stdClass * @throws Exception + * @throws GuzzleException */ public function getMaxGasPrice(?int $height = null): \stdClass { @@ -311,6 +357,7 @@ public function getMaxGasPrice(?int $height = null): \stdClass * * @return \stdClass * @throws Exception + * @throws GuzzleException */ public function getMinGasPrice(): \stdClass { @@ -324,11 +371,12 @@ public function getMinGasPrice(): \stdClass * @param int|null $height * @return \stdClass * @throws Exception + * @throws GuzzleException */ public function getMissedBlocks(string $pubKey, ?int $height = null): \stdClass { $params = ['pub_key' => $pubKey]; - if($height) { + if ($height) { $params['height'] = $height; }