Skip to content

Commit 2e300c0

Browse files
committed
GetDocument::into_future
1 parent ee80254 commit 2e300c0

File tree

6 files changed

+62
-55
lines changed

6 files changed

+62
-55
lines changed

sdk/data_cosmos/examples/document_00.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
156156
let get_document_response = collection_client
157157
.clone()
158158
.into_document_client(doc.id.clone(), &doc.id)?
159-
.get_document::<MySampleStruct>(Context::new(), GetDocumentOptions::new())
159+
.get_document()
160+
.into_future::<MySampleStruct>()
160161
.await?;
161162
println!("get_document_response == {:#?}", get_document_response);
162163

sdk/data_cosmos/examples/document_entries_00.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,11 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
115115
let id = format!("unique_id{}", 3);
116116
let partition_key = &id;
117117

118-
let response = client
118+
let response: GetDocumentResponse<MySampleStruct> = client
119119
.clone()
120120
.into_document_client(id.clone(), partition_key)?
121-
.get_document::<MySampleStruct>(
122-
Context::new(),
123-
GetDocumentOptions::new().consistency_level(session_token),
124-
)
121+
.get_document()
122+
.into_future()
125123
.await?;
126124

127125
assert!(matches!(response, GetDocumentResponse::Found(_)));
@@ -155,14 +153,12 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
155153
// has_been_found == false
156154
println!("\n\nLooking for non-existing item");
157155
let id = format!("unique_id{}", 100);
158-
159-
let response = client
156+
let response: GetDocumentResponse<MySampleStruct> = client
160157
.clone()
161158
.into_document_client(id.clone(), &id)?
162-
.get_document::<MySampleStruct>(
163-
Context::new(),
164-
GetDocumentOptions::new().consistency_level(&response),
165-
)
159+
.get_document()
160+
.consistency_level(&response)
161+
.into_future()
166162
.await?;
167163

168164
assert!(matches!(response, GetDocumentResponse::NotFound(_)));

sdk/data_cosmos/examples/document_entries_01.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,18 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
6262
let get_document_response = client
6363
.clone()
6464
.into_document_client(doc.id.clone(), &doc.id)?
65-
.get_document::<serde_json::Value>(
66-
Context::new(),
67-
GetDocumentOptions::new().consistency_level(&create_document_response),
68-
)
65+
.get_document()
66+
.consistency_level(&create_document_response)
67+
.into_future::<serde_json::Value>()
6968
.await?;
7069
println!("get_document_response == {:#?}", get_document_response);
7170

