|
1 | 1 | use crate::headers::from_headers::*;
|
| 2 | +use crate::prelude::*; |
2 | 3 | use crate::resources::Document;
|
3 | 4 | use crate::ResourceQuota;
|
4 | 5 | use azure_core::headers::{etag_from_headers, session_token_from_headers};
|
| 6 | +use azure_core::prelude::*; |
5 | 7 | use azure_core::SessionToken;
|
| 8 | +use azure_core::{collect_pinned_stream, Request as HttpRequest, Response as HttpResponse}; |
6 | 9 | use chrono::{DateTime, Utc};
|
7 |
| -use http::response::Response; |
8 |
| -use http::StatusCode; |
| 10 | +use http::{HeaderMap, StatusCode}; |
9 | 11 | use serde::de::DeserializeOwned;
|
10 | 12 |
|
| 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 | + |
11 | 58 | #[derive(Debug, Clone)]
|
12 | 59 | pub enum GetDocumentResponse<T> {
|
13 | 60 | Found(Box<FoundDocumentResponse<T>>),
|
14 | 61 | NotFound(Box<NotFoundDocumentResponse>),
|
15 | 62 | }
|
16 | 63 |
|
17 |
| -impl<T> std::convert::TryFrom<Response<bytes::Bytes>> for GetDocumentResponse<T> |
| 64 | +impl<T> GetDocumentResponse<T> |
18 | 65 | where
|
19 | 66 | T: DeserializeOwned,
|
20 | 67 | {
|
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(); |
25 | 70 |
|
26 | 71 | let has_been_found =
|
27 | 72 | status_code == StatusCode::OK || status_code == StatusCode::NOT_MODIFIED;
|
28 | 73 |
|
| 74 | + let body = collect_pinned_stream(pinned_stream).await?; |
| 75 | + |
29 | 76 | if has_been_found {
|
30 | 77 | Ok(GetDocumentResponse::Found(Box::new(
|
31 |
| - FoundDocumentResponse::try_from(response)?, |
| 78 | + FoundDocumentResponse::try_from(&headers, body).await?, |
32 | 79 | )))
|
33 | 80 | } else {
|
34 | 81 | Ok(GetDocumentResponse::NotFound(Box::new(
|
35 |
| - NotFoundDocumentResponse::try_from(response)?, |
| 82 | + NotFoundDocumentResponse::try_from(&headers).await?, |
36 | 83 | )))
|
37 | 84 | }
|
38 | 85 | }
|
@@ -65,18 +112,13 @@ pub struct FoundDocumentResponse<T> {
|
65 | 112 | pub date: DateTime<Utc>,
|
66 | 113 | }
|
67 | 114 |
|
68 |
| -impl<T> std::convert::TryFrom<Response<bytes::Bytes>> for FoundDocumentResponse<T> |
| 115 | +impl<T> FoundDocumentResponse<T> |
69 | 116 | where
|
70 | 117 | T: DeserializeOwned,
|
71 | 118 | {
|
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> { |
78 | 120 | Ok(Self {
|
79 |
| - document: Document::try_from((headers, body))?, |
| 121 | + document: serde_json::from_slice(&body)?, |
80 | 122 |
|
81 | 123 | content_location: content_location_from_headers(headers)?.to_owned(),
|
82 | 124 | last_state_change: last_state_change_from_headers(headers)?,
|
@@ -126,12 +168,8 @@ pub struct NotFoundDocumentResponse {
|
126 | 168 | pub date: DateTime<Utc>,
|
127 | 169 | }
|
128 | 170 |
|
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> { |
135 | 173 | Ok(Self {
|
136 | 174 | content_location: content_location_from_headers(headers)?.to_owned(),
|
137 | 175 | last_state_change: last_state_change_from_headers(headers)?,
|
|
0 commit comments