use std::sync::Arc;
use crate::auth::models::Secret;
#[derive(Debug, Clone)]
pub struct ServiceAccountKey {
pub client_email: String,
pub private_key: Secret,
pub token_uri: String,
pub project_id: String,
}
#[derive(Debug, Clone)]
pub struct BigQueryClient {
pub(crate) http: reqwest::Client,
pub(crate) base_url: String,
pub(crate) project: String,
pub(crate) location: String,
pub(crate) key: ServiceAccountKey,
pub(crate) token: Arc<tokio::sync::Mutex<Option<(Secret, i64)>>>,
pub(crate) storage_url: String,
pub(crate) channel: Arc<tokio::sync::OnceCell<tonic::transport::Channel>>,
}
impl BigQueryClient {
pub fn location(&self) -> &str {
&self.location
}
pub fn new(
http: reqwest::Client,
(project, location): (&str, &str),
key: ServiceAccountKey,
(base_url, storage_url): (Option<&str>, Option<&str>),
) -> Self {
Self {
http,
base_url: base_url
.unwrap_or(crate::bigquery_api::constants::BASE_URL)
.trim_end_matches('/')
.to_owned(),
project: project.to_owned(),
location: location.to_owned(),
key,
token: Arc::default(),
storage_url: storage_url
.unwrap_or(crate::bigquery_api::constants::STORAGE_URL)
.trim_end_matches('/')
.to_owned(),
channel: Arc::default(),
}
}
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct Int64Value {
#[prost(int64, tag = "1")]
pub value: i64,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct Timestamp {
#[prost(int64, tag = "1")]
pub seconds: i64,
#[prost(int32, tag = "2")]
pub nanos: i32,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct RpcStatus {
#[prost(int32, tag = "1")]
pub code: i32,
#[prost(string, tag = "2")]
pub message: String,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct WriteStream {
#[prost(string, tag = "1")]
pub name: String,
#[prost(int32, tag = "2")]
pub r#type: i32,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct CreateWriteStreamRequest {
#[prost(string, tag = "1")]
pub parent: String,
#[prost(message, optional, tag = "2")]
pub write_stream: Option<WriteStream>,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct ArrowSchema {
#[prost(bytes = "vec", tag = "1")]
pub serialized_schema: Vec<u8>,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct ArrowRecordBatch {
#[prost(bytes = "vec", tag = "1")]
pub serialized_record_batch: Vec<u8>,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct ArrowData {
#[prost(message, optional, tag = "1")]
pub writer_schema: Option<ArrowSchema>,
#[prost(message, optional, tag = "2")]
pub rows: Option<ArrowRecordBatch>,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct AppendRowsRequest {
#[prost(string, tag = "1")]
pub write_stream: String,
#[prost(message, optional, tag = "2")]
pub offset: Option<Int64Value>,
#[prost(message, optional, tag = "5")]
pub arrow_rows: Option<ArrowData>,
#[prost(string, tag = "6")]
pub trace_id: String,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct AppendResult {
#[prost(message, optional, tag = "1")]
pub offset: Option<Int64Value>,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct RefusedRow {
#[prost(int64, tag = "1")]
pub index: i64,
#[prost(int32, tag = "2")]
pub code: i32,
#[prost(string, tag = "3")]
pub message: String,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct AppendRowsResponse {
#[prost(message, optional, tag = "1")]
pub append_result: Option<AppendResult>,
#[prost(message, optional, tag = "2")]
pub error: Option<RpcStatus>,
#[prost(message, repeated, tag = "4")]
pub row_errors: Vec<RefusedRow>,
#[prost(string, tag = "5")]
pub write_stream: String,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct FinalizeWriteStreamRequest {
#[prost(string, tag = "1")]
pub name: String,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct FinalizeWriteStreamResponse {
#[prost(int64, tag = "1")]
pub row_count: i64,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct BatchCommitWriteStreamsRequest {
#[prost(string, tag = "1")]
pub parent: String,
#[prost(string, repeated, tag = "2")]
pub write_streams: Vec<String>,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct StreamRefusal {
#[prost(int32, tag = "1")]
pub code: i32,
#[prost(string, tag = "2")]
pub entity: String,
#[prost(string, tag = "3")]
pub error_message: String,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct BatchCommitWriteStreamsResponse {
#[prost(message, optional, tag = "1")]
pub commit_time: Option<Timestamp>,
#[prost(message, repeated, tag = "2")]
pub stream_errors: Vec<StreamRefusal>,
}
#[derive(Debug)]
pub struct AppendStream {
pub requests: tokio::sync::mpsc::Sender<AppendRowsRequest>,
pub responses: tonic::Streaming<AppendRowsResponse>,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct ReadSession {
#[prost(string, tag = "1")]
pub name: String,
#[prost(int32, tag = "3")]
pub data_format: i32,
#[prost(message, optional, tag = "5")]
pub arrow_schema: Option<ArrowSchema>,
#[prost(string, tag = "6")]
pub table: String,
#[prost(message, optional, tag = "7")]
pub table_modifiers: Option<TableModifiers>,
#[prost(message, optional, tag = "8")]
pub read_options: Option<TableReadOptions>,
#[prost(message, repeated, tag = "10")]
pub streams: Vec<ReadStream>,
#[prost(int64, tag = "12")]
pub estimated_total_bytes_scanned: i64,
#[prost(int64, tag = "14")]
pub estimated_row_count: i64,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct TableModifiers {
#[prost(message, optional, tag = "1")]
pub snapshot_time: Option<Timestamp>,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct TableReadOptions {
#[prost(string, repeated, tag = "1")]
pub selected_fields: Vec<String>,
#[prost(string, tag = "2")]
pub row_restriction: String,
#[prost(message, optional, tag = "3")]
pub arrow_serialization_options: Option<ArrowSerializationOptions>,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct ArrowSerializationOptions {
#[prost(int32, tag = "2")]
pub buffer_compression: i32,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct ReadStream {
#[prost(string, tag = "1")]
pub name: String,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct CreateReadSessionRequest {
#[prost(string, tag = "1")]
pub parent: String,
#[prost(message, optional, tag = "2")]
pub read_session: Option<ReadSession>,
#[prost(int32, tag = "3")]
pub max_stream_count: i32,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct ReadRowsRequest {
#[prost(string, tag = "1")]
pub read_stream: String,
#[prost(int64, tag = "2")]
pub offset: i64,
}
#[derive(Clone, PartialEq, Debug, prost::Message)]
#[prost(skip_debug)]
pub struct ReadRowsResponse {
#[prost(message, optional, tag = "4")]
pub arrow_record_batch: Option<ArrowRecordBatch>,
#[prost(int64, tag = "6")]
pub row_count: i64,
}
#[derive(Debug, Clone)]
pub struct ReadSessionRequest<'a> {
pub fields: &'a [String],
pub restriction: Option<&'a str>,
pub snapshot_ms: Option<i64>,
pub max_streams: i32,
}