Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sanity"
version = "0.1.1"
version = "0.2.3"
authors = ["Douglass Hooker <[email protected]>"]
edition = "2018"
description = "The open source client for consuming https://sanity.io with Rust, based on reqwest."
Expand All @@ -14,3 +14,4 @@ readme = "README.md"
reqwest = { version = "0.11", features = ["blocking", "json"] }
serde = "1.0.193"
serde_json = "1.0.108"
tokio = { version = "1.38.1", features = ["full"] }
79 changes: 79 additions & 0 deletions src/async_client/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use reqwest::{Client, Error, StatusCode};
use serde_json::Value;
use std::{error, io::ErrorKind, time::Duration};
use tokio::time::sleep;

const MAX_RETRIES: u32 = 5;
const INITIAL_BACKOFF: Duration = Duration::from_secs(1);

#[derive(Debug, Clone)]
pub struct SanityConfig {
project_id: String,
access_token: String,
data_set: String,
url: String,
pub query: Query,
}

#[derive(Debug, Clone)]
pub struct Query {
base_url: String,
pub query: Option<String>,
}

pub fn create(project_id: &str, data_set: &str, token: &str, use_prod: bool) -> SanityConfig {
SanityConfig {
project_id: project_id.to_string(),
access_token: token.to_string(),
data_set: data_set.to_string(),
url: get_url(project_id, data_set),
query: Query {
base_url: format!("https://{}.api.sanity.io/v1/data/query/{}", project_id, data_set),
query: None,
},
}
}

pub fn get_url(project_id: &str, data_set: &str) -> String {
format!(
"https://{}.api.sanity.io/v1/data/query/{}",
project_id, data_set
)
}

impl Query {
pub async fn execute(&self) -> Result<Value, Box<dyn std::error::Error>> {
let url = format!("{} {}", &self.base_url, &self.query.as_ref().unwrap());
Copy link

Copilot AI May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concatenating the base URL and query with a space may lead to an improperly formatted URL. Consider using proper query parameter formatting (e.g., using '?' or '&' as appropriate) and ensuring the query is correctly appended.

Copilot uses AI. Check for mistakes.
let res: _ = reqwest::get(&url).await?.text().await?;
let data: Value = serde_json::from_str(&res)?;
Ok(data)
}
}

impl SanityConfig {
pub async fn build_url(&mut self, query: Option<&str>) -> String {
match query {
Some(query) => format!("{}?query={}", self.query.base_url, query),
None => format!(
"{}?query={}",
self.query.base_url,
self.query.query.as_ref().unwrap()
),
}
}

pub async fn get(&mut self, query: &str) -> Result<reqwest::Response, reqwest::Error> {
let client: Client = reqwest::Client::new();
let url = self.build_url(Some(query)).await;
println!("URL: {}", url);

// TODO: Add support for retries
let res = client
.get(&url)
.bearer_auth(&self.access_token)
.send()
.await?;

Ok(res)
}
}
9 changes: 9 additions & 0 deletions src/async_client/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use serde_json::Value;

pub async fn get_json(
reqwest_res: reqwest::Response,
) -> Result<Value, Box<dyn std::error::Error>> {
let data: Value = serde_json::from_str(&reqwest_res.text().await?)?;
Copy link

Copilot AI May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Instead of converting the response text to JSON manually, it might be clearer and more efficient to use reqwest's built-in JSON deserialization with reqwest_res.json().await.

Suggested change
let data: Value = serde_json::from_str(&reqwest_res.text().await?)?;
let data: Value = reqwest_res.json().await?;

Copilot uses AI. Check for mistakes.

Ok(data)
}
4 changes: 4 additions & 0 deletions src/async_client/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod config;
mod helpers;
pub use config::*;
pub use helpers::*;
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate serde;
extern crate serde_json;

pub mod helpers;
pub mod async_client;

use reqwest::blocking::Client;
use reqwest::header::{HeaderMap, HeaderValue};
Expand Down