Table of Contents generated with DocToc
添加如下一段到你的 Cargo.toml
[dependencies]
jdcloud_signer = "0.1"
详细范例参见 client.rs
$ export JDCLOUD_AK="..."
$ export JDCLOUD_SK="..."
$ cargo run --example client
Finished dev [unoptimized + debuginfo] target(s) in 0.37s
Running `target/debug/examples/client`
status: 200 OK
content-type: "application/json; charset=utf-8"
transfer-encoding: "chunked"
connection: "close"
date: "Mon, 22 Jul 2019 09:18:34 GMT"
x-jdcloud-request-id: "bkrovrdrv8ewru46782326noreauvdsf"
x-jdcloud-operationid: "describeInstances"
x-jdcloud-upstream-latency: "310"
x-jdcloud-proxy-latency: "30"
via: "jd-gateway/1.0.1"
requestId: "bkrovrdrv8ewru46782326noreauvdsf"
如果你不喜欢 reqwest
, 准备使用自己的http库,那么可以选择只做签名。
签名时我们会添加如下几个 Header 字段
User-Agent
: 如果未指定,那么设为 "JdcloudSdkRust/0.1.0", 如果已指定,则不做改动。X-Jdcloud-Date
: 当前时间。X-Jdcloud-Nonce
: 随机数。Authorization
: 签名。
添加如下一段到你的 Cargo.toml
[dependencies]
jdcloud_signer = { version = "0.1", default-features = false }
use jdcloud_signer::{Credential, Signer};
use http::Request;
fn main() {
let ak = "...";
let sk = "...";
let credential = Credential::new(ak, sk);
let signer = Signer::new(credential, "vm".to_string(), "cn-north-1".to_string());
let mut req = Request::builder();
let mut req = req.method("GET")
.uri("https://vm.jdcloud-api.com/v1/regions/cn-north-1/instances")
.body("".to_string()).unwrap();
signer.sign_request(&mut req).unwrap();
println!("{}", req);
}
更多调用示例参考 SDK使用Demo