Skip to content

More into_future implementations #665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sdk/data_cosmos/examples/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
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);
}
Expand Down Expand Up @@ -77,7 +78,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
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);
Expand Down
4 changes: 1 addition & 3 deletions sdk/data_cosmos/examples/create_delete_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

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());
Expand Down
4 changes: 1 addition & 3 deletions sdk/data_cosmos/examples/database_01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
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(())
Expand Down
4 changes: 3 additions & 1 deletion sdk/data_cosmos/examples/get_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
context.insert(custom_headers);

let response = database_client
.get_database(context, GetDatabaseOptions::new())
.get_database()
.context(context)
.into_future()
.await?;
println!("response == {:?}", response);

Expand Down
23 changes: 8 additions & 15 deletions sdk/data_cosmos/examples/permission_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,13 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.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
Expand Down Expand Up @@ -152,12 +146,11 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
);

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);

Expand Down
4 changes: 1 addition & 3 deletions sdk/data_cosmos/examples/user_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

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(())
Expand Down
9 changes: 2 additions & 7 deletions sdk/data_cosmos/examples/user_permission_token.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use azure_core::Context;
use azure_data_cosmos::prelude::*;
use std::error::Error;

Expand Down Expand Up @@ -34,9 +33,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.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?;
Expand Down Expand Up @@ -168,9 +165,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
);

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(())
Expand Down
17 changes: 2 additions & 15 deletions sdk/data_cosmos/src/clients/collection_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,8 @@ impl CollectionClient {
}

/// Get a collection
pub async fn get_collection(
&self,
ctx: Context,
options: GetCollectionOptions,
) -> crate::Result<GetCollectionResponse> {
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
Expand Down
18 changes: 2 additions & 16 deletions sdk/data_cosmos/src/clients/database_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,8 @@ impl DatabaseClient {
}

/// Get the database
pub async fn get_database(
&self,
ctx: Context,
options: GetDatabaseOptions,
) -> crate::Result<GetDatabaseResponse> {
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
Expand Down
15 changes: 2 additions & 13 deletions sdk/data_cosmos/src/clients/user_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,8 @@ impl UserClient {
}

/// Delete the user
pub async fn delete_user(
&self,
ctx: Context,
options: DeleteUserOptions,
) -> crate::Result<DeleteUserResponse> {
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
Expand Down
49 changes: 38 additions & 11 deletions sdk/data_cosmos/src/operations/delete_user.rs
Original file line number Diff line number Diff line change
@@ -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<ConsistencyLevel>,
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<DeleteUserResponse>>;

Ok(())
#[cfg(feature = "into_future")]
impl std::future::IntoFuture for DeleteUserBuilder {
type Future = DeleteUser;
type Output = <DeleteUser as std::future::Future>::Output;
fn into_future(self) -> Self::Future {
Self::into_future(self)
}
}

Expand Down
45 changes: 38 additions & 7 deletions sdk/data_cosmos/src/operations/get_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConsistencyLevel>,
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<GetCollectionResponse>>;

#[cfg(feature = "into_future")]
impl std::future::IntoFuture for GetCollectionBuilder {
type Future = GetCollection;
type Output = <GetCollection as std::future::Future>::Output;
fn into_future(self) -> Self::Future {
Self::into_future(self)
}
}

Expand Down
Loading