gcp_bigquery_client/
lib.rs#[macro_use]
extern crate serde;
extern crate serde_json;
use std::env;
use std::path::PathBuf;
use std::sync::Arc;
use client_builder::ClientBuilder;
use reqwest::Response;
use serde::Deserialize;
use storage::StorageApi;
use yup_oauth2::ServiceAccountKey;
use crate::auth::Authenticator;
use crate::dataset::DatasetApi;
use crate::error::BQError;
use crate::job::JobApi;
use crate::model_api::ModelApi;
use crate::project::ProjectApi;
use crate::routine::RoutineApi;
use crate::table::TableApi;
use crate::tabledata::TableDataApi;
pub use yup_oauth2;
pub mod auth;
pub mod client_builder;
pub mod dataset;
pub mod error;
pub mod job;
pub mod model;
pub mod model_api;
pub mod project;
pub mod routine;
pub mod storage;
pub mod table;
pub mod tabledata;
const BIG_QUERY_V2_URL: &str = "https://bigquery.googleapis.com/bigquery/v2";
const BIG_QUERY_AUTH_URL: &str = "https://www.googleapis.com/auth/bigquery";
#[derive(Clone)]
pub struct Client {
dataset_api: DatasetApi,
table_api: TableApi,
job_api: JobApi,
tabledata_api: TableDataApi,
routine_api: RoutineApi,
model_api: ModelApi,
project_api: ProjectApi,
storage_api: StorageApi,
}
impl Client {
pub async fn from_authenticator(auth: Arc<dyn Authenticator>) -> Result<Self, BQError> {
let write_client = StorageApi::new_write_client().await?;
let client = reqwest::Client::new();
Ok(Self {
dataset_api: DatasetApi::new(client.clone(), Arc::clone(&auth)),
table_api: TableApi::new(client.clone(), Arc::clone(&auth)),
job_api: JobApi::new(client.clone(), Arc::clone(&auth)),
tabledata_api: TableDataApi::new(client.clone(), Arc::clone(&auth)),
routine_api: RoutineApi::new(client.clone(), Arc::clone(&auth)),
model_api: ModelApi::new(client.clone(), Arc::clone(&auth)),
project_api: ProjectApi::new(client, Arc::clone(&auth)),
storage_api: StorageApi::new(write_client, auth),
})
}
pub async fn from_service_account_key_file(sa_key_file: &str) -> Result<Self, BQError> {
ClientBuilder::new()
.build_from_service_account_key_file(sa_key_file)
.await
}
pub async fn from_service_account_key(sa_key: ServiceAccountKey, readonly: bool) -> Result<Self, BQError> {
ClientBuilder::new()
.build_from_service_account_key(sa_key, readonly)
.await
}
pub async fn with_workload_identity(readonly: bool) -> Result<Self, BQError> {
ClientBuilder::new().build_with_workload_identity(readonly).await
}
pub(crate) fn v2_base_url(&mut self, base_url: String) -> &mut Self {
self.dataset_api.with_base_url(base_url.clone());
self.table_api.with_base_url(base_url.clone());
self.job_api.with_base_url(base_url.clone());
self.tabledata_api.with_base_url(base_url.clone());
self.routine_api.with_base_url(base_url.clone());
self.model_api.with_base_url(base_url.clone());
self.project_api.with_base_url(base_url.clone());
self.storage_api.with_base_url(base_url);
self
}
pub async fn from_installed_flow_authenticator<S: AsRef<[u8]>, P: Into<PathBuf>>(
secret: S,
persistant_file_path: P,
) -> Result<Self, BQError> {
ClientBuilder::new()
.build_from_installed_flow_authenticator(secret, persistant_file_path)
.await
}
pub async fn from_installed_flow_authenticator_from_secret_file<P: Into<PathBuf>>(
secret_file: &str,
persistant_file_path: P,
) -> Result<Self, BQError> {
Self::from_installed_flow_authenticator(
tokio::fs::read(secret_file)
.await
.expect("expecting a valid secret file."),
persistant_file_path,
)
.await
}
pub async fn from_application_default_credentials() -> Result<Self, BQError> {
ClientBuilder::new().build_from_application_default_credentials().await
}
pub async fn from_authorized_user_secret(secret: &str) -> Result<Self, BQError> {
ClientBuilder::new()
.build_from_authorized_user_authenticator(secret)
.await
}
pub fn dataset(&self) -> &DatasetApi {
&self.dataset_api
}
pub fn table(&self) -> &TableApi {
&self.table_api
}
pub fn job(&self) -> &JobApi {
&self.job_api
}
pub fn tabledata(&self) -> &TableDataApi {
&self.tabledata_api
}
pub fn routine(&self) -> &RoutineApi {
&self.routine_api
}
pub fn model(&self) -> &ModelApi {
&self.model_api
}
pub fn project(&self) -> &ProjectApi {
&self.project_api
}
pub fn storage(&self) -> &StorageApi {
&self.storage_api
}
pub fn storage_mut(&mut self) -> &mut StorageApi {
&mut self.storage_api
}
}
pub(crate) fn urlencode<T: AsRef<str>>(s: T) -> String {
url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
}
async fn process_response<T: for<'de> Deserialize<'de>>(resp: Response) -> Result<T, BQError> {
if resp.status().is_success() {
Ok(resp.json().await?)
} else {
Err(BQError::ResponseError {
error: resp.json().await?,
})
}
}
pub fn env_vars() -> (String, String, String, String) {
let project_id = env::var("PROJECT_ID").expect("Environment variable PROJECT_ID");
let dataset_id = env::var("DATASET_ID").expect("Environment variable DATASET_ID");
let table_id = env::var("TABLE_ID").expect("Environment variable TABLE_ID");
let gcp_sa_key =
env::var("GOOGLE_APPLICATION_CREDENTIALS").expect("Environment variable GOOGLE_APPLICATION_CREDENTIALS");
(project_id, dataset_id, table_id, gcp_sa_key)
}
pub mod google {
#![allow(clippy::all)]
#[path = "google.api.rs"]
pub mod api;
#[path = ""]
pub mod cloud {
#[path = ""]
pub mod bigquery {
#[path = ""]
pub mod storage {
#![allow(clippy::all)]
#[path = "google.cloud.bigquery.storage.v1.rs"]
pub mod v1;
}
}
}
#[path = "google.rpc.rs"]
pub mod rpc;
}