use std::time::Duration;
use e2e_test_model::{
CreateExecutionRequest, CreateStepRequest, Execution, GetExecutionsRequest, Step,
};
pub use reqwest::header::HeaderValue;
use uuid::Uuid;
pub mod model {
pub use e2e_test_model::*;
}
pub struct Config {
pub base_url: String,
pub api_header: Option<(String, HeaderValue)>,
}
pub struct Client {
inner: reqwest_middleware::ClientWithMiddleware,
config: Config,
}
impl Client {
pub fn new(config: Config) -> reqwest::Result<Self> {
let inner = reqwest::Client::builder()
.timeout(Duration::from_secs(15))
.connect_timeout(Duration::from_secs(30))
.build()?;
let retry_policy = reqwest_retry::policies::ExponentialBackoff {
max_n_retries: 5,
min_retry_interval: Duration::from_millis(100),
max_retry_interval: Duration::from_secs(5),
backoff_exponent: 2,
};
let retry_transient_middleware =
reqwest_retry::RetryTransientMiddleware::new_with_policy(retry_policy);
let inner = reqwest_middleware::ClientBuilder::new(inner)
.with(retry_transient_middleware)
.build();
let mut config = config;
if let Some((_, ref mut api_key)) = config.api_header {
api_key.set_sensitive(true);
}
Ok(Self { inner, config })
}
pub async fn get_executions(
&self,
params: &GetExecutionsRequest,
) -> reqwest_middleware::Result<Vec<Execution>> {
let resp = self
.inner
.get(self.url("/test-warehouse/executions"))
.base_headers(&self.config)
.query(¶ms)
.send()
.await?
.json()
.await?;
Ok(resp)
}
pub async fn post_execution(
&self,
body: &CreateExecutionRequest,
) -> reqwest_middleware::Result<Execution> {
let resp = self
.inner
.post(self.url("/test-warehouse/executions"))
.base_headers(&self.config)
.json(body)
.send()
.await?
.json()
.await?;
Ok(resp)
}
pub async fn post_step(
&self,
uuid: &Uuid,
body: &CreateStepRequest,
) -> reqwest_middleware::Result<Step> {
let url = format!("/test-warehouse/executions/{}/steps", uuid);
let resp = self
.inner
.post(self.url(&url))
.base_headers(&self.config)
.json(body)
.send()
.await?
.json()
.await?;
Ok(resp)
}
fn url(&self, uri: &str) -> String {
format!("{}{}", self.config.base_url, uri)
}
}
trait BaseHeaders {
fn base_headers(self, config: &Config) -> Self;
}
impl BaseHeaders for reqwest_middleware::RequestBuilder {
fn base_headers(self, config: &Config) -> Self {
if let Some((ref header, ref api_key)) = config.api_header {
self.header(header, api_key)
} else {
self
}
}
}