1 unstable release
0.1.0 | May 25, 2022 |
---|
#5 in #sanity
21KB
294 lines
Rust Client for Sanity.io
Rust Client for Sanity.io is a simple client for sanity.io to query, mutate and upload images to sanity datasets.
[dependencies]
sanity_rs_client = "0.1.0"
Full documentation is available here
Usage
Create a client
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);
Simple Query
The client uses
tokio
runtime for querying data
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;
Query with Variables
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;
Simple Mutation
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;
Mutation with params
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;
Uploading an image
Image upload currently doesn't work in an async context as I couldn't figure out a way to execute
reqwest
upload with tokio like post and get request any help/PR would be appreciated in this regard
let response = client.upload_image(String::from("image.jpg"));
Examples
to run the examples, run
cargo run --example <example-name>
Dependencies
~7–20MB
~267K SLoC