From 9f0b659acee60958d463604770ffb9893efac145 Mon Sep 17 00:00:00 2001 From: Artur Khylskyi Date: Mon, 14 Nov 2022 17:22:57 +0200 Subject: [PATCH 1/2] Base Requests --- src/Enums/HttpMethod.php | 12 +++ src/Exceptions/PowerApiException.php | 10 +++ src/PowerClient.php | 128 +++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 src/Enums/HttpMethod.php create mode 100644 src/Exceptions/PowerApiException.php create mode 100644 src/PowerClient.php diff --git a/src/Enums/HttpMethod.php b/src/Enums/HttpMethod.php new file mode 100644 index 0000000..511b0bc --- /dev/null +++ b/src/Enums/HttpMethod.php @@ -0,0 +1,12 @@ +client = new Client([ + ...$options, + 'http_errors' => false, + 'base_uri' => trim($apiUrl, " \t\n\r\0\x0B\/").'/api/', + ]); + } + + /** + * @param HttpMethod $method + * @param string $path + * @param array $payload + * @return array + * @throws PowerApiException + */ + protected function request(HttpMethod $method, string $path, array $payload = []): array + { + try { + $response = $this->client->request($method->value, $path, [ + $method === HttpMethod::GET ? 'query' : 'json' => $payload, + ]); + } catch (\Throwable $e) { + throw new PowerApiException($e->getMessage(), $e->getCode()); + } + + $data = json_decode($response->getBody()->getContents(), true); + + if (empty($data[ 'ok' ]) || $data[ 'ok' ] !== true) { + throw new PowerApiException($data[ 'msg' ], $data[ 'code' ]); + } + + return $data; + } + + /** + * @return bool + * @throws PowerApiException + */ + public function status(): bool + { + $data = $this->request(HttpMethod::GET, 'status'); + + return (bool) $data[ 'ok' ]; + } + + /** + * @return array + * @throws PowerApiException + */ + public function nodeStatus(): array + { + $data = $this->request(HttpMethod::GET, 'node/status'); + + return $data[ 'status' ]; + } + + /** + * @return array + * @throws PowerApiException + */ + public function settings(): array + { + $data = $this->request(HttpMethod::GET, 'settings'); + + return $data[ 'settings' ]; + } + + /** + * @param string $hash + * @return array + * @throws PowerApiException + */ + public function blockInfo(string $hash): array + { + $data = $this->request(HttpMethod::GET, 'blockinfo/'.$hash); + + return $data[ 'block' ]; + } + + /** + * @param string $hash + * @return array + * @throws PowerApiException + */ + public function block(string $hash): array + { + $data = $this->request(HttpMethod::GET, 'block/'.$hash); + + return $data[ 'block' ]; + } + + /** + * @param string $address + * @return array + * @throws PowerApiException + */ + public function where(string $address): array + { + return $this->request(HttpMethod::GET, 'where/'.$address); + } + + /** + * @param string $address + * @return array + * @throws PowerApiException + */ + public function address(string $address): array + { + return $this->request(HttpMethod::GET, 'address/'.$address); + } +} \ No newline at end of file From 6fd91441e04345e734fac13a304d8f2375723e90 Mon Sep 17 00:00:00 2001 From: Artur Khylskyi Date: Mon, 14 Nov 2022 17:46:40 +0200 Subject: [PATCH 2/2] Add readme --- README.md | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..797a905 --- /dev/null +++ b/README.md @@ -0,0 +1,88 @@ + +# ThePower.io SDK for PHP + +Production ready SDK for ThePower.io API + + +## Installation + +### Install via composer + +```bash + composer require arthurpatriot/thepower-sdk +``` + +### Create new API Client + +```php +use ThePower\PowerClient; + +$client = new PowerClient('{NODE_URL}'); +``` + +Example, `{NODE_URL}` is `https://power-node.allsteeply.com:1443`. + +## Usage + +### Get Status + +```php +$client->status(); +``` + +### Get Node Status + +Get current status of the addressed node + +```php +$client->nodeStatus(); +``` + +### Get Settings + +Get current chain parameters + +```php +$client->settings(); +``` + +### Get Block Info + +Get information about the block without transactions + +```php +$client->blockInfo('{HASH}'); +``` +Where `{HASH}` is hash of the block for which the information is needed. + +### Get Block + +Get information about the block + +```php +$client->block('{HASH}'); +``` +Where `{HASH}` hash of the block for which the information is needed. + +### Where Address + +The definition of chain belonging to address. + +```php +$client->where('{ADDRESS}'); +``` +Where `{ADDRESS}` is the address of the wallet in textual or binary representation in hex format. + +### Get Address Info + +Information about a wallet with a given address. + +```php +$client->address('{ADDRESS}'); +``` +Where `{ADDRESS}` is the address of the wallet in textual or binary representation in hex format. +## Acknowledgements + + - [ThePower.io API Reference](https://doc.thepower.io/docs/Build/api/api-reference) + - [ThePower.io About](https://thepower.io) +