aliyun-openapi-core-rust-sdk
Aliyun OpenAPI POP core SDK for Rust.
Notes
You must know your AK
(accessKeyId/accessKeySecret
), and the aliyun product's endpoint
and apiVersion
.
For example, The ECS OpenAPI, the API version is 2014-05-26
.
And the endpoint list can be found at here, the center endpoint is ecs.aliyuncs.com. Add http protocol http
or https
, should be https://ecs.aliyuncs.com/
.
Install
Run the following Cargo command in your project directory:
cargo add aliyun-openapi-core-rust-sdk
Or add the following line to your Cargo.toml:
aliyun-openapi-core-rust-sdk = "1.1.0"
Usage
The RPC style client:
use std::collections::HashMap;
use std::env;
use std::error::Error;
use aliyun_openapi_core_rust_sdk::client::rpc::RPClient;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct Region {
region_id: String,
region_endpoint: String,
local_name: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct RegionList {
request_id: String,
regions: HashMap<String, Vec<Region>>,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let aliyun_openapi_client = RPClient::new(
env::var("ACCESS_KEY_ID")?,
env::var("ACCESS_KEY_SECRET")?,
"https://ecs.aliyuncs.com/",
);
let response = aliyun_openapi_client
.clone()
.version("2014-05-26")
.get("DescribeRegions")
.json::<RegionList>()
.await?;
println!("DescribeRegions response: {response:#?}");
let response = aliyun_openapi_client
.version("2014-05-26")
.get("DescribeInstances")
.query([("RegionId", "cn-hangzhou")])
.text()
.await?;
println!("DescribeInstances response: {response}");
Ok(())
}
The ROA style client:
use std::collections::HashMap;
use std::env;
use std::error::Error;
use aliyun_openapi_core_rust_sdk::client::roa::ROAClient;
use serde::{Deserialize, Serialize};
use serde_json::json;
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct TranslateData {
word_count: String,
translated: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct Translate {
request_id: String,
data: TranslateData,
code: String,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let aliyun_openapi_client = ROAClient::new(
env::var("ACCESS_KEY_ID")?,
env::var("ACCESS_KEY_SECRET")?,
"http://mt.aliyuncs.com",
);
let mut params = HashMap::new();
params.insert("SourceText", "你好");
params.insert("SourceLanguage", "zh");
params.insert("TargetLanguage", "en");
params.insert("FormatType", "text");
params.insert("Scene", "general");
let response = aliyun_openapi_client
.version("2018-04-08")
.post("/api/translate/web/general")
.header([("Content-Type".to_string(), "application/json".to_string())])?
.body(json!(params).to_string())?
.json::<Translate>()
.await?;
println!("Translate response: {response:#?}");
Ok(())
}
Examples
Export AK info to env, then run cargo run --example <NAME>
:
export ACCESS_KEY_ID=<access_key_id>
export ACCESS_KEY_SECRET=<access_key_secret>
cargo run --example ecs
cargo run --example rds
cargo run --example slb
cargo run --example vpc
cargo run --example log_service
License
The MIT License