openstack_sdk/api/object_store/v1/container/
get.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13// SPDX-License-Identifier: Apache-2.0
14//
15// WARNING: This file is automatically generated from OpenAPI schema using
16// `openstack-codegenerator`.
17
18//! Shows details for a container and lists objects, sorted by name, in the
19//! container. Specify query parameters in the request to filter the list and
20//! return a subset of objects. Omit query parameters to return a list of
21//! objects that are stored in the container, up to 10,000 names. The 10,000
22//! maximum value is configurable. To view the value for the cluster, issue a
23//! GET /info request.
24//!
25use derive_builder::Builder;
26use http::{HeaderMap, HeaderName, HeaderValue};
27
28use crate::api::rest_endpoint_prelude::*;
29
30use std::borrow::Cow;
31
32use crate::api::Pageable;
33#[derive(Builder, Debug, Clone)]
34#[builder(setter(strip_option))]
35pub struct Request<'a> {
36    /// The unique name for the account. An account is also known as the
37    /// project or tenant.
38    #[builder(default, setter(into))]
39    account: Cow<'a, str>,
40
41    /// The unique (within an account) name for the container. The container
42    /// name must be from 1 to 256 characters long and can start with any
43    /// character and contain any pattern. Character set must be UTF-8. The
44    /// container name cannot contain a slash (/) character because this
45    /// character delimits the container and object name. For example, the path
46    /// /v1/account/www/pages specifies the www container, not the www/pages
47    /// container.
48    #[builder(default, setter(into))]
49    container: Cow<'a, str>,
50
51    /// The delimiter is a single character used to split object names to
52    /// present a pseudo-directory hierarchy of objects. When combined with a
53    /// prefix query, this enables API users to simulate and traverse the
54    /// objects in a container as if they were in a directory tree.
55    #[builder(default, setter(into))]
56    delimiter: Option<Cow<'a, str>>,
57
58    /// For a string value, x, constrains the list to items whose names are
59    /// less than x.
60    #[builder(default, setter(into))]
61    end_marker: Option<Cow<'a, str>>,
62
63    /// The response format. Valid values are json, xml, or plain. The default
64    /// is plain. If you append the format=xml or format=json query parameter
65    /// to the storage account URL, the response shows extended container
66    /// information serialized in that format. If you append the format=plain
67    /// query parameter, the response lists the container names separated by
68    /// newlines.
69    #[builder(default, setter(into))]
70    format: Option<Cow<'a, str>>,
71
72    /// For an integer value n, limits the number of results to n.
73    #[builder(default)]
74    limit: Option<u32>,
75
76    /// For a string value, x, constrains the list to items whose names are
77    /// greater than x.
78    #[builder(default, setter(into))]
79    marker: Option<Cow<'a, str>>,
80
81    /// Only objects with this prefix will be returned. When combined with a
82    /// delimiter query, this enables API users to simulate and traverse the
83    /// objects in a container as if they were in a directory tree.
84    #[builder(default, setter(into))]
85    prefix: Option<Cow<'a, str>>,
86
87    /// By default, listings are returned sorted by name, ascending. If you
88    /// include the reverse=true query parameter, the listing will be returned
89    /// sorted by name, descending.
90    #[builder(default)]
91    reverse: Option<bool>,
92
93    #[builder(setter(name = "_headers"), default, private)]
94    _headers: Option<HeaderMap>,
95}
96impl<'a> Request<'a> {
97    /// Create a builder for the endpoint.
98    pub fn builder() -> RequestBuilder<'a> {
99        RequestBuilder::default()
100    }
101}
102
103impl<'a> RequestBuilder<'a> {
104    /// Add a single header to the Container.
105    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
106    where
107        K: Into<HeaderName>,
108        V: Into<HeaderValue>,
109    {
110        self._headers
111            .get_or_insert(None)
112            .get_or_insert_with(HeaderMap::new)
113            .insert(header_name.into(), header_value.into());
114        self
115    }
116
117    /// Add multiple headers.
118    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
119    where
120        I: Iterator<Item = T>,
121        T: Into<(Option<HeaderName>, HeaderValue)>,
122    {
123        self._headers
124            .get_or_insert(None)
125            .get_or_insert_with(HeaderMap::new)
126            .extend(iter.map(Into::into));
127        self
128    }
129}
130
131impl RestEndpoint for Request<'_> {
132    fn method(&self) -> http::Method {
133        http::Method::GET
134    }
135
136    fn endpoint(&self) -> Cow<'static, str> {
137        format!(
138            "{account}/{container}",
139            account = self.account.as_ref(),
140            container = self.container.as_ref(),
141        )
142        .into()
143    }
144
145    fn parameters(&self) -> QueryParams<'_> {
146        let mut params = QueryParams::default();
147        params.push_opt("limit", self.limit);
148        params.push_opt("marker", self.marker.as_ref());
149        params.push_opt("end_marker", self.end_marker.as_ref());
150        params.push_opt("format", self.format.as_ref());
151        params.push_opt("prefix", self.prefix.as_ref());
152        params.push_opt("delimiter", self.delimiter.as_ref());
153        params.push_opt("reverse", self.reverse);
154
155        params
156    }
157
158    fn service_type(&self) -> ServiceType {
159        ServiceType::ObjectStore
160    }
161
162    fn response_key(&self) -> Option<Cow<'static, str>> {
163        None
164    }
165
166    /// Returns headers to be set into the request
167    fn request_headers(&self) -> Option<&HeaderMap> {
168        self._headers.as_ref()
169    }
170
171    /// Returns required API version
172    fn api_version(&self) -> Option<ApiVersion> {
173        Some(ApiVersion::new(1, 0))
174    }
175}
176impl Pageable for Request<'_> {
177    fn use_keyset_pagination(&self) -> bool {
178        false
179    }
180}
181
182#[cfg(test)]
183mod tests {
184    use super::*;
185    #[cfg(feature = "sync")]
186    use crate::api::Query;
187    use crate::test::client::FakeOpenStackClient;
188    use crate::types::ServiceType;
189    use http::{HeaderName, HeaderValue};
190    use httpmock::MockServer;
191    use serde_json::json;
192
193    #[test]
194    fn test_service_type() {
195        assert_eq!(
196            Request::builder().build().unwrap().service_type(),
197            ServiceType::ObjectStore
198        );
199    }
200
201    #[test]
202    fn test_response_key() {
203        assert!(Request::builder().build().unwrap().response_key().is_none())
204    }
205
206    #[cfg(feature = "sync")]
207    #[test]
208    fn endpoint() {
209        let server = MockServer::start();
210        let client = FakeOpenStackClient::new(server.base_url());
211        let mock = server.mock(|when, then| {
212            when.method(httpmock::Method::GET).path(format!(
213                "/{account}/{container}",
214                account = "account",
215                container = "container",
216            ));
217
218            then.status(200)
219                .header("content-type", "application/json")
220                .json_body(json!({ "dummy": {} }));
221        });
222
223        let endpoint = Request::builder()
224            .account("account")
225            .container("container")
226            .build()
227            .unwrap();
228        let _: serde_json::Value = endpoint.query(&client).unwrap();
229        mock.assert();
230    }
231
232    #[cfg(feature = "sync")]
233    #[test]
234    fn endpoint_headers() {
235        let server = MockServer::start();
236        let client = FakeOpenStackClient::new(server.base_url());
237        let mock = server.mock(|when, then| {
238            when.method(httpmock::Method::GET)
239                .path(format!(
240                    "/{account}/{container}",
241                    account = "account",
242                    container = "container",
243                ))
244                .header("foo", "bar")
245                .header("not_foo", "not_bar");
246            then.status(200)
247                .header("content-type", "application/json")
248                .json_body(json!({ "dummy": {} }));
249        });
250
251        let endpoint = Request::builder()
252            .account("account")
253            .container("container")
254            .headers(
255                [(
256                    Some(HeaderName::from_static("foo")),
257                    HeaderValue::from_static("bar"),
258                )]
259                .into_iter(),
260            )
261            .header(
262                HeaderName::from_static("not_foo"),
263                HeaderValue::from_static("not_bar"),
264            )
265            .build()
266            .unwrap();
267        let _: serde_json::Value = endpoint.query(&client).unwrap();
268        mock.assert();
269    }
270}