1 个不稳定版本
0.1.0 | 2022年5月25日 |
---|
#4 in #sanity
21KB
294 行
Rust for Sanity.io 客户端
Rust for Sanity.io 客户端是一个简单的客户端,用于查询、修改并将图像上传到 Sanity 数据集。
[dependencies]
sanity_rs_client = "0.1.0"
完整文档请访问 此处
使用方法
创建客户端
use sanity_rs_client::config::SanityConfig;
let config = SanityConfig::new("<project-id>", "<dataset-name>")
.api_version("<api-version>")
.access_token("<access-token>")
.build();
let client = sanity_rs_client::sanity_client::SanityClient::new(config);
简单查询
客户端使用
tokio
运行时进行数据查询
use sanity_rs_client::sanity_client::SanityClient;
use sanity_rs_client::sanity_client::Query;
let query_str = String::from("*[_type == "image"]{_id, _type, ...}");
let response = client.fetch(Query::new(query_str, HashMap::new())).unwrap().await;
let json_text = response.text().await;
带有变量的查询
use sanity_rs_client::sanity_client::SanityClient;
use sanity_rs_client::sanity_client::Query;
use serde_json::Value;
let query_str = String::from("*[_type == $type]{_id, _type, ...}");
let variables: HashMap<String, Value> = HashMap::new();
variables.insert(String::from("type"), Value::String(String::from("file")));
let query = Query::new(query_str, variables);
let response = client.fetch(query).unwrap().await;
简单修改
use sanity_rs_client::sanity_client::Mutation;
let mutation = Mutation::Create(json!{
"_id": "drafts.cfeba160-1123-4af9-ad4e-c657d5e537af",
"_type": "author",
"name": "Random"
});
let response = client.mutate(vec![mutation], &Vec::new()).await.unwrap();
let json_text = response.text().await;
带有参数的修改
use sanity_rs_client::sanity_client::Mutation;
let mutation = Mutation::Create(json!{
"_id": "drafts.cfeba160-1123-4af9-ad4e-c657d5e537af",
"_type": "author",
"name": "Random"
});
// Query vector is required as per `reqwest` crate's requirements for providing
// query params in an http request
let query: Vec<(String, Value)> = vec![
(String::from("returnIds"), Value::Bool(true)),
(String::from("returnDocuments"), Value::Bool(true)),
(String::from("dryRun"), Value::Bool(true))
];
let response = client.mutate(vec![mutation], &query).await.unwrap();
//To get json text
let json_text = response.text().await;
上传图像
由于无法像 post 和 get 请求一样在 tokio 中执行
reqwest
上传,因此当前在异步上下文中上传图像不起作用,任何帮助/PR 都将受到欢迎
let response = client.upload_image(String::from("image.jpg"));
示例
要运行示例,请运行
cargo run --example <example-name>
依赖关系
~6–19MB
~278K SLoC