-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathstudio_client.rs
45 lines (39 loc) · 1.53 KB
/
studio_client.rs
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
use crate::blocking::Client;
use crate::headers;
use crate::RoverClientError;
use houston::Credential;
use graphql_client::GraphQLQuery;
/// Represents a client for making GraphQL requests to Apollo Studio.
pub struct StudioClient {
pub credential: Credential,
client: reqwest::blocking::Client,
uri: String,
version: String,
}
impl StudioClient {
/// Construct a new [StudioClient] from an `api_key`, a `uri`, and a `version`.
/// For use in Rover, the `uri` is usually going to be to Apollo Studio
pub fn new(credential: Credential, uri: &str, version: &str) -> StudioClient {
StudioClient {
credential,
client: reqwest::blocking::Client::new(),
uri: uri.to_string(),
version: version.to_string(),
}
}
/// Client method for making a GraphQL request.
///
/// Takes one argument, `variables`. Returns an optional response.
pub fn post<Q: GraphQLQuery>(
&self,
variables: Q::Variables,
) -> Result<Q::ResponseData, RoverClientError> {
let h = headers::build_studio_headers(&self.credential.api_key, &self.version)?;
let body = Q::build_query(variables);
tracing::trace!(request_headers = ?h);
tracing::trace!("Request Body: {}", serde_json::to_string(&body)?);
let response = self.client.post(&self.uri).headers(h).json(&body).send()?;
tracing::trace!(response_status = ?response.status(), response_headers = ?response.headers());
Client::handle_response::<Q>(response)
}
}