azure_sdk_cosmos/requests/
list_databases_builder.rs1use crate::prelude::*;
2use crate::responses::ListDatabasesResponse;
3use crate::ResourceType;
4use azure_sdk_core::errors::{check_status_extract_headers_and_body, AzureError};
5use azure_sdk_core::prelude::*;
6use futures::stream::{unfold, Stream};
7use hyper::StatusCode;
8use std::convert::TryInto;
9
10#[derive(Debug, Clone)]
11pub struct ListDatabasesBuilder<'a> {
12 cosmos_client: &'a dyn CosmosClient,
13 user_agent: Option<&'a str>,
14 activity_id: Option<&'a str>,
15 consistency_level: Option<ConsistencyLevel<'a>>,
16 continuation: Option<&'a str>,
17 max_item_count: i32,
18}
19
20impl<'a> ListDatabasesBuilder<'a> {
21 pub(crate) fn new(cosmos_client: &'a dyn CosmosClient) -> ListDatabasesBuilder<'a> {
22 ListDatabasesBuilder {
23 cosmos_client,
24 user_agent: None,
25 activity_id: None,
26 consistency_level: None,
27 continuation: None,
28 max_item_count: -1,
29 }
30 }
31}
32
33impl<'a> CosmosClientRequired<'a> for ListDatabasesBuilder<'a> {
34 fn cosmos_client(&self) -> &'a dyn CosmosClient {
35 self.cosmos_client
36 }
37}
38
39impl<'a> UserAgentOption<'a> for ListDatabasesBuilder<'a> {
43 fn user_agent(&self) -> Option<&'a str> {
44 self.user_agent
45 }
46}
47
48impl<'a> ActivityIdOption<'a> for ListDatabasesBuilder<'a> {
49 fn activity_id(&self) -> Option<&'a str> {
50 self.activity_id
51 }
52}
53
54impl<'a> ConsistencyLevelOption<'a> for ListDatabasesBuilder<'a> {
55 fn consistency_level(&self) -> Option<ConsistencyLevel<'a>> {
56 self.consistency_level.clone()
57 }
58}
59
60impl<'a> ContinuationOption<'a> for ListDatabasesBuilder<'a> {
61 fn continuation(&self) -> Option<&'a str> {
62 self.continuation
63 }
64}
65
66impl<'a> MaxItemCountOption for ListDatabasesBuilder<'a> {
67 fn max_item_count(&self) -> i32 {
68 self.max_item_count
69 }
70}
71
72impl<'a> UserAgentSupport<'a> for ListDatabasesBuilder<'a> {
73 type O = ListDatabasesBuilder<'a>;
74
75 fn with_user_agent(self, user_agent: &'a str) -> Self::O {
76 ListDatabasesBuilder {
77 cosmos_client: self.cosmos_client,
78 user_agent: Some(user_agent),
79 activity_id: self.activity_id,
80 consistency_level: self.consistency_level,
81 continuation: self.continuation,
82 max_item_count: self.max_item_count,
83 }
84 }
85}
86
87impl<'a> ActivityIdSupport<'a> for ListDatabasesBuilder<'a> {
88 type O = ListDatabasesBuilder<'a>;
89
90 fn with_activity_id(self, activity_id: &'a str) -> Self::O {
91 ListDatabasesBuilder {
92 cosmos_client: self.cosmos_client,
93 user_agent: self.user_agent,
94 activity_id: Some(activity_id),
95 consistency_level: self.consistency_level,
96 continuation: self.continuation,
97 max_item_count: self.max_item_count,
98 }
99 }
100}
101
102impl<'a> ConsistencyLevelSupport<'a> for ListDatabasesBuilder<'a> {
103 type O = ListDatabasesBuilder<'a>;
104
105 fn with_consistency_level(self, consistency_level: ConsistencyLevel<'a>) -> Self::O {
106 ListDatabasesBuilder {
107 cosmos_client: self.cosmos_client,
108 user_agent: self.user_agent,
109 activity_id: self.activity_id,
110 consistency_level: Some(consistency_level),
111 continuation: self.continuation,
112 max_item_count: self.max_item_count,
113 }
114 }
115}
116
117impl<'a> ContinuationSupport<'a> for ListDatabasesBuilder<'a> {
118 type O = ListDatabasesBuilder<'a>;
119
120 fn with_continuation(self, continuation: &'a str) -> Self::O {
121 ListDatabasesBuilder {
122 cosmos_client: self.cosmos_client,
123 user_agent: self.user_agent,
124 activity_id: self.activity_id,
125 consistency_level: self.consistency_level,
126 continuation: Some(continuation),
127 max_item_count: self.max_item_count,
128 }
129 }
130}
131
132impl<'a> MaxItemCountSupport for ListDatabasesBuilder<'a> {
133 type O = ListDatabasesBuilder<'a>;
134
135 fn with_max_item_count(self, max_item_count: i32) -> Self::O {
136 ListDatabasesBuilder {
137 cosmos_client: self.cosmos_client,
138 user_agent: self.user_agent,
139 activity_id: self.activity_id,
140 consistency_level: self.consistency_level,
141 continuation: self.continuation,
142 max_item_count,
143 }
144 }
145}
146
147impl<'a> ListDatabasesBuilder<'a> {
149 pub async fn execute(&self) -> Result<ListDatabasesResponse, AzureError> {
150 trace!("ListDatabasesBuilder::execute called");
151
152 let request =
153 self.cosmos_client
154 .prepare_request("dbs", hyper::Method::GET, ResourceType::Databases);
155
156 let request = UserAgentOption::add_header(self, request);
157 let request = ActivityIdOption::add_header(self, request);
158 let request = ConsistencyLevelOption::add_header(self, request);
159 let request = ContinuationOption::add_header(self, request);
160 let request = MaxItemCountOption::add_header(self, request);
161
162 let request = request.body(hyper::Body::empty())?;
163
164 let future_response = self.cosmos_client.hyper_client().request(request);
165 let (headers, body) =
166 check_status_extract_headers_and_body(future_response, StatusCode::OK).await?;
167
168 Ok((&headers, &body as &[u8]).try_into()?)
169 }
170
171 pub fn stream(&self) -> impl Stream<Item = Result<ListDatabasesResponse, AzureError>> + '_ {
172 #[derive(Debug, Clone, PartialEq)]
173 enum States {
174 Init,
175 Continuation(String),
176 };
177
178 unfold(
179 Some(States::Init),
180 move |continuation_token: Option<States>| {
181 async move {
182 debug!("continuation_token == {:?}", &continuation_token);
183 let response = match continuation_token {
184 Some(States::Init) => self.execute().await,
185 Some(States::Continuation(continuation_token)) => {
186 self.clone()
187 .with_continuation(&continuation_token)
188 .execute()
189 .await
190 }
191 None => return None,
192 };
193
194 let response = match response {
197 Ok(response) => response,
198 Err(err) => return Some((Err(err), None)),
199 };
200
201 let continuation_token = match &response.continuation_token {
202 Some(ct) => Some(States::Continuation(ct.to_owned())),
203 None => None,
204 };
205
206 Some((Ok(response), continuation_token))
207 }
208 },
209 )
210 }
211}