Skip to content

Commit

Permalink
chore: Add client builder instead of function wrapper (#890)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptondereau committed Oct 25, 2021
1 parent 95b19e4 commit 32008f4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
14 changes: 7 additions & 7 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use structopt::{clap::AppSettings, StructOpt};
use crate::command::output::JsonOutput;
use crate::command::{self, RoverOutput};
use crate::utils::{
client::{get_configured_client, ClientTimeout, StudioClientConfig},
client::{ClientBuilder, ClientTimeout, StudioClientConfig},
env::{RoverEnv, RoverEnvKey},
stringify::option_from_display,
version,
Expand Down Expand Up @@ -259,12 +259,12 @@ impl Rover {
// if a request hasn't been made yet, this cell won't be populated yet
self.client
.fill(
get_configured_client(
self.accept_invalid_certs,
self.accept_invalid_hostnames,
self.client_timeout,
)
.expect("Could not configure the request client"),
ClientBuilder::new()
.accept_invalid_certs(self.accept_invalid_certs)
.accept_invalid_hostnames(self.accept_invalid_hostnames)
.with_timeout(self.client_timeout.get_duration())
.build()
.expect("Could not configure the request client"),
)
.expect("Could not overwrite the existing request client");
self.get_reqwest_client()
Expand Down
56 changes: 45 additions & 11 deletions src/utils/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,53 @@ use serde::Serialize;
/// the Apollo graph registry's production API endpoint
const STUDIO_PROD_API_ENDPOINT: &str = "https://graphql.api.apollographql.com/api/graphql";

pub(crate) fn get_configured_client(
pub(crate) struct ClientBuilder {
accept_invalid_certs: bool,
accept_invalid_hostnames: bool,
client_timeout: ClientTimeout,
) -> Result<Client> {
let client = Client::builder()
.gzip(true)
.brotli(true)
.danger_accept_invalid_certs(accept_invalid_certs)
.danger_accept_invalid_hostnames(accept_invalid_hostnames)
.timeout(client_timeout.get_duration())
.build()?;
Ok(client)
timeout: Option<std::time::Duration>,
}

impl ClientBuilder {
pub(crate) fn new() -> Self {
Self {
accept_invalid_certs: false,
accept_invalid_hostnames: false,
timeout: None,
}
}

pub(crate) fn accept_invalid_certs(self, value: bool) -> Self {
Self {
accept_invalid_certs: value,
..self
}
}

pub(crate) fn accept_invalid_hostnames(self, value: bool) -> Self {
Self {
accept_invalid_hostnames: value,
..self
}
}

pub(crate) fn with_timeout(self, timeout: std::time::Duration) -> Self {
Self {
timeout: Some(timeout),
..self
}
}

pub(crate) fn build(self) -> Result<Client> {
let client = Client::builder()
.gzip(true)
.brotli(true)
.danger_accept_invalid_certs(self.accept_invalid_certs)
.danger_accept_invalid_hostnames(self.accept_invalid_hostnames)
.timeout(self.timeout)
.build()?;

Ok(client)
}
}

#[derive(Debug, Copy, Clone, Serialize)]
Expand Down

0 comments on commit 32008f4

Please sign in to comment.