#json-rpc-client #json-rpc #rpc-client #rpc #json #client

jsonrpc_v2_client

基于async-std的JSONRPC客户端

2个版本

0.2.1 2021年12月27日
0.2.0 2021年12月15日
0.1.0 2021年12月15日

#23 in #json-rpc-client

Apache-2.0

12KB
148

JSONRPC v2客户端

特性

  • 日志记录
  • async-std运行时
  • APIKey选项(作为HTTP头发送)

示例

let service_address = jsonrpc_v2_client::ServiceAddress::new("127.0.0.1:8082", "/api");
let method = "add";
let params = jsonrpc_v2_client::Params([10.5, 20.5]);
let id = "0";
let request = jsonrpc_v2_client::Request::new(method, params, id);
// Without API KEY
let response = request.send(&service_address, None);
println!("{}", response);
// Or with API KEY
let api_key = jsonrpc_v2_client::APIKey::new("API-KEY", "abcdef123456");
let response = request.send(&service_address, Some(&api_key));
println!("{}", response);
// Access JSON field
println!("{}", response["result"]);
println!("{}", response["error"]);
println!("{}", response["id"]);

日志记录

日志目标名称为:jsonrpc_v2_client
使用log4rs的示例

文件:config.yaml

# Scan this file for changes every x seconds
refresh_rate: 30 seconds

appenders:
  stdout:
    kind: file
    path: "log/logs/stdout.log"
    encoder:
      pattern: "{d} - {m}{n}"

  jsonrpc_v2_client:
    kind: file
    path: "log/logs/jsonrpc_v2_client.log"
    encoder:
      pattern: "{d} - {m}{n}"

root:
  level: info
  appenders:
    - stdout

  jsonrpc_v2_client:
    level: trace
    appenders:
      - jsonrpc_v2_client
    additive: false

日志输出示例

2021-12-15T14:58:35.293410700+01:00 - [jsonrpc_v2_client: request as string]
POST /api HTTP/1.1
Content-Type: application/json
User-Agent: jsonrpc_v2_client
Accept: application/json
X-API-KEY: 1q2w3e4r5t
Content-Length: 100

{
  "jsonrpc": "2.0",
  "method": "mul",
  "params": [
    10.3,
    10.1,
    12.2
  ],
  "id": 0
}
2021-12-15T14:58:35.294005600+01:00 - [jsonrpc_v2_client: sending request]
2021-12-15T14:58:35.294470100+01:00 - [jsonrpc_v2_client: request successfully sent]
2021-12-15T14:58:35.294679+01:00 - [jsonrpc_v2_client: reading response]
2021-12-15T14:58:35.297373900+01:00 - [jsonrpc_v2_client: received response of len = 183]
2021-12-15T14:58:35.300850900+01:00 - [jsonrpc_v2_client: request as string]

依赖

~5–16MB
~201K SLoC