use crate::{
clients::DatabaseClient,
cosmos_request::CosmosRequest,
models::{DatabaseProperties, ResourceResponse},
operation_context::OperationType,
pipeline::GatewayPipeline,
resource_context::ResourceLink,
routing::{
global_endpoint_manager::GlobalEndpointManager,
global_partition_endpoint_manager::GlobalPartitionEndpointManager,
},
CreateDatabaseOptions, FeedItemIterator, Query, QueryDatabasesOptions,
};
use azure_core::http::{Context, Url};
use azure_data_cosmos_driver::CosmosDriver;
use serde::Serialize;
use std::sync::Arc;
pub use super::cosmos_client_builder::CosmosClientBuilder;
#[derive(Debug, Clone)]
pub struct CosmosClient {
pub(crate) databases_link: ResourceLink,
pub(crate) pipeline: Arc<GatewayPipeline>,
pub(crate) driver: Arc<CosmosDriver>,
pub(crate) global_endpoint_manager: Arc<GlobalEndpointManager>,
pub(crate) global_partition_endpoint_manager: Arc<GlobalPartitionEndpointManager>,
}
impl CosmosClient {
pub fn builder() -> CosmosClientBuilder {
CosmosClientBuilder::new()
}
pub fn database_client(&self, id: &str) -> DatabaseClient {
DatabaseClient::new(
self.pipeline.clone(),
id,
self.driver.clone(),
self.global_endpoint_manager.clone(),
self.global_partition_endpoint_manager.clone(),
)
}
pub fn endpoint(&self) -> &Url {
&self.pipeline.endpoint
}
pub fn query_databases(
&self,
query: impl Into<Query>,
_options: Option<QueryDatabasesOptions>,
) -> azure_core::Result<FeedItemIterator<DatabaseProperties>> {
crate::query::executor::QueryExecutor::new(
self.pipeline.clone(),
self.databases_link.clone(),
Context::default(),
query.into(),
azure_core::http::headers::Headers::new(),
)
.into_stream()
}
#[doc = include_str!("../../docs/control-plane-warning.md")]
pub async fn create_database(
&self,
id: &str,
options: Option<CreateDatabaseOptions>,
) -> azure_core::Result<ResourceResponse<DatabaseProperties>> {
let options = options.unwrap_or_default();
#[derive(Serialize)]
struct RequestBody<'a> {
id: &'a str,
}
let cosmos_request =
CosmosRequest::builder(OperationType::Create, self.databases_link.clone())
.request_headers(&options.throughput)
.json(&RequestBody { id })
.build()?;
self.pipeline
.send(cosmos_request, Context::default())
.await
.map(ResourceResponse::new)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[allow(dead_code, unreachable_code, unused_variables)]
fn _assert_futures_are_send() {
fn assert_send<T: Send>(_: T) {}
let client: &CosmosClient = todo!();
assert_send(client.create_database(todo!(), todo!()));
}
}