Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add client builder instead of function wrapper #890

Merged
merged 1 commit into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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