Skip to content

Commit 729833b

Browse files
authored
Merge pull request #24 from make-software/authorization-header
Added headers to RpcClient
2 parents edd46dc + b975e72 commit 729833b

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ jobs:
4747
- name: Run test suite
4848
run: composer run-script test
4949
env:
50-
CASPER_PHP_SDK_TEST_NODE_URL: "65.21.237.160:7777"
50+
CASPER_PHP_SDK_TEST_NODE_URL: "88.99.100.42:7777"

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ extension=secp256k1.so
3131

3232
## Usage
3333
### Creating RpcClient
34-
Create `RpcClient` by passing node url to constructor
34+
Create `RpcClient` by passing node url and headers (optional) to constructor
3535
```php
3636
$nodeUrl = 'http://127.0.0.1:7777';
37-
$client = new Casper\Rpc\RpcClient($nodeUrl);
37+
$headers = array('Authorization' => 'Bearer 6ae6c8b31f09df244019ffef60c274e4'); // Optional
38+
39+
$client = new Casper\Rpc\RpcClient($nodeUrl, $headers);
3840
```
3941

4042
### RPC call examples

docs/API/RpcClientAPI.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ Class for interacting with the network via RPC
44
---
55
## Constructor
66
```php
7-
__constructor(string $nodeUrl)
7+
__constructor(string $nodeUrl, array $headers = array())
88
```
99
### Parameters
10-
| Name | Type | Description | Required |
11-
|---|---|---|---|
12-
| `$nodeUrl` | `string` | Full node url string | Yes |
10+
| Name | Type | Description | Required |
11+
|------------|----------|----------------------|----------|
12+
| `$nodeUrl` | `string` | Full node url string | Yes |
13+
| `$headers` | `array` | Additional headers | No |
1314

1415
---
1516
## Put deploy

src/Rpc/RpcClient.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,14 @@ class RpcClient
6767

6868
private string $nodeUrl;
6969

70+
private array $headers;
71+
7072
private ?string $lastApiVersion = null;
7173

72-
public function __construct(string $nodeUrl)
74+
public function __construct(string $nodeUrl, array $headers = array())
7375
{
7476
$this->nodeUrl = $nodeUrl;
77+
$this->headers = $headers;
7578
}
7679

7780
public function getLastApiVersion(): ?string
@@ -461,20 +464,23 @@ private function rpcCallMethod(string $method, array $params = array()): array
461464
{
462465
$url = $this->nodeUrl . '/rpc';
463466
$curl = curl_init($url);
464-
$data = array(
465-
'id' => self::ID,
466-
'jsonrpc' => self::JSON_RPC,
467-
'method' => $method,
468-
'params' => $params
469-
);
467+
468+
$headers = ['Accept: application/json', 'Content-type: application/json'];
469+
foreach ($this->headers as $name => $value) {
470+
$headers[] = "$name: $value";
471+
}
470472

471473
curl_setopt($curl, CURLOPT_URL, $url);
472474
curl_setopt($curl, CURLOPT_POST, true);
473475
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
474-
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
475-
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
476-
'Accept: application/json',
477-
'Content-type: application/json',
476+
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
477+
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(
478+
array(
479+
'id' => self::ID,
480+
'jsonrpc' => self::JSON_RPC,
481+
'method' => $method,
482+
'params' => $params
483+
)
478484
));
479485

480486
$response = curl_exec($curl);

0 commit comments

Comments
 (0)