use crate::client::BigQuery;
use gaxi::options::ClientConfig;
use google_cloud_auth::credentials::Credentials;
use google_cloud_gax::client_builder::Result;
#[derive(Clone, Debug)]
pub struct ClientBuilder {
pub(crate) config: ClientConfig,
pub(crate) project_id: Option<String>,
}
impl Default for ClientBuilder {
fn default() -> Self {
Self::new()
}
}
impl ClientBuilder {
pub fn new() -> Self {
Self {
config: ClientConfig::default(),
project_id: None,
}
}
pub fn with_project_id<V: Into<String>>(mut self, project_id: V) -> Self {
self.project_id = Some(project_id.into());
self
}
pub fn with_endpoint<V: Into<String>>(mut self, v: V) -> Self {
self.config.endpoint = Some(v.into());
self
}
pub fn with_credentials<V: Into<Credentials>>(mut self, credentials: V) -> Self {
self.config.cred = Some(credentials.into());
self
}
pub fn with_universe_domain<V: Into<String>>(mut self, v: V) -> Self {
self.config.universe_domain = Some(v.into());
self
}
pub fn with_tracing(mut self) -> Self {
self.config.tracing = true;
self
}
pub fn with_retry_policy<V: Into<google_cloud_gax::retry_policy::RetryPolicyArg>>(
mut self,
v: V,
) -> Self {
self.config.retry_policy = Some(v.into().into());
self
}
pub fn with_backoff_policy<V: Into<google_cloud_gax::backoff_policy::BackoffPolicyArg>>(
mut self,
v: V,
) -> Self {
self.config.backoff_policy = Some(v.into().into());
self
}
pub async fn build(self) -> Result<BigQuery> {
BigQuery::new(self).await
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::query::retry_policy::RetryableErrors;
use google_cloud_auth::credentials::anonymous::Builder as Anonymous;
use google_cloud_gax::exponential_backoff::ExponentialBackoff;
#[test]
fn defaults() -> anyhow::Result<()> {
let builder = ClientBuilder::new();
assert!(builder.config.endpoint.is_none(), "{builder:?}");
assert!(builder.config.universe_domain.is_none(), "{builder:?}");
assert!(builder.config.cred.is_none(), "{builder:?}");
assert!(!builder.config.tracing);
assert!(builder.config.retry_policy.is_none(), "{builder:?}");
assert!(builder.config.backoff_policy.is_none(), "{builder:?}");
assert!(builder.project_id.is_none(), "{builder:?}");
Ok(())
}
#[tokio::test]
async fn setters() -> anyhow::Result<()> {
let builder = ClientBuilder::new()
.with_project_id("test-project")
.with_endpoint("test-endpoint.com")
.with_universe_domain("test-universe.com")
.with_credentials(Anonymous::new().build())
.with_retry_policy(RetryableErrors)
.with_backoff_policy(ExponentialBackoff::default())
.with_tracing();
assert_eq!(builder.project_id, Some("test-project".to_string()));
assert_eq!(
builder.config.endpoint,
Some("test-endpoint.com".to_string())
);
assert_eq!(
builder.config.universe_domain,
Some("test-universe.com".to_string())
);
assert!(builder.config.cred.is_some(), "{builder:?}");
assert!(builder.config.tracing);
assert!(builder.config.retry_policy.is_some(), "{builder:?}");
assert!(builder.config.backoff_policy.is_some(), "{builder:?}");
Ok(())
}
}