azure_storage_blobs/service/operations/
list_containers.rs

1use crate::clients::BlobServiceClient;
2use crate::container::Container;
3use azure_core::{
4    error::{Error, ErrorKind, ResultExt},
5    headers::Headers,
6    prelude::*,
7    Method, Pageable, Response,
8};
9use azure_storage::parsing_xml::{cast_optional, traverse};
10use xml::Element;
11
12#[derive(Debug, Clone)]
13pub struct ListContainersBuilder {
14    client: BlobServiceClient,
15    prefix: Option<Prefix>,
16    include_metadata: bool,
17    include_deleted: bool,
18    include_system: bool,
19    max_results: Option<MaxResults>,
20    context: Context,
21}
22
23impl ListContainersBuilder {
24    pub(crate) fn new(client: BlobServiceClient) -> Self {
25        Self {
26            client,
27            prefix: None,
28            include_metadata: false,
29            include_deleted: false,
30            include_system: false,
31            max_results: None,
32            context: Context::new(),
33        }
34    }
35
36    setters! {
37        prefix: Prefix => Some(prefix),
38        include_metadata: bool => include_metadata,
39        include_deleted: bool => include_deleted,
40        include_system: bool => include_system,
41        max_results: MaxResults => Some(max_results),
42        context: Context => context,
43    }
44
45    pub fn into_stream(self) -> Pageable<ListContainersResponse, Error> {
46        let make_request = move |continuation: Option<NextMarker>| {
47            let this = self.clone();
48            let mut ctx = self.context.clone();
49            async move {
50                let mut url = this.client.url()?;
51
52                url.query_pairs_mut().append_pair("comp", "list");
53
54                this.prefix.append_to_url_query(&mut url);
55
56                if let Some(next_marker) = continuation {
57                    next_marker.append_to_url_query(&mut url);
58                }
59
60                let mut to_include = vec![];
61
62                if this.include_metadata {
63                    to_include.push("metadata");
64                }
65                if this.include_deleted {
66                    to_include.push("deleted");
67                }
68                if this.include_system {
69                    to_include.push("system");
70                }
71                if !to_include.is_empty() {
72                    url.query_pairs_mut()
73                        .append_pair("include", &to_include.join(","));
74                }
75                this.max_results.append_to_url_query(&mut url);
76
77                let mut request =
78                    BlobServiceClient::finalize_request(url, Method::Get, Headers::new(), None)?;
79
80                let response = this.client.send(&mut ctx, &mut request).await?;
81
82                ListContainersResponse::try_from(response).await
83            }
84        };
85
86        Pageable::new(make_request)
87    }
88}
89
90#[derive(Debug, Clone)]
91pub struct ListContainersResponse {
92    pub containers: Vec<Container>,
93    pub next_marker: Option<String>,
94}
95
96impl ListContainersResponse {
97    async fn try_from(response: Response) -> azure_core::Result<Self> {
98        let body = response.into_body().collect_string().await?;
99        let elem: Element = body.parse().map_kind(ErrorKind::Other)?;
100
101        let mut containers = Vec::new();
102
103        for container in traverse(&elem, &["Containers", "Container"], true)? {
104            containers.push(Container::parse(container)?);
105        }
106
107        let next_marker = match cast_optional::<String>(&elem, &["NextMarker"])? {
108            Some(nm) if nm.is_empty() => None,
109            Some(nm) => Some(nm),
110            None => None,
111        };
112
113        Ok(Self {
114            containers,
115            next_marker,
116        })
117    }
118}
119
120impl Continuable for ListContainersResponse {
121    type Continuation = NextMarker;
122    fn continuation(&self) -> Option<Self::Continuation> {
123        self.next_marker.clone().map(NextMarker::from)
124    }
125}