Skip to content

Commit 4fdb3ef

Browse files
committed
[Cosmos] Start migrating get_document to pipelines
This change begins the migration process for moving get_document to the new pipelines architecture. Most of the code is moved over now, but the example programs still need updated. Issue Azure#290
1 parent 1f2162a commit 4fdb3ef

File tree

6 files changed

+100
-105
lines changed

6 files changed

+100
-105
lines changed

sdk/cosmos/src/clients/document_client.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use super::{AttachmentClient, CollectionClient, CosmosClient, DatabaseClient};
2+
use crate::prelude::{GetDocumentOptions, GetDocumentResponse};
23
use crate::resources::ResourceType;
34
use crate::{requests, ReadonlyString};
4-
use azure_core::HttpClient;
5+
use azure_core::{Context, HttpClient, PipelineContext, Request};
6+
use serde::de::DeserializeOwned;
57
use serde::Serialize;
68

79
/// A client for Cosmos document resources.
@@ -61,8 +63,28 @@ impl DocumentClient {
6163
}
6264

6365
/// Get a document
64-
pub fn get_document(&self) -> requests::GetDocumentBuilder<'_, '_> {
65-
requests::GetDocumentBuilder::new(self)
66+
pub async fn get_document<T>(
67+
&self,
68+
ctx: Context,
69+
options: GetDocumentOptions<'_, '_>,
70+
) -> Result<GetDocumentResponse<T>, crate::Error>
71+
where
72+
T: DeserializeOwned,
73+
{
74+
let mut request = self.prepare_request_pipeline_with_document_name(http::Method::GET);
75+
let mut pipeline_context = PipelineContext::new(ctx, ResourceType::Databases.into());
76+
77+
options.decorate_request(&mut request)?;
78+
79+
let response = self
80+
.cosmos_client()
81+
.pipeline()
82+
.send(&mut pipeline_context, &mut request)
83+
.await?
84+
.validate(http::StatusCode::OK)
85+
.await?;
86+
87+
GetDocumentResponse::try_from(response).await
6688
}
6789

6890
/// Delete a document
@@ -99,6 +121,18 @@ impl DocumentClient {
99121
)
100122
}
101123

124+
fn prepare_request_pipeline_with_document_name(&self, method: http::Method) -> Request {
125+
self.cosmos_client().prepare_request_pipeline(
126+
&format!(
127+
"dbs/{}/colls/{}/docs/{}",
128+
self.database_client().database_name(),
129+
self.collection_client().collection_name(),
130+
self.document_name()
131+
),
132+
method,
133+
)
134+
}
135+
102136
pub(crate) fn http_client(&self) -> &dyn HttpClient {
103137
self.cosmos_client().http_client()
104138
}

sdk/cosmos/src/responses/get_document_response.rs renamed to sdk/cosmos/src/operations/get_document.rs

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,85 @@
11
use crate::headers::from_headers::*;
2+
use crate::prelude::*;
23
use crate::resources::Document;
34
use crate::ResourceQuota;
45
use azure_core::headers::{etag_from_headers, session_token_from_headers};
6+
use azure_core::prelude::*;
57
use azure_core::SessionToken;
8+
use azure_core::{collect_pinned_stream, Request as HttpRequest, Response as HttpResponse};
69
use chrono::{DateTime, Utc};
7-
use http::response::Response;
8-
use http::StatusCode;
10+
use http::{HeaderMap, StatusCode};
911
use serde::de::DeserializeOwned;
1012

13+
#[derive(Debug, Clone)]
14+
pub struct GetDocumentOptions<'a, 'b> {
15+
document_client: &'a DocumentClient,
16+
if_match_condition: Option<IfMatchCondition<'b>>,
17+
if_modified_since: Option<IfModifiedSince<'b>>,
18+
activity_id: Option<ActivityId<'b>>,
19+
consistency_level: Option<ConsistencyLevel>,
20+
}
21+
22+
impl<'a, 'b> GetDocumentOptions<'a, 'b> {
23+
pub fn new(document_client: &'a DocumentClient) -> Self {
24+
Self {
25+
document_client,
26+
if_match_condition: None,
27+
if_modified_since: None,
28+
activity_id: None,
29+
consistency_level: None,
30+
}
31+
}
32+
33+
setters! {
34+
activity_id: &'b str => Some(ActivityId::new(activity_id)),
35+
consistency_level: ConsistencyLevel => Some(consistency_level),
36+
if_match_condition: IfMatchCondition<'b> => Some(if_match_condition),
37+
if_modified_since: &'b DateTime<Utc> => Some(IfModifiedSince::new(if_modified_since)),
38+
}
39+
40+
pub(crate) fn decorate_request(&self, request: &mut HttpRequest) -> Result<(), crate::Error> {
41+
// add trait headers
42+
azure_core::headers::add_optional_header2(&self.if_match_condition, request)?;
43+
azure_core::headers::add_optional_header2(&self.if_modified_since, request)?;
44+
azure_core::headers::add_optional_header2(&self.activity_id, request)?;
45+
azure_core::headers::add_optional_header2(&self.consistency_level, request)?;
46+
47+
crate::cosmos_entity::add_as_partition_key_header_serialized2(
48+
self.document_client.partition_key_serialized(),
49+
request,
50+
);
51+
52+
request.set_body(bytes::Bytes::from_static(EMPTY_BODY).into());
53+
54+
Ok(())
55+
}
56+
}
57+
1158
#[derive(Debug, Clone)]
1259
pub enum GetDocumentResponse<T> {
1360
Found(Box<FoundDocumentResponse<T>>),
1461
NotFound(Box<NotFoundDocumentResponse>),
1562
}
1663

