diff --git a/sdk/data_cosmos/examples/collection.rs b/sdk/data_cosmos/examples/collection.rs index cfb6d23d99..bf1fd86589 100644 --- a/sdk/data_cosmos/examples/collection.rs +++ b/sdk/data_cosmos/examples/collection.rs @@ -49,7 +49,8 @@ async fn main() -> Result<(), Box> { let db = client .clone() .into_database_client(db.id.clone()) - .get_database(Context::new(), GetDatabaseOptions::default()) + .get_database() + .into_future() .await?; println!("db {} found == {:?}", &db.database.id, &db); } @@ -77,7 +78,8 @@ async fn main() -> Result<(), Box> { let collection_response = database_client .clone() .into_collection_client(collection.id) - .get_collection(Context::new(), GetCollectionOptions::new()) + .get_collection() + .into_future() .await?; println!("\tcollection_response {:?}", collection_response); diff --git a/sdk/data_cosmos/examples/create_delete_database.rs b/sdk/data_cosmos/examples/create_delete_database.rs index dbeab5f539..04be1b78b7 100644 --- a/sdk/data_cosmos/examples/create_delete_database.rs +++ b/sdk/data_cosmos/examples/create_delete_database.rs @@ -58,9 +58,7 @@ async fn main() -> Result<(), Box> { let db_collection = db_client.clone().into_collection_client("panzadoro"); - let get_collection_response = db_collection - .get_collection(Context::new(), GetCollectionOptions::new()) - .await?; + let get_collection_response = db_collection.get_collection().into_future().await?; println!("get_collection_response == {:#?}", get_collection_response); let stream = db_client.list_collections(Context::new(), ListCollectionsOptions::new()); diff --git a/sdk/data_cosmos/examples/database_01.rs b/sdk/data_cosmos/examples/database_01.rs index 5670d18d22..878aa1f5c2 100644 --- a/sdk/data_cosmos/examples/database_01.rs +++ b/sdk/data_cosmos/examples/database_01.rs @@ -26,9 +26,7 @@ async fn main() -> Result<(), Box> { println!("collections == {:#?}", collections); let collection_client = database_client.into_collection_client("cnt"); - let collection = collection_client - .get_collection(Context::new(), GetCollectionOptions::new()) - .await?; + let collection = collection_client.get_collection().into_future().await?; println!("collection == {:#?}", collection); Ok(()) diff --git a/sdk/data_cosmos/examples/get_database.rs b/sdk/data_cosmos/examples/get_database.rs index 2426f63dab..806625a63c 100644 --- a/sdk/data_cosmos/examples/get_database.rs +++ b/sdk/data_cosmos/examples/get_database.rs @@ -38,7 +38,9 @@ async fn main() -> Result<(), Box> { context.insert(custom_headers); let response = database_client - .get_database(context, GetDatabaseOptions::new()) + .get_database() + .context(context) + .into_future() .await?; println!("response == {:?}", response); diff --git a/sdk/data_cosmos/examples/permission_00.rs b/sdk/data_cosmos/examples/permission_00.rs index 73ff089593..9dd77a4fdc 100644 --- a/sdk/data_cosmos/examples/permission_00.rs +++ b/sdk/data_cosmos/examples/permission_00.rs @@ -40,19 +40,13 @@ async fn main() -> Result<(), Box> { .into_collection_client(collection_name2); let user_client = database_client.clone().into_user_client(user_name); - let get_database_response = database_client - .get_database(Context::new(), GetDatabaseOptions::new()) - .await?; + let get_database_response = database_client.get_database().into_future().await?; println!("get_database_response == {:#?}", get_database_response); - let get_collection_response = collection_client - .get_collection(Context::new(), GetCollectionOptions::new()) - .await?; + let get_collection_response = collection_client.get_collection().into_future().await?; println!("get_collection_response == {:#?}", get_collection_response); - let get_collection2_response = collection2_client - .get_collection(Context::new(), GetCollectionOptions::new()) - .await?; + let get_collection2_response = collection2_client.get_collection().into_future().await?; println!( "get_collection2_response == {:#?}", get_collection2_response @@ -152,12 +146,11 @@ async fn main() -> Result<(), Box> { ); let delete_user_response = user_client - .delete_user( - Context::new(), - DeleteUserOptions::new().consistency_level(ConsistencyLevel::Session( - delete_permission_response.session_token, - )), - ) + .delete_user() + .consistency_level(ConsistencyLevel::Session( + delete_permission_response.session_token, + )) + .into_future() .await?; println!("delete_user_response == {:#?}", delete_user_response); diff --git a/sdk/data_cosmos/examples/user_00.rs b/sdk/data_cosmos/examples/user_00.rs index b0c82f0316..cc14770279 100644 --- a/sdk/data_cosmos/examples/user_00.rs +++ b/sdk/data_cosmos/examples/user_00.rs @@ -53,9 +53,7 @@ async fn main() -> Result<(), Box> { let user_client = database_client.into_user_client(new_user); - let delete_user_response = user_client - .delete_user(Context::new(), DeleteUserOptions::new()) - .await?; + let delete_user_response = user_client.delete_user().into_future().await?; println!("delete_user_response == {:#?}", delete_user_response); Ok(()) diff --git a/sdk/data_cosmos/examples/user_permission_token.rs b/sdk/data_cosmos/examples/user_permission_token.rs index 467595b63e..e78f783a50 100644 --- a/sdk/data_cosmos/examples/user_permission_token.rs +++ b/sdk/data_cosmos/examples/user_permission_token.rs @@ -1,4 +1,3 @@ -use azure_core::Context; use azure_data_cosmos::prelude::*; use std::error::Error; @@ -34,9 +33,7 @@ async fn main() -> Result<(), Box> { .into_collection_client(collection_name.clone()); let user_client = database_client.into_user_client(user_name); - let get_collection_response = collection_client - .get_collection(Context::new(), GetCollectionOptions::new()) - .await?; + let get_collection_response = collection_client.get_collection().into_future().await?; println!("get_collection_response == {:#?}", get_collection_response); let create_user_response = user_client.create_user().into_future().await?; @@ -168,9 +165,7 @@ async fn main() -> Result<(), Box> { ); println!("Cleaning up user."); - let delete_user_response = user_client - .delete_user(Context::new(), DeleteUserOptions::new()) - .await?; + let delete_user_response = user_client.delete_user().into_future().await?; println!("delete_user_response == {:#?}", delete_user_response); Ok(()) diff --git a/sdk/data_cosmos/src/clients/collection_client.rs b/sdk/data_cosmos/src/clients/collection_client.rs index 0a6b34fe6f..add52fee4b 100644 --- a/sdk/data_cosmos/src/clients/collection_client.rs +++ b/sdk/data_cosmos/src/clients/collection_client.rs @@ -42,21 +42,8 @@ impl CollectionClient { } /// Get a collection - pub async fn get_collection( - &self, - ctx: Context, - options: GetCollectionOptions, - ) -> crate::Result { - let mut request = self.prepare_request_with_collection_name(http::Method::GET); - - options.decorate_request(&mut request)?; - - let response = self - .pipeline() - .send(ctx.clone().insert(ResourceType::Collections), &mut request) - .await?; - - Ok(GetCollectionResponse::try_from(response).await?) + pub fn get_collection(&self) -> GetCollectionBuilder { + GetCollectionBuilder::new(self.clone()) } /// Delete a collection diff --git a/sdk/data_cosmos/src/clients/database_client.rs b/sdk/data_cosmos/src/clients/database_client.rs index fd815d564e..4220f7a3c2 100644 --- a/sdk/data_cosmos/src/clients/database_client.rs +++ b/sdk/data_cosmos/src/clients/database_client.rs @@ -58,22 +58,8 @@ impl DatabaseClient { } /// Get the database - pub async fn get_database( - &self, - ctx: Context, - options: GetDatabaseOptions, - ) -> crate::Result { - let mut request = self - .cosmos_client() - .prepare_request_pipeline(&format!("dbs/{}", self.database_name()), http::Method::GET); - - options.decorate_request(&mut request)?; - let response = self - .pipeline() - .send(ctx.clone().insert(ResourceType::Databases), &mut request) - .await?; - - Ok(GetDatabaseResponse::try_from(response).await?) + pub fn get_database(&self) -> GetDatabaseBuilder { + GetDatabaseBuilder::new(self.clone()) } /// Delete the database diff --git a/sdk/data_cosmos/src/clients/user_client.rs b/sdk/data_cosmos/src/clients/user_client.rs index d95b92fb5d..fc385640ab 100644 --- a/sdk/data_cosmos/src/clients/user_client.rs +++ b/sdk/data_cosmos/src/clients/user_client.rs @@ -79,19 +79,8 @@ impl UserClient { } /// Delete the user - pub async fn delete_user( - &self, - ctx: Context, - options: DeleteUserOptions, - ) -> crate::Result { - let mut request = self.prepare_request_with_user_name(http::Method::DELETE); - options.decorate_request(&mut request)?; - let response = self - .pipeline() - .send(ctx.clone().insert(ResourceType::Users), &mut request) - .await?; - - Ok(DeleteUserResponse::try_from(response).await?) + pub fn delete_user(&self) -> DeleteUserBuilder { + DeleteUserBuilder::new(self.clone()) } /// List the user's permissions diff --git a/sdk/data_cosmos/src/operations/delete_user.rs b/sdk/data_cosmos/src/operations/delete_user.rs index 57dc4390e9..010c5ce176 100644 --- a/sdk/data_cosmos/src/operations/delete_user.rs +++ b/sdk/data_cosmos/src/operations/delete_user.rs @@ -1,30 +1,57 @@ use crate::headers::from_headers::*; use crate::prelude::*; -use azure_core::{ - headers::session_token_from_headers, Request as HttpRequest, Response as HttpResponse, -}; +use azure_core::{headers::session_token_from_headers, Context, Response as HttpResponse}; -#[derive(Debug, Clone, Default)] -pub struct DeleteUserOptions { +#[derive(Debug, Clone)] +pub struct DeleteUserBuilder { + client: UserClient, consistency_level: Option, + context: Context, } -impl DeleteUserOptions { - pub fn new() -> Self { +impl DeleteUserBuilder { + pub(crate) fn new(client: UserClient) -> Self { Self { + client, consistency_level: None, + context: Context::new(), } } setters! { consistency_level: ConsistencyLevel => Some(consistency_level), + context: Context => context, + } + + pub fn into_future(self) -> DeleteUser { + Box::pin(async move { + let mut request = self + .client + .prepare_request_with_user_name(http::Method::DELETE); + azure_core::headers::add_optional_header2(&self.consistency_level, &mut request)?; + request.set_body(bytes::Bytes::from_static(&[]).into()); + let response = self + .client + .pipeline() + .send( + self.context.clone().insert(ResourceType::Users), + &mut request, + ) + .await?; + + DeleteUserResponse::try_from(response).await + }) } +} - pub(crate) fn decorate_request(&self, request: &mut HttpRequest) -> crate::Result<()> { - azure_core::headers::add_optional_header2(&self.consistency_level, request)?; - request.set_body(bytes::Bytes::from_static(&[]).into()); +type DeleteUser = futures::future::BoxFuture<'static, crate::Result>; - Ok(()) +#[cfg(feature = "into_future")] +impl std::future::IntoFuture for DeleteUserBuilder { + type Future = DeleteUser; + type Output = ::Output; + fn into_future(self) -> Self::Future { + Self::into_future(self) } } diff --git a/sdk/data_cosmos/src/operations/get_collection.rs b/sdk/data_cosmos/src/operations/get_collection.rs index bddc1865c4..14127119c6 100644 --- a/sdk/data_cosmos/src/operations/get_collection.rs +++ b/sdk/data_cosmos/src/operations/get_collection.rs @@ -4,29 +4,60 @@ use crate::headers::from_headers::*; use azure_core::headers::{ content_type_from_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 GetCollectionOptions { +pub struct GetCollectionBuilder { + client: CollectionClient, consistency_level: Option, + context: Context, } -impl GetCollectionOptions { - pub fn new() -> Self { +impl GetCollectionBuilder { + pub(crate) fn new(client: CollectionClient) -> Self { Self { + client, consistency_level: None, + context: Context::new(), } } setters! { consistency_level: ConsistencyLevel => Some(consistency_level), + context: Context => context, } - pub(crate) fn decorate_request(&self, request: &mut HttpRequest) -> crate::Result<()> { - azure_core::headers::add_optional_header2(&self.consistency_level, request)?; + pub fn into_future(self) -> GetCollection { + Box::pin(async move { + let mut request = self + .client + .prepare_request_with_collection_name(http::Method::GET); - Ok(()) + azure_core::headers::add_optional_header2(&self.consistency_level, &mut request)?; + + let response = self + .client + .pipeline() + .send( + self.context.clone().insert(ResourceType::Collections), + &mut request, + ) + .await?; + + GetCollectionResponse::try_from(response).await + }) + } +} + +type GetCollection = futures::future::BoxFuture<'static, crate::Result>; + +#[cfg(feature = "into_future")] +impl std::future::IntoFuture for GetCollectionBuilder { + type Future = GetCollection; + type Output = ::Output; + fn into_future(self) -> Self::Future { + Self::into_future(self) } } diff --git a/sdk/data_cosmos/src/operations/get_database.rs b/sdk/data_cosmos/src/operations/get_database.rs index 8b60635e59..d1904e2fac 100644 --- a/sdk/data_cosmos/src/operations/get_database.rs +++ b/sdk/data_cosmos/src/operations/get_database.rs @@ -3,30 +3,62 @@ use crate::prelude::*; 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::Context; +use azure_core::{collect_pinned_stream, Response as HttpResponse}; use chrono::{DateTime, Utc}; -#[derive(Debug, Clone, Default)] -pub struct GetDatabaseOptions { +#[derive(Debug, Clone)] +pub struct GetDatabaseBuilder { + client: DatabaseClient, consistency_level: Option, + context: Context, } -impl GetDatabaseOptions { - pub fn new() -> Self { +impl GetDatabaseBuilder { + pub(crate) fn new(client: DatabaseClient) -> Self { Self { + client, consistency_level: None, + context: Context::new(), } } setters! { consistency_level: ConsistencyLevel => Some(consistency_level), + context: Context => context, } - pub(crate) fn decorate_request(&self, request: &mut HttpRequest) -> crate::Result<()> { - azure_core::headers::add_optional_header2(&self.consistency_level, request)?; - request.set_body(bytes::Bytes::from_static(&[]).into()); + pub fn into_future(self) -> GetDatabase { + Box::pin(async move { + let mut request = self.client.cosmos_client().prepare_request_pipeline( + &format!("dbs/{}", self.client.database_name()), + http::Method::GET, + ); + + azure_core::headers::add_optional_header2(&self.consistency_level, &mut request)?; + request.set_body(bytes::Bytes::from_static(&[]).into()); + let response = self + .client + .pipeline() + .send( + self.context.clone().insert(ResourceType::Databases), + &mut request, + ) + .await?; + + GetDatabaseResponse::try_from(response).await + }) + } +} + +type GetDatabase = futures::future::BoxFuture<'static, crate::Result>; - Ok(()) +#[cfg(feature = "into_future")] +impl std::future::IntoFuture for GetDatabaseBuilder { + type Future = GetDatabase; + type Output = ::Output; + fn into_future(self) -> Self::Future { + Self::into_future(self) } } diff --git a/sdk/data_cosmos/tests/collection_operations.rs b/sdk/data_cosmos/tests/collection_operations.rs index b42a6d9e07..aeb1727338 100644 --- a/sdk/data_cosmos/tests/collection_operations.rs +++ b/sdk/data_cosmos/tests/collection_operations.rs @@ -41,9 +41,7 @@ async fn collection_operations() -> Result<(), BoxedError> { let collection_client = db_client.clone().into_collection_client(collection_name); // get collection! - let get_collection_response = collection_client - .get_collection(context.clone(), GetCollectionOptions::new()) - .await?; + let get_collection_response = collection_client.get_collection().into_future().await?; assert_eq!(get_collection_response.collection.id, collection_name); diff --git a/sdk/data_cosmos/tests/cosmos_collection.rs b/sdk/data_cosmos/tests/cosmos_collection.rs index 880ed8b913..e1e47595df 100644 --- a/sdk/data_cosmos/tests/cosmos_collection.rs +++ b/sdk/data_cosmos/tests/cosmos_collection.rs @@ -41,7 +41,8 @@ async fn create_and_delete_collection() { .into_collection_client(COLLECTION_NAME); let collection_after_get = collection_client - .get_collection(Context::new(), GetCollectionOptions::new()) + .get_collection() + .into_future() .await .unwrap(); assert!(collection.collection.rid == collection_after_get.collection.rid); diff --git a/sdk/data_cosmos/tests/cosmos_database.rs b/sdk/data_cosmos/tests/cosmos_database.rs index 25a9f6b679..0852ff4fee 100644 --- a/sdk/data_cosmos/tests/cosmos_database.rs +++ b/sdk/data_cosmos/tests/cosmos_database.rs @@ -2,7 +2,6 @@ mod setup; -use azure_core::prelude::*; use azure_data_cosmos::prelude::*; use futures::stream::StreamExt; @@ -39,7 +38,8 @@ async fn create_and_delete_database() { let database_after_get = client .clone() .into_database_client(DATABASE_NAME) - .get_database(Context::new(), GetDatabaseOptions::new()) + .get_database() + .into_future() .await .unwrap(); assert!(database.database.rid == database_after_get.database.rid); diff --git a/sdk/data_cosmos/tests/user.rs b/sdk/data_cosmos/tests/user.rs index a4456f7050..08519a10ad 100644 --- a/sdk/data_cosmos/tests/user.rs +++ b/sdk/data_cosmos/tests/user.rs @@ -69,10 +69,7 @@ async fn users() { let user_client = database_client.clone().into_user_client(USER_NAME_REPLACED); - let _delete_user_response = user_client - .delete_user(Context::new(), DeleteUserOptions::new()) - .await - .unwrap(); + let _delete_user_response = user_client.delete_user().into_future().await.unwrap(); let list_users_response = Box::pin(database_client.list_users(Context::new(), ListUsersOptions::new()))