From edcef12c2f26a1b533ba59f903846a9d1e9351f5 Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Sun, 27 Aug 2023 03:52:38 -0400 Subject: [PATCH] Generalize query running function --- cli/src/dataset.rs | 0 cli/src/main.rs | 21 ++++----------------- cli/src/queries.rs | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 17 deletions(-) create mode 100644 cli/src/dataset.rs diff --git a/cli/src/dataset.rs b/cli/src/dataset.rs new file mode 100644 index 0000000..e69de29 diff --git a/cli/src/main.rs b/cli/src/main.rs index 5773ab1..b127bbf 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -3,7 +3,8 @@ use std::path::Path; use futures::executor::block_on; mod queries; -use queries::search_games::{VideogameSearch, VideogameSearchVars}; +use queries::*; +use search_games::{VideogameSearch, VideogameSearchVars}; fn get_auth_key(config_dir: &Path) -> Option { @@ -23,21 +24,6 @@ fn get_auth_key(config_dir: &Path) -> Option { } } -async fn run_query(name: &str, auth: &str) -> cynic::GraphQlResponse { - use cynic::http::SurfExt; - use cynic::QueryBuilder; - - let query = VideogameSearch::build(VideogameSearchVars { - name: String::from(name) - }); - - let response = surf::post("https://api.start.gg/gql/alpha") - .header("Authorization", String::from("Bearer ") + auth) - .run_graphql(query) - .await; - - response.unwrap() -} fn main() { let mut config_dir = dirs::config_dir().unwrap(); @@ -50,7 +36,8 @@ fn main() { let _ = io::stdout().flush(); io::stdin().read_line(&mut search).expect("Error reading from stdin"); - if let Some(response) = block_on(run_query(&search, &auth_key)).data { + if let Some(response) = block_on( + run_query::(VideogameSearchVars { name: search }, &auth_key)).data { for maybe_game in response.videogames.unwrap().nodes.unwrap().into_iter() { let game = maybe_game.unwrap(); println!("{:?} - {}", game.id.unwrap(), game.name.unwrap()); diff --git a/cli/src/queries.rs b/cli/src/queries.rs index d7706cf..4aa838f 100644 --- a/cli/src/queries.rs +++ b/cli/src/queries.rs @@ -1,3 +1,5 @@ +use cynic::QueryBuilder; +use serde::{Serialize, Deserialize}; pub mod search_games; @@ -8,3 +10,22 @@ use schema::schema; /// new scalar type that serializes to u64. #[derive(cynic::Scalar, Debug)] pub struct ID(u64); + + +pub async fn run_query(vars: Vars, auth: &str) -> cynic::GraphQlResponse + where Builder: QueryBuilder, + Vars: Serialize, + for<'de> Builder: Deserialize<'de> + +{ + use cynic::http::SurfExt; + + let query = Builder::build(vars); + + let response = surf::post("https://api.start.gg/gql/alpha") + .header("Authorization", String::from("Bearer ") + auth) + .run_graphql(query) + .await; + + response.unwrap() +}