2023-08-27 04:32:55 -04:00
|
|
|
use cynic::{GraphQlResponse, QueryBuilder};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2023-08-26 23:48:13 -04:00
|
|
|
|
2023-08-27 03:02:18 -04:00
|
|
|
pub mod search_games;
|
2023-08-28 00:57:20 -04:00
|
|
|
pub use search_games::*;
|
2023-08-27 16:18:22 -04:00
|
|
|
pub mod tournament_sets;
|
2023-08-28 00:57:20 -04:00
|
|
|
pub use tournament_sets::*;
|
2023-08-27 03:11:23 -04:00
|
|
|
|
|
|
|
use schema::schema;
|
|
|
|
|
2023-08-27 05:33:03 -04:00
|
|
|
// Types
|
|
|
|
|
2023-08-27 04:32:55 -04:00
|
|
|
// HACK: Unfortunately, start.gg seems to use integers for its ID type, whereas
|
|
|
|
// cynic always assumes that IDs are strings. To get around that, we define a
|
|
|
|
// new scalar type that serializes to u64.
|
|
|
|
#[derive(cynic::Scalar, Debug, Copy, Clone)]
|
|
|
|
pub struct ID(pub u64);
|
|
|
|
|
2023-08-27 05:33:03 -04:00
|
|
|
// Wrapper types to differentiate between different types of IDs
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub struct VideogameId(pub u64);
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
2023-08-27 16:18:03 -04:00
|
|
|
pub struct EntrantId(pub u64);
|
2023-08-27 05:33:03 -04:00
|
|
|
|
2023-08-27 16:18:22 -04:00
|
|
|
#[derive(cynic::Scalar, Debug, Clone)]
|
|
|
|
pub struct Timestamp(pub u64);
|
|
|
|
|
2023-08-27 05:33:03 -04:00
|
|
|
// Query machinery
|
2023-08-27 03:52:38 -04:00
|
|
|
|
2023-08-27 16:44:18 -04:00
|
|
|
pub trait QueryUnwrap<Vars>: 'static + QueryBuilder<Vars> {
|
2023-08-27 16:18:03 -04:00
|
|
|
type VarsUnwrapped;
|
2023-08-27 04:32:55 -04:00
|
|
|
type Unwrapped;
|
2023-08-27 03:52:38 -04:00
|
|
|
|
2023-08-27 16:18:03 -04:00
|
|
|
fn wrap_vars(vars: Self::VarsUnwrapped) -> Vars;
|
2023-08-27 04:32:55 -04:00
|
|
|
fn unwrap_response(response: GraphQlResponse<Self>) -> Option<Self::Unwrapped>;
|
|
|
|
}
|
2023-08-27 03:52:38 -04:00
|
|
|
|
2023-08-27 04:32:55 -04:00
|
|
|
// Generic function for running start.gg queries
|
2023-09-02 01:35:36 -04:00
|
|
|
pub fn run_query<Builder, Vars>(
|
2023-08-27 16:18:03 -04:00
|
|
|
vars: Builder::VarsUnwrapped,
|
|
|
|
auth: &str,
|
|
|
|
) -> Option<Builder::Unwrapped>
|
2023-08-27 04:32:55 -04:00
|
|
|
where
|
|
|
|
Builder: QueryUnwrap<Vars>,
|
|
|
|
Vars: Serialize,
|
|
|
|
for<'de> Builder: Deserialize<'de>,
|
2023-08-27 03:52:38 -04:00
|
|
|
{
|
2023-09-02 01:35:36 -04:00
|
|
|
use cynic::http::ReqwestBlockingExt;
|
2023-08-27 03:52:38 -04:00
|
|
|
|
2023-08-27 16:44:18 -04:00
|
|
|
let query = Builder::build(Builder::wrap_vars(vars));
|
2023-08-27 03:52:38 -04:00
|
|
|
|
2023-09-02 01:35:36 -04:00
|
|
|
let response = reqwest::blocking::Client::new()
|
|
|
|
.post("https://api.start.gg/gql/alpha")
|
2023-08-27 03:52:38 -04:00
|
|
|
.header("Authorization", String::from("Bearer ") + auth)
|
2023-09-02 01:35:36 -04:00
|
|
|
.run_graphql(query);
|
2023-08-27 03:52:38 -04:00
|
|
|
|
2023-08-27 16:44:18 -04:00
|
|
|
Builder::unwrap_response(response.unwrap())
|
2023-08-27 03:52:38 -04:00
|
|
|
}
|