Skip to content

Commit

Permalink
chore: update RPC & ROA client example
Browse files Browse the repository at this point in the history
  • Loading branch information
r4ntix committed Jul 4, 2023
1 parent b2ab451 commit 9a32079
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 19 deletions.
48 changes: 40 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,27 @@ aliyun-openapi-core-rust-sdk = "1.0.0"
The RPC style client:

```rust
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>> {
Expand All @@ -48,16 +65,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
"https://ecs.aliyuncs.com/",
);

// call `DescribeRegions` with empty queries.
// call `DescribeRegions` with empty queries, return `RegionList`
let response = aliyun_openapi_client
.clone()
.version("2014-05-26")
.get("DescribeRegions")
.text()
.json::<RegionList>()
.await?;
println!("DescribeRegions response: {response}");
println!("DescribeRegions response: {response:#?}");

// call `DescribeInstances` with queries.
// call `DescribeInstances` with queries, return `String`
let response = aliyun_openapi_client
.version("2014-05-26")
.get("DescribeInstances")
Expand All @@ -68,7 +85,6 @@ async fn main() -> Result<(), Box<dyn Error>> {

Ok(())
}

```

The ROA style client:
Expand All @@ -79,8 +95,24 @@ 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>> {
// create roa style api client.
Expand All @@ -98,16 +130,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
params.insert("FormatType", "text");
params.insert("Scene", "general");

// call `Translate` with json params.
// call `Translate` with json params, return `Translate`
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())?
.text()
.json::<Translate>()
.await?;

println!("Translate response: {response}");
println!("Translate response: {response:#?}");

Ok(())
}
Expand Down
30 changes: 24 additions & 6 deletions examples/ecs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
use aliyun_openapi_core_rust_sdk::client::rpc::RPClient;
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>> {
// create rpc style api client.
Expand All @@ -11,23 +29,23 @@ async fn main() -> Result<(), Box<dyn Error>> {
"https://ecs.aliyuncs.com/",
);

// call `DescribeRegions` with empty queries.
// call `DescribeRegions` with empty queries, return `RegionList`
let response = aliyun_openapi_client
.clone()
.version("2014-05-26")
.get("DescribeRegions")
.text()
.json::<RegionList>()
.await?;
println!("DescribeRegions response:\n{response}\n");
println!("DescribeRegions response: {response:#?}");

// call `DescribeInstances` with queries.
// call `DescribeInstances` with queries, return `String`
let response = aliyun_openapi_client
.version("2014-05-26")
.get("DescribeInstances")
.query([("RegionId", "cn-hangzhou")])
.text()
.await?;
println!("DescribeInstances response:\n{response}");
println!("DescribeInstances response: {response}");

Ok(())
}
27 changes: 22 additions & 5 deletions examples/nlp.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
use aliyun_openapi_core_rust_sdk::client::roa::ROAClient;
use serde_json::json;
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>> {
// create roa style api client.
Expand All @@ -21,16 +38,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
params.insert("FormatType", "text");
params.insert("Scene", "general");

// call `Translate` with json params.
// call `Translate` with json params, return `Translate`
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())?
.text()
.json::<Translate>()
.await?;

println!("Translate response:\n{response}");
println!("Translate response: {response:#?}");

Ok(())
}

0 comments on commit 9a32079

Please sign in to comment.