17-
impl<T> std::convert::TryFrom<Response<bytes::Bytes>> for GetDocumentResponse<T>
64+
impl<T> GetDocumentResponse<T>
1865
where
1966
T: DeserializeOwned,
2067
{
21-
type Error = crate::Error;
22-
23-
fn try_from(response: Response<bytes::Bytes>) -> Result<Self, Self::Error> {
24-
let status_code = response.status();
68+
pub async fn try_from(response: HttpResponse) -> Result<Self, crate::Error> {
69+
let (status_code, headers, pinned_stream) = response.deconstruct();
2570

2671
let has_been_found =
2772
status_code == StatusCode::OK || status_code == StatusCode::NOT_MODIFIED;
2873

74+
let body = collect_pinned_stream(pinned_stream).await?;
75+
2976
if has_been_found {
3077
Ok(GetDocumentResponse::Found(Box::new(
31-
FoundDocumentResponse::try_from(response)?,
78+
FoundDocumentResponse::try_from(&headers, body).await?,
3279
)))
3380
} else {
3481
Ok(GetDocumentResponse::NotFound(Box::new(
35-
NotFoundDocumentResponse::try_from(response)?,
82+
NotFoundDocumentResponse::try_from(&headers).await?,
3683
)))
3784
}
3885
}
@@ -65,18 +112,13 @@ pub struct FoundDocumentResponse<T> {
65112
pub date: DateTime<Utc>,
66113
}
67114

68-
impl<T> std::convert::TryFrom<Response<bytes::Bytes>> for FoundDocumentResponse<T>
115+
impl<T> FoundDocumentResponse<T>
69116
where
70117
T: DeserializeOwned,
71118
{
72-
type Error = crate::Error;
73-
74-
fn try_from(response: Response<bytes::Bytes>) -> Result<Self, Self::Error> {
75-
let headers = response.headers();
76-
let body: &[u8] = response.body();
77-
119+
async fn try_from(headers: &HeaderMap, body: bytes::Bytes) -> Result<Self, crate::Error> {
78120
Ok(Self {
79-
document: Document::try_from((headers, body))?,
121+
document: serde_json::from_slice(&body)?,
80122

81123
content_location: content_location_from_headers(headers)?.to_owned(),
82124
last_state_change: last_state_change_from_headers(headers)?,
@@ -126,12 +168,8 @@ pub struct NotFoundDocumentResponse {
126168
pub date: DateTime<Utc>,
127169
}
128170

129-
impl std::convert::TryFrom<Response<bytes::Bytes>> for NotFoundDocumentResponse {
130-
type Error = crate::Error;
131-
132-
fn try_from(response: Response<bytes::Bytes>) -> Result<Self, Self::Error> {
133-
let headers = response.headers();
134-
171+
impl NotFoundDocumentResponse {
172+
async fn try_from(headers: &HeaderMap) -> Result<Self, crate::Error> {
135173
Ok(Self {
136174
content_location: content_location_from_headers(headers)?.to_owned(),
137175
last_state_change: last_state_change_from_headers(headers)?,

sdk/cosmos/src/operations/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod create_permission;
99
mod create_user;
1010
mod delete_permission;
1111
mod get_database;
12+
mod get_document;
1213
mod get_permission;
1314
mod get_user;
1415
mod list_databases;
@@ -22,6 +23,7 @@ pub use create_permission::*;
2223
pub use create_user::*;
2324
pub use delete_permission::*;
2425
pub use get_database::*;
26+
pub use get_document::*;
2527
pub use get_permission::*;
2628
pub use get_user::*;
2729
pub use list_databases::*;

sdk/cosmos/src/requests/get_document_builder.rs

Lines changed: 0 additions & 75 deletions
This file was deleted.

sdk/cosmos/src/requests/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ mod delete_user_defined_function_builder;
2222
mod execute_stored_procedure_builder;
2323
mod get_attachment_builder;
2424
mod get_collection_builder;
25-
mod get_document_builder;
2625
mod get_partition_key_ranges_builder;
2726
mod list_attachments_builder;
2827
mod list_collections_builder;
@@ -55,7 +54,6 @@ pub use delete_user_defined_function_builder::DeleteUserDefinedFunctionBuilder;
5554
pub use execute_stored_procedure_builder::ExecuteStoredProcedureBuilder;
5655
pub use get_attachment_builder::GetAttachmentBuilder;
5756
pub use get_collection_builder::GetCollectionBuilder;
58-
pub use get_document_builder::GetDocumentBuilder;
5957
pub use get_partition_key_ranges_builder::GetPartitionKeyRangesBuilder;
6058
pub use list_attachments_builder::ListAttachmentsBuilder;
6159
pub use list_collections_builder::ListCollectionsBuilder;

sdk/cosmos/src/responses/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ mod delete_user_response;
1919
mod execute_stored_procedure_response;
2020
mod get_attachment_response;
2121
mod get_collection_response;
22-
mod get_document_response;
2322
mod get_partition_key_ranges_response;
2423
mod list_attachments_response;
2524
mod list_collections_response;
@@ -51,7 +50,6 @@ pub use delete_user_response::DeleteUserResponse;
5150
pub use execute_stored_procedure_response::ExecuteStoredProcedureResponse;
5251
pub use get_attachment_response::GetAttachmentResponse;
5352
pub use get_collection_response::GetCollectionResponse;
54-
pub use get_document_response::GetDocumentResponse;
5553
pub use get_partition_key_ranges_response::GetPartitionKeyRangesResponse;
5654
pub use list_attachments_response::ListAttachmentsResponse;
5755
pub use list_collections_response::ListCollectionsResponse;

0 commit comments

Comments
 (0)