Skip to content

Commit 63dfdf2

Browse files
committed
DeleteDocument::into_future
1 parent 2ffd963 commit 63dfdf2

File tree

5 files changed

+61
-50
lines changed

5 files changed

+61
-50
lines changed

sdk/data_cosmos/examples/document_entries_00.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
173173
client
174174
.clone()
175175
.into_document_client(id.clone(), &id)?
176-
.delete_document(
177-
Context::new(),
178-
DeleteDocumentOptions::new().consistency_level(&response),
179-
)
176+
.delete_document()
177+
.into_future()
180178
.await?;
181179
}
182180
println!("Cleaned up");

sdk/data_cosmos/examples/readme.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use azure_core::Context;
21
use serde::{Deserialize, Serialize};
32
// Using the prelude module of the Cosmos crate makes easier to use the Rust Azure SDK for Cosmos.
43
use azure_data_cosmos::prelude::*;
@@ -147,12 +146,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
147146
collection_client
148147
.clone()
149148
.into_document_client(document.result.id.clone(), &document.result.a_number)?
150-
.delete_document(
151-
Context::new(),
152-
DeleteDocumentOptions::new()
153-
.consistency_level(session_token.clone())
154-
.if_match_condition(&document.document_attributes),
155-
)
149+
.delete_document()
150+
.consistency_level(session_token.clone())
151+
.if_match_condition(&document.document_attributes)
152+
.into_future()
156153
.await?;
157154
}
158155

sdk/data_cosmos/examples/remove_all_documents.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use azure_core::prelude::*;
21
use azure_data_cosmos::prelude::*;
32
use futures::stream::StreamExt;
43
use serde_json::Value;
@@ -74,7 +73,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
7473
client
7574
.clone()
7675
.into_document_client(id.clone(), &partition_key)?
77-
.delete_document(Context::new(), DeleteDocumentOptions::new())
76+
.delete_document()
77+
.into_future()
7878
.await?;
7979
}
8080

sdk/data_cosmos/src/clients/document_client.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,8 @@ impl DocumentClient {
101101
}
102102

103103
/// Delete a document
104-
pub async fn delete_document(
105-
&self,
106-
ctx: Context,
107-
options: DeleteDocumentOptions,
108-
) -> crate::Result<DeleteDocumentResponse> {
109-
let mut request = self.prepare_request_pipeline_with_document_name(http::Method::DELETE);
110-
111-
options.decorate_request(&mut request, self.partition_key_serialized())?;
112-
113-
let response = self
114-
.cosmos_client()
115-
.pipeline()
116-
.send(ctx.clone().insert(ResourceType::Documents), &mut request)
117-
.await?;
118-
119-
DeleteDocumentResponse::try_from(response).await
104+
pub fn delete_document(&self) -> DeleteDocumentBuilder {
105+
DeleteDocumentBuilder::new(self.clone())
120106
}
121107

122108
/// List all attachments for a document
@@ -132,7 +118,10 @@ impl DocumentClient {
132118
AttachmentClient::new(self, attachment_name)
133119
}
134120

135-
fn prepare_request_pipeline_with_document_name(&self, method: http::Method) -> Request {
121+
pub(crate) fn prepare_request_pipeline_with_document_name(
122+
&self,
123+
method: http::Method,
124+
) -> Request {
136125
self.cosmos_client().prepare_request_pipeline(
137126
&format!(
138127
"dbs/{}/colls/{}/docs/{}",

sdk/data_cosmos/src/operations/delete_document.rs

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1+
use crate::headers::from_headers::*;
12
use crate::prelude::*;
23

4+
use azure_core::headers::session_token_from_headers;
35
use azure_core::prelude::*;
4-
use azure_core::{Request as HttpRequest, Response as HttpResponse};
6+
use azure_core::Response as HttpResponse;
57
use chrono::{DateTime, Utc};
68

79
#[derive(Debug, Clone)]
8-
pub struct DeleteDocumentOptions {
10+
pub struct DeleteDocumentBuilder {
11+
client: DocumentClient,
912
if_match_condition: Option<IfMatchCondition>,
1013
if_modified_since: Option<IfModifiedSince>,
1114
consistency_level: Option<ConsistencyLevel>,
1215
allow_tentative_writes: TentativeWritesAllowance,
16+
context: Context,
1317
}
1418

15-
impl DeleteDocumentOptions {
16-
pub fn new() -> DeleteDocumentOptions {
19+
impl DeleteDocumentBuilder {
20+
pub(crate) fn new(client: DocumentClient) -> DeleteDocumentBuilder {
1721
Self {
22+
client,
1823
if_match_condition: None,
1924
if_modified_since: None,
2025
consistency_level: None,
2126
allow_tentative_writes: TentativeWritesAllowance::Deny,
27+
context: Context::new(),
2228
}
2329
}
2430

@@ -27,29 +33,50 @@ impl DeleteDocumentOptions {
2733
if_match_condition: IfMatchCondition => Some(if_match_condition),
2834
allow_tentative_writes: TentativeWritesAllowance,
2935
if_modified_since: DateTime<Utc> => Some(IfModifiedSince::new(if_modified_since)),
36+
context: Context => context,
3037
}
3138

32-
pub fn decorate_request(
33-
&self,
34-
request: &mut HttpRequest,
35-
serialized_partition_key: &str,
36-
) -> crate::Result<()> {
37-
azure_core::headers::add_optional_header2(&self.if_match_condition, request)?;
38-
azure_core::headers::add_optional_header2(&self.if_modified_since, request)?;
39-
azure_core::headers::add_optional_header2(&self.consistency_level, request)?;
40-
azure_core::headers::add_mandatory_header2(&self.allow_tentative_writes, request)?;
39+
pub fn into_future(self) -> DeleteDocument {
40+
Box::pin(async move {
41+
let mut request = self
42+
.client
43+
.prepare_request_pipeline_with_document_name(http::Method::DELETE);
44+
45+
azure_core::headers::add_optional_header2(&self.if_match_condition, &mut request)?;
46+
azure_core::headers::add_optional_header2(&self.if_modified_since, &mut request)?;
47+
azure_core::headers::add_optional_header2(&self.consistency_level, &mut request)?;
48+
azure_core::headers::add_mandatory_header2(&self.allow_tentative_writes, &mut request)?;
4149

42-
crate::cosmos_entity::add_as_partition_key_header_serialized2(
43-
serialized_partition_key,
44-
request,
45-
);
50+
crate::cosmos_entity::add_as_partition_key_header_serialized2(
51+
&self.client.partition_key_serialized(),
52+
&mut request,
53+
);
4654

47-
Ok(())
55+
let response = self
56+
.client
57+
.cosmos_client()
58+
.pipeline()
59+
.send(
60+
self.context.clone().insert(ResourceType::Documents),
61+
&mut request,
62+
)
63+
.await?;
64+
65+
DeleteDocumentResponse::try_from(response).await
66+
})
4867
}
4968
}
5069

51-
use crate::headers::from_headers::*;
52-
use azure_core::headers::session_token_from_headers;
70+
type DeleteDocument = futures::future::BoxFuture<'static, crate::Result<DeleteDocumentResponse>>;
71+
72+
#[cfg(feature = "into_future")]
73+
impl std::future::IntoFuture for DeleteDocumentBuilder {
74+
type Future = DeleteDocument;
75+
type Output = <DeleteDocument as std::future::Future>::Output;
76+
fn into_future(self) -> Self::Future {
77+
Self::into_future(self)
78+
}
79+
}
5380

5481
#[derive(Debug, Clone)]
5582
pub struct DeleteDocumentResponse {

0 commit comments

Comments
 (0)