diff --git a/sdk/core/src/macros.rs b/sdk/core/src/macros.rs index 0499bb270d..cf5781df71 100644 --- a/sdk/core/src/macros.rs +++ b/sdk/core/src/macros.rs @@ -1,6 +1,6 @@ /// Creates setter methods /// -/// The methods created are of the form `with_$name` that takes an argument of type `$typ` +/// The methods created are of the form `$name` that takes an argument of type `$typ` /// and sets the field $name to result of calling `$transform` with the value of the argument. /// /// In other words. The following macro call: diff --git a/sdk/core/src/options.rs b/sdk/core/src/options.rs index 99c9976f11..c88e38f4cc 100644 --- a/sdk/core/src/options.rs +++ b/sdk/core/src/options.rs @@ -17,19 +17,14 @@ use std::time::Duration; /// ``` #[derive(Clone, Debug, Default)] pub struct ClientOptions { - // TODO: Expose transport override. /// Policies called per call. pub(crate) per_call_policies: Vec>, - /// Policies called per retry. pub(crate) per_retry_policies: Vec>, - /// Retry options. pub(crate) retry: RetryOptions, - /// Telemetry options. pub(crate) telemetry: TelemetryOptions, - /// Transport options. pub(crate) transport: TransportOptions, } diff --git a/sdk/cosmos/examples/cancellation.rs b/sdk/cosmos/examples/cancellation.rs index 99519685de..0c2817aeb2 100644 --- a/sdk/cosmos/examples/cancellation.rs +++ b/sdk/cosmos/examples/cancellation.rs @@ -1,4 +1,3 @@ -use azure_core::prelude::*; use azure_cosmos::prelude::*; use stop_token::prelude::*; use stop_token::StopSource; @@ -19,8 +18,7 @@ async fn main() -> azure_cosmos::Result<()> { let client = CosmosClient::new(account.clone(), authorization_token.clone(), options); // Create a new database, and time out if it takes more than 1 second. - let options = CreateDatabaseOptions::new(); - let future = client.create_database(Context::new(), "my_database", options); + let future = client.create_database("my_database").into_future(); let deadline = Instant::now() + Duration::from_secs(1); match future.until(deadline).await { Ok(Ok(r)) => println!("successful response: {:?}", r), @@ -36,8 +34,7 @@ async fn main() -> azure_cosmos::Result<()> { // Clone the stop token for each request. let stop = source.token(); tokio::spawn(async move { - let options = CreateDatabaseOptions::new(); - let future = client.create_database(Context::new(), "my_database", options); + let future = client.create_database("my_database").into_future(); match future.until(stop).await { Ok(Ok(r)) => println!("successful response: {:?}", r), Ok(Err(e)) => println!("request was made but failed: {:?}", e), diff --git a/sdk/cosmos/src/clients/cosmos_client.rs b/sdk/cosmos/src/clients/cosmos_client.rs index 16ddbfa201..1bb2a90fe3 100644 --- a/sdk/cosmos/src/clients/cosmos_client.rs +++ b/sdk/cosmos/src/clients/cosmos_client.rs @@ -169,21 +169,8 @@ impl CosmosClient { } /// Create a database - pub async fn create_database>( - &self, - ctx: Context, - database_name: S, - options: CreateDatabaseOptions, - ) -> crate::Result { - let mut request = self.prepare_request_pipeline("dbs", http::Method::POST); - - options.decorate_request(&mut request, database_name.as_ref())?; - let response = self - .pipeline() - .send(ctx.clone().insert(ResourceType::Databases), &mut request) - .await?; - - Ok(CreateDatabaseResponse::try_from(response).await?) + pub fn create_database>(&self, database_name: S) -> CreateDatabaseBuilder { + CreateDatabaseBuilder::new(self.clone(), database_name.as_ref().to_owned()) } /// List all databases diff --git a/sdk/cosmos/src/operations/create_database.rs b/sdk/cosmos/src/operations/create_database.rs index d4dd648744..1164ea5091 100644 --- a/sdk/cosmos/src/operations/create_database.rs +++ b/sdk/cosmos/src/operations/create_database.rs @@ -3,44 +3,68 @@ use crate::prelude::*; use crate::resources::Database; use crate::ResourceQuota; use azure_core::headers::{etag_from_headers, session_token_from_headers}; -use azure_core::{collect_pinned_stream, Request as HttpRequest, Response as HttpResponse}; +use azure_core::{collect_pinned_stream, Context, Response as HttpResponse}; use chrono::{DateTime, Utc}; #[derive(Debug, Clone)] -pub struct CreateDatabaseOptions { +pub struct CreateDatabaseBuilder { + client: CosmosClient, + database_name: String, consistency_level: Option, + context: Context, } -impl CreateDatabaseOptions { - pub fn new() -> Self { +impl CreateDatabaseBuilder { + pub(crate) fn new(client: CosmosClient, database_name: String) -> Self { Self { + client, + database_name, consistency_level: None, + context: Context::new(), } } setters! { consistency_level: ConsistencyLevel => Some(consistency_level), + context: Context => context, } -} -impl CreateDatabaseOptions { - pub(crate) fn decorate_request( - &self, - request: &mut HttpRequest, - database_name: &str, - ) -> crate::Result<()> { - #[derive(Serialize)] - struct CreateDatabaseRequest<'a> { - pub id: &'a str, - } - let req = CreateDatabaseRequest { id: database_name }; + pub fn insert(&mut self, entity: E) -> &mut Self { + self.context.insert(entity); + self + } + + pub fn into_future(mut self) -> CreateDatabase { + Box::pin(async move { + let mut request = self + .client + .prepare_request_pipeline("dbs", http::Method::POST); + + let body = CreateDatabaseBody { + id: self.database_name.as_str(), + }; - azure_core::headers::add_optional_header2(&self.consistency_level, request)?; - request.set_body(bytes::Bytes::from(serde_json::to_string(&req)?).into()); - Ok(()) + azure_core::headers::add_optional_header2(&self.consistency_level, &mut request)?; + request.set_body(bytes::Bytes::from(serde_json::to_string(&body)?).into()); + let response = self + .client + .pipeline() + .send(self.context.insert(ResourceType::Databases), &mut request) + .await?; + + CreateDatabaseResponse::try_from(response).await + }) } } +/// A future of a create database response +type CreateDatabase = futures::future::BoxFuture<'static, crate::Result>; + +#[derive(Serialize)] +struct CreateDatabaseBody<'a> { + pub id: &'a str, +} + #[derive(Debug, Clone, PartialEq, PartialOrd)] pub struct CreateDatabaseResponse { pub database: Database, diff --git a/sdk/cosmos/tests/attachment_00.rs b/sdk/cosmos/tests/attachment_00.rs index 0d05a1412c..fea476b600 100644 --- a/sdk/cosmos/tests/attachment_00.rs +++ b/sdk/cosmos/tests/attachment_00.rs @@ -37,11 +37,8 @@ async fn attachment() -> Result<(), azure_cosmos::Error> { // create a temp database let _create_database_response = client - .create_database( - azure_core::Context::new(), - DATABASE_NAME, - CreateDatabaseOptions::new(), - ) + .create_database(DATABASE_NAME) + .into_future() .await .unwrap(); diff --git a/sdk/cosmos/tests/collection_operations.rs b/sdk/cosmos/tests/collection_operations.rs index adcd0dc63b..49e1534ca8 100644 --- a/sdk/cosmos/tests/collection_operations.rs +++ b/sdk/cosmos/tests/collection_operations.rs @@ -17,9 +17,7 @@ async fn collection_operations() -> Result<(), BoxedError> { let database_name = "test-collection-operations"; let context = Context::new(); - client - .create_database(context.clone(), database_name, CreateDatabaseOptions::new()) - .await?; + client.create_database(database_name).into_future().await?; // create collection! let db_client = client.clone().into_database_client(database_name.clone()); diff --git a/sdk/cosmos/tests/cosmos_collection.rs b/sdk/cosmos/tests/cosmos_collection.rs index e62d0b2954..8ef93d7df2 100644 --- a/sdk/cosmos/tests/cosmos_collection.rs +++ b/sdk/cosmos/tests/cosmos_collection.rs @@ -14,11 +14,8 @@ async fn create_and_delete_collection() { let client = setup::initialize().unwrap(); client - .create_database( - azure_core::Context::new(), - DATABASE_NAME, - CreateDatabaseOptions::new(), - ) + .create_database(DATABASE_NAME) + .into_future() .await .unwrap(); @@ -85,11 +82,8 @@ async fn replace_collection() { const COLLECTION_NAME: &str = "test-collection"; client - .create_database( - azure_core::Context::new(), - DATABASE_NAME, - CreateDatabaseOptions::new(), - ) + .create_database(DATABASE_NAME) + .into_future() .await .unwrap(); diff --git a/sdk/cosmos/tests/cosmos_database.rs.disabled b/sdk/cosmos/tests/cosmos_database.rs.disabled index 9d9757de6c..bbe1304343 100644 --- a/sdk/cosmos/tests/cosmos_database.rs.disabled +++ b/sdk/cosmos/tests/cosmos_database.rs.disabled @@ -22,11 +22,8 @@ async fn create_and_delete_database() { // create a new database and check if the number of DBs increased let database = client - .create_database( - azure_core::Context::new(), - DATABASE_NAME, - CreateDatabaseOptions::new(), - ) + .create_database(DATABASE_NAME) + .into_future() .await .unwrap(); diff --git a/sdk/cosmos/tests/cosmos_document.rs b/sdk/cosmos/tests/cosmos_document.rs index a6d6604078..ea1dd5e280 100644 --- a/sdk/cosmos/tests/cosmos_document.rs +++ b/sdk/cosmos/tests/cosmos_document.rs @@ -32,11 +32,8 @@ async fn create_and_delete_document() { let client = setup::initialize().unwrap(); client - .create_database( - azure_core::Context::new(), - DATABASE_NAME, - CreateDatabaseOptions::new(), - ) + .create_database(DATABASE_NAME) + .into_future() .await .unwrap(); @@ -126,11 +123,8 @@ async fn query_documents() { let client = setup::initialize().unwrap(); client - .create_database( - azure_core::Context::new(), - DATABASE_NAME, - CreateDatabaseOptions::new(), - ) + .create_database(DATABASE_NAME) + .into_future() .await .unwrap(); let database_client = client.into_database_client(DATABASE_NAME); @@ -203,11 +197,8 @@ async fn replace_document() { let client = setup::initialize().unwrap(); client - .create_database( - azure_core::Context::new(), - DATABASE_NAME, - CreateDatabaseOptions::new(), - ) + .create_database(DATABASE_NAME) + .into_future() .await .unwrap(); let database_client = client.into_database_client(DATABASE_NAME); diff --git a/sdk/cosmos/tests/create_database_and_collection.rs b/sdk/cosmos/tests/create_database_and_collection.rs index 2660643686..f7f8ad7895 100644 --- a/sdk/cosmos/tests/create_database_and_collection.rs +++ b/sdk/cosmos/tests/create_database_and_collection.rs @@ -19,13 +19,7 @@ async fn create_database_and_collection() -> Result<(), BoxedError> { // create database! log::info!("Creating a database with name '{}'...", database_name); - let db = client - .create_database( - context.clone(), - &database_name, - CreateDatabaseOptions::new(), - ) - .await?; + let db = client.create_database(&database_name).into_future().await?; log::info!("Successfully created a database"); log::debug!("The create_database response: {:#?}", db); diff --git a/sdk/cosmos/tests/permission.rs b/sdk/cosmos/tests/permission.rs index d4da02d17c..3612ce301d 100644 --- a/sdk/cosmos/tests/permission.rs +++ b/sdk/cosmos/tests/permission.rs @@ -17,11 +17,8 @@ async fn permissions() { // create a temp database let _create_database_response = client - .create_database( - azure_core::Context::new(), - DATABASE_NAME, - CreateDatabaseOptions::new(), - ) + .create_database(DATABASE_NAME) + .into_future() .await .unwrap(); diff --git a/sdk/cosmos/tests/permission_token_usage.rs b/sdk/cosmos/tests/permission_token_usage.rs index ec735e957f..58069ae96d 100644 --- a/sdk/cosmos/tests/permission_token_usage.rs +++ b/sdk/cosmos/tests/permission_token_usage.rs @@ -33,11 +33,8 @@ async fn permission_token_usage() { // create a temp database let _create_database_response = client - .create_database( - azure_core::Context::new(), - DATABASE_NAME, - CreateDatabaseOptions::new(), - ) + .create_database(DATABASE_NAME) + .into_future() .await .unwrap(); diff --git a/sdk/cosmos/tests/trigger.rs b/sdk/cosmos/tests/trigger.rs index 56cab1b00b..bbae3e3fa1 100644 --- a/sdk/cosmos/tests/trigger.rs +++ b/sdk/cosmos/tests/trigger.rs @@ -45,11 +45,8 @@ async fn trigger() -> Result<(), azure_cosmos::Error> { // create a temp database let _create_database_response = client - .create_database( - azure_core::Context::new(), - DATABASE_NAME, - CreateDatabaseOptions::new(), - ) + .create_database(DATABASE_NAME) + .into_future() .await .unwrap(); diff --git a/sdk/cosmos/tests/user.rs.disabled b/sdk/cosmos/tests/user.rs.disabled index 8a36cee741..edc7b7526f 100644 --- a/sdk/cosmos/tests/user.rs.disabled +++ b/sdk/cosmos/tests/user.rs.disabled @@ -18,11 +18,8 @@ async fn users() { // create a temp database let _create_database_response = client - .create_database( - azure_core::Context::new(), - DATABASE_NAME, - CreateDatabaseOptions::new(), - ) + .create_database(DATABASE_NAME) + .into_future() .await .unwrap(); diff --git a/sdk/cosmos/tests/user_defined_function00.rs b/sdk/cosmos/tests/user_defined_function00.rs index 6b779b767d..f5c5b95401 100644 --- a/sdk/cosmos/tests/user_defined_function00.rs +++ b/sdk/cosmos/tests/user_defined_function00.rs @@ -28,11 +28,8 @@ async fn user_defined_function00() -> Result<(), azure_cosmos::Error> { // create a temp database let _create_database_response = client - .create_database( - azure_core::Context::new(), - DATABASE_NAME, - CreateDatabaseOptions::new(), - ) + .create_database(DATABASE_NAME) + .into_future() .await .unwrap();