Create struct for global app state

This commit is contained in:
Kiana Sheibani 2023-09-26 21:37:13 -04:00
parent da6b4a9220
commit c8cb974762
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
3 changed files with 19 additions and 3 deletions

View file

@ -4,6 +4,8 @@ use std::io::{self, Write};
mod queries; mod queries;
use queries::*; use queries::*;
mod state;
use state::*;
mod datasets; mod datasets;
use datasets::*; use datasets::*;
@ -11,7 +13,14 @@ fn main() {
let mut config_dir = dirs::config_dir().unwrap(); let mut config_dir = dirs::config_dir().unwrap();
config_dir.push("ggelo"); config_dir.push("ggelo");
let path = dataset_path(&config_dir, "test"); let auth_token = get_auth_key(&config_dir).unwrap();
// let config = AppState {
// config_dir,
// auth_token,
// };
let path = dataset_path(&config_dir, "test").unwrap();
let connection = open_dataset(&path).unwrap(); let connection = open_dataset(&path).unwrap();
let set_data = SetData { let set_data = SetData {

View file

@ -9,6 +9,7 @@ pub use tournament_sets::*;
pub mod player_info; pub mod player_info;
pub use player_info::*; pub use player_info::*;
use crate::state::*;
use schema::schema; use schema::schema;
// Auth key // Auth key
@ -65,7 +66,7 @@ pub trait QueryUnwrap<Vars>: 'static + QueryBuilder<Vars> {
} }
// Generic function for running start.gg queries // Generic function for running start.gg queries
pub fn run_query<Builder, Vars>(vars: Vars, auth: &str) -> Option<Builder::Unwrapped> pub fn run_query<Builder, Vars>(vars: Vars, state: &AppState) -> Option<Builder::Unwrapped>
where where
Builder: QueryUnwrap<Vars>, Builder: QueryUnwrap<Vars>,
Vars: Serialize, Vars: Serialize,
@ -77,7 +78,7 @@ where
let response = reqwest::blocking::Client::new() let response = reqwest::blocking::Client::new()
.post("https://api.start.gg/gql/alpha") .post("https://api.start.gg/gql/alpha")
.header("Authorization", String::from("Bearer ") + auth) .header("Authorization", String::from("Bearer ") + &state.auth_token)
.run_graphql(query); .run_graphql(query);
Builder::unwrap_response(response.unwrap()) Builder::unwrap_response(response.unwrap())

6
src/state.rs Normal file
View file

@ -0,0 +1,6 @@
use std::path::PathBuf;
pub struct AppState {
pub config_dir: PathBuf,
pub auth_token: String,
}