From 99217eedb67896beb2911218c935258e80df9848 Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Sun, 27 Aug 2023 03:02:18 -0400 Subject: [PATCH] Write basic API call --- Cargo.lock | 2 ++ cli/Cargo.toml | 2 ++ cli/src/main.rs | 58 ++++++++++++++++++++++++++++++--- cli/src/queries.rs | 2 +- cli/src/queries/search_games.rs | 4 +-- 5 files changed, 60 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d65c298..d629597 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -300,6 +300,8 @@ version = "0.1.0" dependencies = [ "cynic", "cynic-codegen", + "dirs", + "futures", "schema", "surf", ] diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 20c2e1b..48ec65c 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -11,6 +11,8 @@ path = "src/main.rs" schema.path = "../schema" cynic = { version = "3.2", features = ["http-surf"] } surf = "2" +dirs = "5.0" +futures = "0.3" [build-dependencies] cynic-codegen = "3.2" diff --git a/cli/src/main.rs b/cli/src/main.rs index d87dca4..5773ab1 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,13 +1,61 @@ -use std::env; +use std::io::{self, Write}; +use std::path::Path; +use futures::executor::block_on; mod queries; -use queries::*; +use queries::search_games::{VideogameSearch, VideogameSearchVars}; -fn get_auth_key() -> Option { - env::var("AUTH_KEY").ok() +fn get_auth_key(config_dir: &Path) -> Option { + use std::env::{var, VarError}; + use std::fs::read_to_string; + + match var("AUTH_KEY") { + Ok(key) => Some(key), + Err(VarError::NotUnicode(_)) => panic!("Invalid authorization key"), + Err(VarError::NotPresent) => { + let mut auth_file = config_dir.to_owned(); + auth_file.push("auth.txt"); + read_to_string(auth_file) + .ok() + .and_then(|s| s.split_whitespace().next().map(String::from)) + } + } +} + +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 _auth_key = get_auth_key().expect("Could not find authorization key"); + let mut config_dir = dirs::config_dir().unwrap(); + config_dir.push("ggelo"); + let auth_key = get_auth_key(&config_dir).expect("Could not find authorization key"); + + // Get search prompt + let mut search = String::new(); + print!("Search for game: "); + 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 { + for maybe_game in response.videogames.unwrap().nodes.unwrap().into_iter() { + let game = maybe_game.unwrap(); + println!("{:?} - {}", game.id.unwrap(), game.name.unwrap()); + } + } else { + println!("No response"); + } } diff --git a/cli/src/queries.rs b/cli/src/queries.rs index a4f64ec..a6af99c 100644 --- a/cli/src/queries.rs +++ b/cli/src/queries.rs @@ -1,2 +1,2 @@ -mod search_games; +pub mod search_games; diff --git a/cli/src/queries/search_games.rs b/cli/src/queries/search_games.rs index e6d4033..e08c055 100644 --- a/cli/src/queries/search_games.rs +++ b/cli/src/queries/search_games.rs @@ -5,7 +5,7 @@ use schema::schema; #[derive(cynic::QueryVariables)] pub struct VideogameSearchVars { - name: String + pub name: String } // QUERY @@ -24,6 +24,6 @@ pub struct VideogameConnection { #[derive(cynic::QueryFragment, Debug)] pub struct Videogame { - pub id: Option, + pub id: Option pub name: Option, }