Skip to main content

azure_sdk_cosmos/requests/
delete_document_builder.rs

1use crate::prelude::*;
2use crate::responses::DeleteDocumentResponse;
3use crate::DocumentClientRequired;
4use azure_sdk_core::errors::{check_status_extract_headers_and_body, AzureError};
5use azure_sdk_core::modify_conditions::IfMatchCondition;
6use azure_sdk_core::prelude::*;
7use azure_sdk_core::{IfMatchConditionOption, IfMatchConditionSupport};
8use chrono::{DateTime, Utc};
9use hyper::StatusCode;
10use std::convert::TryInto;
11
12#[derive(Debug, Clone)]
13pub struct DeleteDocumentBuilder<'a, C, D, COLL>
14where
15    C: CosmosClient,
16    D: DatabaseClient<C>,
17    COLL: CollectionClient<C, D>,
18{
19    document_client: &'a dyn DocumentClient<C, D, COLL>,
20    if_match_condition: Option<IfMatchCondition<'a>>,
21    if_modified_since: Option<&'a DateTime<Utc>>,
22    user_agent: Option<&'a str>,
23    activity_id: Option<&'a str>,
24    consistency_level: Option<ConsistencyLevel<'a>>,
25    allow_tentative_writes: bool,
26}
27
28impl<'a, C, D, COLL> DeleteDocumentBuilder<'a, C, D, COLL>
29where
30    C: CosmosClient,
31    D: DatabaseClient<C>,
32    COLL: CollectionClient<C, D>,
33{
34    #[inline]
35    pub(crate) fn new(
36        document_client: &'a dyn DocumentClient<C, D, COLL>,
37    ) -> DeleteDocumentBuilder<'a, C, D, COLL> {
38        DeleteDocumentBuilder {
39            document_client,
40            if_match_condition: None,
41            if_modified_since: None,
42            user_agent: None,
43            activity_id: None,
44            consistency_level: None,
45            allow_tentative_writes: false,
46        }
47    }
48}
49
50impl<'a, C, D, COLL> DocumentClientRequired<'a, C, D, COLL>
51    for DeleteDocumentBuilder<'a, C, D, COLL>
52where
53    C: CosmosClient,
54    D: DatabaseClient<C>,
55    COLL: CollectionClient<C, D>,
56{
57    #[inline]
58    fn document_client(&self) -> &'a dyn DocumentClient<C, D, COLL> {
59        self.document_client
60    }
61}
62
63//get mandatory no traits methods
64
65//set mandatory no traits methods
66impl<'a, C, D, COLL> IfMatchConditionOption<'a> for DeleteDocumentBuilder<'a, C, D, COLL>
67where
68    C: CosmosClient,
69    D: DatabaseClient<C>,
70    COLL: CollectionClient<C, D>,
71{
72    #[inline]
73    fn if_match_condition(&self) -> Option<IfMatchCondition<'a>> {
74        self.if_match_condition
75    }
76}
77
78impl<'a, C, D, COLL> IfModifiedSinceOption<'a> for DeleteDocumentBuilder<'a, C, D, COLL>
79where
80    C: CosmosClient,
81    D: DatabaseClient<C>,
82    COLL: CollectionClient<C, D>,
83{
84    #[inline]
85    fn if_modified_since(&self) -> Option<&'a DateTime<Utc>> {
86        self.if_modified_since
87    }
88}
89
90impl<'a, C, D, COLL> UserAgentOption<'a> for DeleteDocumentBuilder<'a, C, D, COLL>
91where
92    C: CosmosClient,
93    D: DatabaseClient<C>,
94    COLL: CollectionClient<C, D>,
95{
96    #[inline]
97    fn user_agent(&self) -> Option<&'a str> {
98        self.user_agent
99    }
100}
101
102impl<'a, C, D, COLL> ActivityIdOption<'a> for DeleteDocumentBuilder<'a, C, D, COLL>
103where
104    C: CosmosClient,
105    D: DatabaseClient<C>,
106    COLL: CollectionClient<C, D>,
107{
108    #[inline]
109    fn activity_id(&self) -> Option<&'a str> {
110        self.activity_id
111    }
112}
113
114impl<'a, C, D, COLL> ConsistencyLevelOption<'a> for DeleteDocumentBuilder<'a, C, D, COLL>
115where
116    C: CosmosClient,
117    D: DatabaseClient<C>,
118    COLL: CollectionClient<C, D>,
119{
120    #[inline]
121    fn consistency_level(&self) -> Option<ConsistencyLevel<'a>> {
122        self.consistency_level.clone()
123    }
124}
125
126impl<'a, C, D, COLL> AllowTentativeWritesOption for DeleteDocumentBuilder<'a, C, D, COLL>
127where
128    C: CosmosClient,
129    D: DatabaseClient<C>,
130    COLL: CollectionClient<C, D>,
131{
132    #[inline]
133    fn allow_tentative_writes(&self) -> bool {
134        self.allow_tentative_writes
135    }
136}
137
138impl<'a, C, D, COLL> IfMatchConditionSupport<'a> for DeleteDocumentBuilder<'a, C, D, COLL>
139where
140    C: CosmosClient,
141    D: DatabaseClient<C>,
142    COLL: CollectionClient<C, D>,
143{
144    type O = DeleteDocumentBuilder<'a, C, D, COLL>;
145
146    #[inline]
147    fn with_if_match_condition(self, if_match_condition: IfMatchCondition<'a>) -> Self::O {
148        DeleteDocumentBuilder {
149            document_client: self.document_client,
150            if_match_condition: Some(if_match_condition),
151            if_modified_since: self.if_modified_since,
152            user_agent: self.user_agent,
153            activity_id: self.activity_id,
154            consistency_level: self.consistency_level,
155            allow_tentative_writes: self.allow_tentative_writes,
156        }
157    }
158}
159
160impl<'a, C, D, COLL> IfModifiedSinceSupport<'a> for DeleteDocumentBuilder<'a, C, D, COLL>
161where
162    C: CosmosClient,
163    D: DatabaseClient<C>,
164    COLL: CollectionClient<C, D>,
165{
166    type O = DeleteDocumentBuilder<'a, C, D, COLL>;
167
168    #[inline]
169    fn with_if_modified_since(self, if_modified_since: &'a DateTime<Utc>) -> Self::O {
170        DeleteDocumentBuilder {
171            document_client: self.document_client,
172            if_match_condition: self.if_match_condition,
173            if_modified_since: Some(if_modified_since),
174            user_agent: self.user_agent,
175            activity_id: self.activity_id,
176            consistency_level: self.consistency_level,
177            allow_tentative_writes: self.allow_tentative_writes,
178        }
179    }
180}
181
182impl<'a, C, D, COLL> UserAgentSupport<'a> for DeleteDocumentBuilder<'a, C, D, COLL>
183where
184    C: CosmosClient,
185    D: DatabaseClient<C>,
186    COLL: CollectionClient<C, D>,
187{
188    type O = DeleteDocumentBuilder<'a, C, D, COLL>;
189
190    #[inline]
191    fn with_user_agent(self, user_agent: &'a str) -> Self::O {
192        DeleteDocumentBuilder {
193            document_client: self.document_client,
194            if_match_condition: self.if_match_condition,
195            if_modified_since: self.if_modified_since,
196            user_agent: Some(user_agent),
197            activity_id: self.activity_id,
198            consistency_level: self.consistency_level,
199            allow_tentative_writes: self.allow_tentative_writes,
200        }
201    }
202}
203
204impl<'a, C, D, COLL> ActivityIdSupport<'a> for DeleteDocumentBuilder<'a, C, D, COLL>
205where
206    C: CosmosClient,
207    D: DatabaseClient<C>,
208    COLL: CollectionClient<C, D>,
209{
210    type O = DeleteDocumentBuilder<'a, C, D, COLL>;
211
212    #[inline]
213    fn with_activity_id(self, activity_id: &'a str) -> Self::O {
214        DeleteDocumentBuilder {
215            document_client: self.document_client,
216            if_match_condition: self.if_match_condition,
217            if_modified_since: self.if_modified_since,
218            user_agent: self.user_agent,
219            activity_id: Some(activity_id),
220            consistency_level: self.consistency_level,
221            allow_tentative_writes: self.allow_tentative_writes,
222        }
223    }
224}
225
226impl<'a, C, D, COLL> ConsistencyLevelSupport<'a> for DeleteDocumentBuilder<'a, C, D, COLL>
227where
228    C: CosmosClient,
229    D: DatabaseClient<C>,
230    COLL: CollectionClient<C, D>,
231{
232    type O = DeleteDocumentBuilder<'a, C, D, COLL>;
233
234    #[inline]
235    fn with_consistency_level(self, consistency_level: ConsistencyLevel<'a>) -> Self::O {
236        DeleteDocumentBuilder {
237            document_client: self.document_client,
238            if_match_condition: self.if_match_condition,
239            if_modified_since: self.if_modified_since,
240            user_agent: self.user_agent,
241            activity_id: self.activity_id,
242            consistency_level: Some(consistency_level),
243            allow_tentative_writes: self.allow_tentative_writes,
244        }
245    }
246}
247
248impl<'a, C, D, COLL> AllowTentativeWritesSupport for DeleteDocumentBuilder<'a, C, D, COLL>
249where
250    C: CosmosClient,
251    D: DatabaseClient<C>,
252    COLL: CollectionClient<C, D>,
253{
254    type O = DeleteDocumentBuilder<'a, C, D, COLL>;
255
256    #[inline]
257    fn with_allow_tentative_writes(self, allow_tentative_writes: bool) -> Self::O {
258        DeleteDocumentBuilder {
259            document_client: self.document_client,
260            if_match_condition: self.if_match_condition,
261            if_modified_since: self.if_modified_since,
262            user_agent: self.user_agent,
263            activity_id: self.activity_id,
264            consistency_level: self.consistency_level,
265            allow_tentative_writes,
266        }
267    }
268}
269
270// methods callable only when every mandatory field has been filled
271impl<'a, C, D, COLL> DeleteDocumentBuilder<'a, C, D, COLL>
272where
273    C: CosmosClient,
274    D: DatabaseClient<C>,
275    COLL: CollectionClient<C, D>,
276{
277    pub async fn execute(&self) -> Result<DeleteDocumentResponse, AzureError> {
278        trace!("DeleteDocumentBuilder::execute called");
279
280        let mut req = self
281            .document_client
282            .prepare_request_with_document_name(hyper::Method::DELETE);
283
284        // add trait headers
285        req = IfMatchConditionOption::add_header(self, req);
286        req = IfModifiedSinceOption::add_header(self, req);
287        req = UserAgentOption::add_header(self, req);
288        req = ActivityIdOption::add_header(self, req);
289        req = ConsistencyLevelOption::add_header(self, req);
290        req = AllowTentativeWritesOption::add_header(self, req);
291
292        req = crate::add_partition_keys_header(self.document_client.partition_keys(), req);
293
294        let req = req.body(hyper::Body::empty())?;
295        debug!("{:?}", req);
296
297        let (headers, body) = check_status_extract_headers_and_body(
298            self.document_client.hyper_client().request(req),
299            StatusCode::NO_CONTENT,
300        )
301        .await?;
302
303        Ok((&headers, &body as &[u8]).try_into()?)
304    }
305}