Skip to content

Commit

Permalink
Merge pull request #24 from make-software/authorization-header
Browse files Browse the repository at this point in the history
Added headers to RpcClient
  • Loading branch information
RomanovSci authored Nov 29, 2023
2 parents edd46dc + b975e72 commit 729833b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ jobs:
- name: Run test suite
run: composer run-script test
env:
CASPER_PHP_SDK_TEST_NODE_URL: "65.21.237.160:7777"
CASPER_PHP_SDK_TEST_NODE_URL: "88.99.100.42:7777"
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ extension=secp256k1.so

## Usage
### Creating RpcClient
Create `RpcClient` by passing node url to constructor
Create `RpcClient` by passing node url and headers (optional) to constructor
```php
$nodeUrl = 'http://127.0.0.1:7777';
$client = new Casper\Rpc\RpcClient($nodeUrl);
$headers = array('Authorization' => 'Bearer 6ae6c8b31f09df244019ffef60c274e4'); // Optional

$client = new Casper\Rpc\RpcClient($nodeUrl, $headers);
```

### RPC call examples
Expand Down
9 changes: 5 additions & 4 deletions docs/API/RpcClientAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ Class for interacting with the network via RPC
---
## Constructor
```php
__constructor(string $nodeUrl)
__constructor(string $nodeUrl, array $headers = array())
```
### Parameters
| Name | Type | Description | Required |
|---|---|---|---|
| `$nodeUrl` | `string` | Full node url string | Yes |
| Name | Type | Description | Required |
|------------|----------|----------------------|----------|
| `$nodeUrl` | `string` | Full node url string | Yes |
| `$headers` | `array` | Additional headers | No |

---
## Put deploy
Expand Down
28 changes: 17 additions & 11 deletions src/Rpc/RpcClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,14 @@ class RpcClient

private string $nodeUrl;

private array $headers;

private ?string $lastApiVersion = null;

public function __construct(string $nodeUrl)
public function __construct(string $nodeUrl, array $headers = array())
{
$this->nodeUrl = $nodeUrl;
$this->headers = $headers;
}

public function getLastApiVersion(): ?string
Expand Down Expand Up @@ -461,20 +464,23 @@ private function rpcCallMethod(string $method, array $params = array()): array
{
$url = $this->nodeUrl . '/rpc';
$curl = curl_init($url);
$data = array(
'id' => self::ID,
'jsonrpc' => self::JSON_RPC,
'method' => $method,
'params' => $params
);

$headers = ['Accept: application/json', 'Content-type: application/json'];
foreach ($this->headers as $name => $value) {
$headers[] = "$name: $value";
}

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-type: application/json',
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(
array(
'id' => self::ID,
'jsonrpc' => self::JSON_RPC,
'method' => $method,
'params' => $params
)
));

$response = curl_exec($curl);
Expand Down

0 comments on commit 729833b

Please sign in to comment.