7271
let get_document_response = client
7372
.clone()
7473
.into_document_client("ciccia", &doc.id)?
75-
.get_document::<serde_json::Value>(
76-
Context::new(),
77-
GetDocumentOptions::new().consistency_level(&create_document_response),
78-
)
74+
.get_document()
75+
.consistency_level(&create_document_response)
76+
.into_future::<serde_json::Value>()
7977
.await?;
8078
println!(
8179
"get_document_response == {:#?}\n\n\n",

sdk/data_cosmos/src/clients/document_client.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::operations::*;
33
use crate::resources::ResourceType;
44
use crate::{requests, ReadonlyString};
55
use azure_core::{Context, HttpClient, Request};
6-
use serde::de::DeserializeOwned;
76
use serde::Serialize;
87

98
/// A client for Cosmos document resources.
@@ -59,25 +58,8 @@ impl DocumentClient {
5958
}
6059

6160
/// Get a document
62-
pub async fn get_document<T>(
63-
&self,
64-
ctx: Context,
65-
options: GetDocumentOptions,
66-
) -> crate::Result<GetDocumentResponse<T>>
67-
where
68-
T: DeserializeOwned,
69-
{
70-
let mut request = self.prepare_request_pipeline_with_document_name(http::Method::GET);
71-
72-
options.decorate_request(&mut request)?;
73-
74-
let response = self
75-
.cosmos_client()
76-
.pipeline()
77-
.send(ctx.clone().insert(ResourceType::Documents), &mut request)
78-
.await?;
79-
80-
GetDocumentResponse::try_from(response).await
61+
pub fn get_document(&self) -> GetDocumentBuilder {
62+
GetDocumentBuilder::new(self.clone())
8163
}
8264

8365
/// replace a document in a collection

sdk/data_cosmos/src/operations/get_document.rs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,28 @@ use crate::resources::Document;
44
use crate::ResourceQuota;
55
use azure_core::headers::{etag_from_headers, session_token_from_headers};
66
use azure_core::prelude::*;
7-
use azure_core::{
8-
collect_pinned_stream, Request as HttpRequest, Response as HttpResponse, SessionToken,
9-
};
7+
use azure_core::{collect_pinned_stream, Response as HttpResponse, SessionToken};
108
use chrono::{DateTime, Utc};
119
use http::{HeaderMap, StatusCode};
1210
use serde::de::DeserializeOwned;
1311

1412
#[derive(Debug, Clone)]
15-
pub struct GetDocumentOptions {
13+
pub struct GetDocumentBuilder {
14+
client: DocumentClient,
1615
if_match_condition: Option<IfMatchCondition>,
1716
if_modified_since: Option<IfModifiedSince>,
1817
consistency_level: Option<ConsistencyLevel>,
18+
context: Context,
1919
}
2020

21-
impl GetDocumentOptions {
22-
pub fn new() -> Self {
21+
impl GetDocumentBuilder {
22+
pub(crate) fn new(client: DocumentClient) -> Self {
2323
Self {
24+
client,
2425
if_match_condition: None,
2526
if_modified_since: None,
2627
consistency_level: None,
28+
context: Context::new(),
2729
}
2830
}
2931

@@ -33,14 +35,41 @@ impl GetDocumentOptions {
3335
if_modified_since: DateTime<Utc> => Some(IfModifiedSince::new(if_modified_since)),
3436
}
3537

36-
pub(crate) fn decorate_request(&self, request: &mut HttpRequest) -> 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)?;
38+
pub fn into_future<T: DeserializeOwned>(self) -> GetDocument<T> {
39+
Box::pin(async move {
40+
let mut request = self
41+
.client
42+
.prepare_request_pipeline_with_document_name(http::Method::GET);
43+
44+
azure_core::headers::add_optional_header2(&self.if_match_condition, &mut request)?;
45+
azure_core::headers::add_optional_header2(&self.if_modified_since, &mut request)?;
46+
azure_core::headers::add_optional_header2(&self.consistency_level, &mut request)?;
47+
48+
request.set_body(azure_core::EMPTY_BODY.into());
49+
50+
let response = self
51+
.client
52+
.cosmos_client()
53+
.pipeline()
54+
.send(
55+
self.context.clone().insert(ResourceType::Documents),
56+
&mut request,
57+
)
58+
.await?;
59+
60+
GetDocumentResponse::try_from(response).await
61+
})
62+
}
63+
}
4064

41-
request.set_body(azure_core::EMPTY_BODY.into());
65+
type GetDocument<T> = futures::future::BoxFuture<'static, crate::Result<GetDocumentResponse<T>>>;
4266

43-
Ok(())
67+
#[cfg(feature = "into_future")]
68+
impl std::future::IntoFuture for GetDocumentBuilder {
69+
type Future = GetDocument;
70+
type Output = <GetDocument as std::future::Future>::Output;
71+
fn into_future(self) -> Self::Future {
72+
Self::into_future(self)
4473
}
4574
}
4675

sdk/data_cosmos/tests/cosmos_document.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![cfg(all(test, feature = "test_e2e"))]
22
use azure_core::Context;
3-
use azure_data_cosmos::prelude::GetDocumentOptions;
43
use serde::{Deserialize, Serialize};
54

65
mod setup;
@@ -85,7 +84,8 @@ async fn create_and_delete_document() {
8584
.unwrap();
8685

8786
let document_after_get = document_client
88-
.get_document::<MyDocument>(Context::new(), GetDocumentOptions::new())
87+
.get_document()
88+
.into_future::<MyDocument>()
8989
.await
9090
.unwrap();
9191

@@ -272,7 +272,8 @@ async fn replace_document() {
272272
.into_document_client(DOCUMENT_NAME, &DOCUMENT_NAME)
273273
.unwrap();
274274
let document_after_get = document_client
275-
.get_document::<MyDocument>(Context::new(), GetDocumentOptions::new())
275+
.get_document()
276+
.into_future::<MyDocument>()
276277
.await
277278
.unwrap();
278279

0 commit comments

Comments
 (0)