From 32008f46b177c3fd2218fb8b74600a658b962fb3 Mon Sep 17 00:00:00 2001 From: Pierre Tondereau Date: Mon, 25 Oct 2021 16:48:41 +0200 Subject: [PATCH] chore: Add client builder instead of function wrapper (#890) --- src/cli.rs | 14 ++++++------ src/utils/client.rs | 56 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index d53160c18..0c4145883 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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, @@ -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() diff --git a/src/utils/client.rs b/src/utils/client.rs index ed3930daa..593fb728d 100644 --- a/src/utils/client.rs +++ b/src/utils/client.rs @@ -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 { - 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, +} + +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 { + 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)]