From 2e58c43668f28a3e5a86270c0e95754d22c9d9c3 Mon Sep 17 00:00:00 2001 From: Roman Bylbas Date: Wed, 29 Nov 2023 11:49:20 +0200 Subject: [PATCH] Replaced bearer token by headers property at RPC client Signed-off-by: Roman Bylbas --- README.md | 6 +++--- docs/API/RpcClientAPI.md | 10 +++++----- src/Rpc/RpcClient.php | 10 +++++----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index d7f0793..f9c2fbf 100644 --- a/README.md +++ b/README.md @@ -31,12 +31,12 @@ extension=secp256k1.so ## Usage ### Creating RpcClient -Create `RpcClient` by passing node url and authorization token (optional) to constructor +Create `RpcClient` by passing node url and headers (optional) to constructor ```php $nodeUrl = 'http://127.0.0.1:7777'; -$bearerToken = '6ae6c8b31f09df244019ffef60c274e4'; // Optional +$headers = array('Authorization' => 'Bearer 6ae6c8b31f09df244019ffef60c274e4'); // Optional -$client = new Casper\Rpc\RpcClient($nodeUrl, $bearerToken); +$client = new Casper\Rpc\RpcClient($nodeUrl, $headers); ``` ### RPC call examples diff --git a/docs/API/RpcClientAPI.md b/docs/API/RpcClientAPI.md index b5b6567..25b7aa3 100644 --- a/docs/API/RpcClientAPI.md +++ b/docs/API/RpcClientAPI.md @@ -4,13 +4,13 @@ Class for interacting with the network via RPC --- ## Constructor ```php -__constructor(string $nodeUrl, string $bearerToken = null) +__constructor(string $nodeUrl, string $headers = null) ``` ### Parameters -| Name | Type | Description | Required | -|----------------|---|----------------------------|----------| -| `$nodeUrl` | `string` | Full node url string | Yes | -| `$bearerToken` | `string` | Authorization bearer token | No | +| Name | Type | Description | Required | +|------------|----------|----------------------|----------| +| `$nodeUrl` | `string` | Full node url string | Yes | +| `$headers` | `array` | Additional headers | No | --- ## Put deploy diff --git a/src/Rpc/RpcClient.php b/src/Rpc/RpcClient.php index 20e6dde..d472785 100644 --- a/src/Rpc/RpcClient.php +++ b/src/Rpc/RpcClient.php @@ -67,14 +67,14 @@ class RpcClient private string $nodeUrl; - private ?string $bearerToken = null; + private array $headers; private ?string $lastApiVersion = null; - public function __construct(string $nodeUrl, string $bearerToken = null) + public function __construct(string $nodeUrl, array $headers = array()) { $this->nodeUrl = $nodeUrl; - $this->bearerToken = $bearerToken; + $this->headers = $headers; } public function getLastApiVersion(): ?string @@ -466,8 +466,8 @@ private function rpcCallMethod(string $method, array $params = array()): array $curl = curl_init($url); $headers = ['Accept: application/json', 'Content-type: application/json']; - if ($this->bearerToken !== null) { - $headers[] = 'Authorization: Bearer ' . $this->bearerToken; + foreach ($this->headers as $name => $value) { + $headers[] = "$name: $value"; } curl_setopt($curl, CURLOPT_URL, $url);