Module near_jsonrpc_client::auth

source ·
Expand description

Helpers for client authentication.

Some RPC nodes will require authentication before requests can be sent to them.

This module provides the ApiKey and Authorization types that can be used to authenticate requests.

§Example

§API Key (x-api-key Header)

use near_jsonrpc_client::{JsonRpcClient, auth, methods};
use near_primitives::types::{BlockReference, Finality};

let client = JsonRpcClient::connect("https://rpc.testnet.near.org");

let client = client.header(auth::ApiKey::new("399ba741-e939-4ffa-8c3c-306ec36fa8de")?);

let request = methods::block::RpcBlockRequest {
    block_reference: BlockReference::Finality(Finality::Final),
};

let response = client.call(request).await?;

assert!(matches!(response, methods::block::RpcBlockResponse { .. }));

§Authorization Header

use near_jsonrpc_client::{JsonRpcClient, auth, methods};
use near_primitives::types::{BlockReference, Finality};

let client = JsonRpcClient::connect("https://rpc.testnet.near.org")
    .header(auth::Authorization::bearer(
        "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
    )?);

let request = methods::block::RpcBlockRequest {
    block_reference: BlockReference::Finality(Finality::Final),
};

let response = client.call(request).await?;

assert!(matches!(response, methods::block::RpcBlockResponse { .. }));

Structs§

Enums§