Skip to content

Commit 94eebdd

Browse files
committed
CreateUser::into_future
1 parent 95d4d8f commit 94eebdd

File tree

7 files changed

+72
-40
lines changed

7 files changed

+72
-40
lines changed

sdk/data_cosmos/examples/permission_00.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
5858
get_collection2_response
5959
);
6060

61-
let create_user_response = user_client
62-
.create_user(Context::new(), CreateUserOptions::default())
63-
.await?;
61+
let create_user_response = user_client.create_user().into_future().await?;
6462
println!("create_user_response == {:#?}", create_user_response);
6563

6664
// create the first permission!

sdk/data_cosmos/examples/user_00.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
3030
let user_client = database_client.clone().into_user_client(user_name.clone());
3131

3232
let create_user_response = user_client
33-
.create_user(Context::new(), CreateUserOptions::new())
33+
.create_user()
34+
.into_future()
3435
.await?;
3536
println!("create_user_response == {:#?}", create_user_response);
3637

sdk/data_cosmos/examples/user_permission_token.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
3939
.await?;
4040
println!("get_collection_response == {:#?}", get_collection_response);
4141

42-
let create_user_response = user_client
43-
.create_user(Context::new(), CreateUserOptions::default())
44-
.await?;
42+
let create_user_response = user_client.create_user().into_future().await?;
4543
println!("create_user_response == {:#?}", create_user_response);
4644

4745
// test list documents

sdk/data_cosmos/src/clients/user_client.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,8 @@ impl UserClient {
3939
}
4040

4141
/// Create the user
42-
pub async fn create_user(
43-
&self,
44-
ctx: Context,
45-
options: CreateUserOptions,
46-
) -> crate::Result<UserResponse> {
47-
let mut request = self.cosmos_client().prepare_request_pipeline(
48-
&format!("dbs/{}/users", self.database_client.database_name()),
49-
http::Method::POST,
50-
);
51-
52-
options.decorate_request(&mut request, self.user_name())?;
53-
let response = self
54-
.pipeline()
55-
.send(ctx.clone().insert(ResourceType::Users), &mut request)
56-
.await?;
57-
58-
Ok(UserResponse::try_from(response).await?)
42+
pub fn create_user(&self) -> CreateUserBuilder {
43+
CreateUserBuilder::new(self.clone())
5944
}
6045

6146
/// Get the user
@@ -137,7 +122,7 @@ impl UserClient {
137122
self.cosmos_client().http_client()
138123
}
139124

140-
fn pipeline(&self) -> &Pipeline {
125+
pub(crate) fn pipeline(&self) -> &Pipeline {
141126
self.cosmos_client().pipeline()
142127
}
143128
}

sdk/data_cosmos/src/operations/create_document.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ impl<D: Serialize + CosmosEntity + Send + 'static> CreateDocumentBuilder<D> {
9494

9595
type CreateDocument = futures::future::BoxFuture<'static, crate::Result<CreateDocumentResponse>>;
9696

97+
#[cfg(feature = "into_future")]
98+
impl <D: Serialize + CosmosEntity + Send + 'static> std::future::IntoFuture for CreateDocumentBuilder<D> {
99+
type Future = CreateDocument;
100+
type Output = <CreateDocument as std::future::Future>::Output;
101+
fn into_future(self) -> Self::Future {
102+
Self::into_future(self)
103+
}
104+
}
105+
97106
#[derive(Debug, Clone)]
98107
pub struct CreateDocumentResponse {
99108
pub document_attributes: DocumentAttributes,

sdk/data_cosmos/src/operations/create_permission.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,12 @@ impl CreatePermissionBuilder {
7272
}
7373

7474
type CreatePermission = futures::future::BoxFuture<'static, crate::Result<PermissionResponse>>;
75+
76+
#[cfg(feature = "into_future")]
77+
impl std::future::IntoFuture for CreatePermissionBuilder {
78+
type Future = CreatePermission;
79+
type Output = <CreatePermission as std::future::Future>::Output;
80+
fn into_future(self) -> Self::Future {
81+
Self::into_future(self)
82+
}
83+
}

sdk/data_cosmos/src/operations/create_user.rs

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,63 @@
1-
use crate::prelude::*;
2-
use azure_core::Request as HttpRequest;
1+
use crate::{prelude::*, resources::user::UserResponse};
2+
use azure_core::Context;
33

4-
#[derive(Debug, Clone, Default)]
5-
pub struct CreateUserOptions {
4+
#[derive(Debug, Clone)]
5+
pub struct CreateUserBuilder {
6+
client: UserClient,
67
consistency_level: Option<ConsistencyLevel>,
8+
context: Context,
79
}
810

9-
impl CreateUserOptions {
10-
pub fn new() -> Self {
11+
impl CreateUserBuilder {
12+
pub(crate) fn new(client: UserClient) -> Self {
1113
Self {
14+
client,
1215
consistency_level: None,
16+
context: Context::new(),
1317
}
1418
}
1519

1620
setters! {
1721
consistency_level: ConsistencyLevel => Some(consistency_level),
1822
}
1923

20-
pub(crate) fn decorate_request(
21-
&self,
22-
request: &mut HttpRequest,
23-
user_name: &str,
24-
) -> crate::Result<()> {
25-
azure_core::headers::add_optional_header2(&self.consistency_level, request)?;
26-
let body = CreateUserBody { id: user_name };
27-
request.set_body(bytes::Bytes::from(serde_json::to_string(&body)?).into());
28-
Ok(())
24+
pub fn into_future(self) -> CreateUser {
25+
Box::pin(async move {
26+
let mut request = self.client.cosmos_client().prepare_request_pipeline(
27+
&format!(
28+
"dbs/{}/users",
29+
self.client.database_client().database_name()
30+
),
31+
http::Method::POST,
32+
);
33+
34+
azure_core::headers::add_optional_header2(&self.consistency_level, &mut request)?;
35+
let body = CreateUserBody {
36+
id: self.client.user_name(),
37+
};
38+
request.set_body(bytes::Bytes::from(serde_json::to_string(&body)?).into());
39+
let response = self
40+
.client
41+
.pipeline()
42+
.send(
43+
self.context.clone().insert(ResourceType::Users),
44+
&mut request,
45+
)
46+
.await?;
47+
48+
Ok(UserResponse::try_from(response).await?)
49+
})
50+
}
51+
}
52+
53+
type CreateUser = futures::future::BoxFuture<'static, crate::Result<UserResponse>>;
54+
55+
#[cfg(feature = "into_future")]
56+
impl std::future::IntoFuture for CreateUserBuilder {
57+
type Future = CreateUser;
58+
type Output = <CreateUser as std::future::Future>::Output;
59+
fn into_future(self) -> Self::Future {
60+
Self::into_future(self)
2961
}
3062
}
3163

0 commit comments

Comments
 (0)