-
Notifications
You must be signed in to change notification settings - Fork 4
Merge async client #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DukeFerdinand
wants to merge
4
commits into
DukeFerdinand:master
Choose a base branch
from
gibsorya:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+95
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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." | ||
|
|
@@ -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"] } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| 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) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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?)?; | ||||||
|
||||||
| let data: Value = serde_json::from_str(&reqwest_res.text().await?)?; | |
| let data: Value = reqwest_res.json().await?; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| mod config; | ||
| mod helpers; | ||
| pub use config::*; | ||
| pub use helpers::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.