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
use crate::api::handle_graphql_response;
use crate::error::TelescopeError;
use crate::web::telescope_ua;
use graphql_client::{GraphQLQuery, Response as GraphQLResponse};
use oauth2::AccessToken;
use reqwest::header::{HeaderValue, ACCEPT, USER_AGENT};
use reqwest::Client;
pub mod users;
const GITHUB_API_ENDPOINT: &'static str = "https://api.github.com/graphql";
const API_NAME: &'static str = "GitHub API V4";
pub async fn send_query<T: GraphQLQuery>(
auth_token: &AccessToken,
variables: T::Variables,
) -> Result<T::ResponseData, TelescopeError> {
let query = T::build_query(variables);
return Client::new()
.post(GITHUB_API_ENDPOINT)
.json(&query)
.bearer_auth(auth_token.secret())
.header(ACCEPT, HeaderValue::from_static("application/json"))
.header(USER_AGENT, telescope_ua())
.send()
.await
.map_err(TelescopeError::github_api_error)?
.text()
.await
.map_err(TelescopeError::github_api_error)
.and_then(|body| {
serde_json::from_str::<GraphQLResponse<T::ResponseData>>(body.as_str())
.map_err(|err| {
error!(
"Malformed GitHub API response: {}\nresponse body: {}",
err,
body.as_str()
);
TelescopeError::GitHubApiError(err.to_string())
})
})
.and_then(|response| handle_graphql_response(API_NAME, response));
}