Skip to content

Commit

Permalink
feat(client): auto-decode gzipped responsesc
Browse files Browse the repository at this point in the history
  • Loading branch information
EverlastingBugstopper committed Jun 21, 2021
1 parent 9e30be5 commit f282490
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 14 deletions.
38 changes: 38 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/rover-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ graphql-parser = "0.3.0"
graphql_client = "0.9"
http = "0.2"
regex = "1.5.4"
reqwest = {version = "0.11", default-features = false, features = ["json", "blocking", "rustls-tls"]}
reqwest = {version = "0.11", default-features = false, features = ["json", "blocking", "rustls-tls", "gzip"]}
sdl-encoder = {path = "../sdl-encoder"}
serde = "1"
serde_json = "1"
Expand Down
10 changes: 5 additions & 5 deletions crates/rover-client/src/blocking/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use graphql_client::{Error as GraphQLError, GraphQLQuery, Response as GraphQLRes
use reqwest::{
blocking::{Client as ReqwestClient, Response},
header::HeaderMap,
StatusCode,
Error as ReqwestError, StatusCode,
};
use std::collections::HashMap;

Expand All @@ -16,11 +16,11 @@ pub struct GraphQLClient {
impl GraphQLClient {
/// Construct a new [Client] from a `graphql_endpoint`.
/// This client is used for generic GraphQL requests, such as introspection.
pub fn new(graphql_endpoint: &str) -> GraphQLClient {
GraphQLClient {
client: ReqwestClient::new(),
pub fn new(graphql_endpoint: &str) -> Result<GraphQLClient, ReqwestError> {
Ok(GraphQLClient {
client: ReqwestClient::builder().gzip(true).build()?,
graphql_endpoint: graphql_endpoint.to_string(),
}
})
}

/// Client method for making a GraphQL request.
Expand Down
13 changes: 9 additions & 4 deletions crates/rover-client/src/blocking/studio_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{blocking::GraphQLClient, headers, RoverClientError};
use houston::Credential;

use graphql_client::GraphQLQuery;
use reqwest::Error as ReqwestError;

/// Represents a client for making GraphQL requests to Apollo Studio.
pub struct StudioClient {
Expand All @@ -13,12 +14,16 @@ pub struct StudioClient {
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, graphql_endpoint: &str, version: &str) -> StudioClient {
StudioClient {
pub fn new(
credential: Credential,
graphql_endpoint: &str,
version: &str,
) -> Result<StudioClient, ReqwestError> {
Ok(StudioClient {
credential,
client: GraphQLClient::new(graphql_endpoint),
client: GraphQLClient::new(graphql_endpoint)?,
version: version.to_string(),
}
})
}

/// Client method for making a GraphQL request to Apollo Studio.
Expand Down
27 changes: 27 additions & 0 deletions crates/rover-client/tests/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const STUDIO_PROD_API_ENDPOINT: &str = "https://graphql.api.apollographql.com/api/graphql";

#[cfg(test)]
mod tests {
use houston::{Credential, CredentialOrigin};
use rover_client::blocking::{GraphQLClient, StudioClient};

use crate::STUDIO_PROD_API_ENDPOINT;

#[test]
fn it_can_build_client() {
assert!(GraphQLClient::new(STUDIO_PROD_API_ENDPOINT).is_ok());
}

#[test]
fn it_can_build_studio_client() {
assert!(StudioClient::new(
Credential {
api_key: "api:key:here".to_string(),
origin: CredentialOrigin::EnvVar,
},
"0.1.0",
STUDIO_PROD_API_ENDPOINT
)
.is_ok());
}
}
2 changes: 1 addition & 1 deletion src/command/graph/introspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct Introspect {

impl Introspect {
pub fn run(&self) -> Result<RoverStdout> {
let client = GraphQLClient::new(&self.endpoint.to_string());
let client = GraphQLClient::new(&self.endpoint.to_string())?;

// add the flag headers to a hashmap to pass along to rover-client
let mut headers = HashMap::new();
Expand Down
2 changes: 1 addition & 1 deletion src/command/subgraph/introspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct Introspect {

impl Introspect {
pub fn run(&self) -> Result<RoverStdout> {
let client = GraphQLClient::new(&self.endpoint.to_string());
let client = GraphQLClient::new(&self.endpoint.to_string())?;

// add the flag headers to a hashmap to pass along to rover-client
let mut headers = HashMap::new();
Expand Down
2 changes: 1 addition & 1 deletion src/command/supergraph/compose/do_compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub(crate) fn get_subgraph_definitions(
SchemaSource::SubgraphIntrospection { subgraph_url } => {
// given a federated introspection URL, use subgraph introspect to
// obtain SDL and add it to subgraph_definition.
let client = GraphQLClient::new(&subgraph_url.to_string());
let client = GraphQLClient::new(&subgraph_url.to_string())?;

let introspection_response = introspect::run(&client, &HashMap::new())?;
let schema = introspection_response.result;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ impl StudioClientConfig {

pub fn get_client(&self, profile_name: &str) -> Result<StudioClient> {
let credential = config::Profile::get_credential(profile_name, &self.config)?;
Ok(StudioClient::new(credential, &self.uri, &self.version))
Ok(StudioClient::new(credential, &self.uri, &self.version)?)
}
}

0 comments on commit f282490

Please sign in to comment.