1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use crate::api::handle_graphql_response;
use crate::api::rcos::auth::ApiJwtClaims;
use crate::env::global_config;
use crate::error::TelescopeError;
use graphql_client::{GraphQLQuery, QueryBody, Response as GraphQlResponse};
use reqwest::{header::HeaderValue, header::ACCEPT, Client};
use serde_json::Value;
mod auth;
pub mod discord_assoications;
pub mod landing_page_stats;
pub mod meetings;
pub mod prelude;
pub mod projects;
pub mod search_strings;
pub mod semesters;
pub mod users;
const API_NAME: &'static str = "RCOS Central Hasura GraphQL API";
pub async fn send_query<T: GraphQLQuery>(
variables: T::Variables,
) -> Result<T::ResponseData, TelescopeError> {
let query = T::build_query(variables);
let QueryBody {
operation_name,
query,
variables,
} = query;
let variables: Value = serde_json::to_value(variables).map_err(|e| {
TelescopeError::ise(format!(
"Could not serialize GraphQL variables to JSON object: {}",
e
))
})?;
let response: Value = send_json_query(operation_name, query, variables).await?;
serde_json::from_value::<T::ResponseData>(response).map_err(|e| {
TelescopeError::ise(format!("Could not deserialize GraphQL API response: {}", e))
})
}
pub async fn send_json_query(
query_name: &str,
query_document: &str,
variables: Value,
) -> Result<Value, TelescopeError> {
let request_body: Value = json!({
"query": query_document,
"operationName": query_name,
"variables": variables
});
let jwt: String = ApiJwtClaims::new(None);
return Client::new()
.post(global_config().api_url.as_str())
.json(&request_body)
.bearer_auth(jwt)
.header(ACCEPT, HeaderValue::from_static("application/json"))
.send()
.await
.map_err(TelescopeError::rcos_api_error)?
.text()
.await
.map_err(TelescopeError::rcos_api_error)
.and_then(|body| {
serde_json::from_str::<GraphQlResponse<Value>>(body.as_str())
.map_err(|err| {
error!(
"Error querying RCOS API: {}\nresponse body: {}",
err,
body.as_str()
);
TelescopeError::RcosApiError(err.to_string())
})
})
.and_then(|response| handle_graphql_response(API_NAME, response));
}