Skip to main content

openstack_sdk_object_store/v1/container/
head.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 container metadata, including the number of objects and the total
19//! bytes of all objects stored in the container.
20//!
21use derive_builder::Builder;
22use http::{HeaderMap, HeaderName, HeaderValue};
23
24use openstack_sdk_core::api::rest_endpoint_prelude::*;
25
26use std::borrow::Cow;
27
28#[derive(Builder, Debug, Clone)]
29#[builder(setter(strip_option))]
30pub struct Request<'a> {
31    /// The unique name for the account. An account is also known as the
32    /// project or tenant.
33    #[builder(default, setter(into))]
34    account: Cow<'a, str>,
35
36    /// The unique (within an account) name for the container. The container
37    /// name must be from 1 to 256 characters long and can start with any
38    /// character and contain any pattern. Character set must be UTF-8. The
39    /// container name cannot contain a slash (/) character because this
40    /// character delimits the container and object name. For example, the path
41    /// /v1/account/www/pages specifies the www container, not the www/pages
42    /// container.
43    #[builder(default, setter(into))]
44    container: Cow<'a, str>,
45
46    #[builder(setter(name = "_headers"), default, private)]
47    _headers: Option<HeaderMap>,
48}
49impl<'a> Request<'a> {
50    /// Create a builder for the endpoint.
51    pub fn builder() -> RequestBuilder<'a> {
52        RequestBuilder::default()
53    }
54}
55
56impl<'a> RequestBuilder<'a> {
57    /// Add a single header to the Container.
58    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
59    where
60        K: Into<HeaderName>,
61        V: Into<HeaderValue>,
62    {
63        self._headers
64            .get_or_insert(None)
65            .get_or_insert_with(HeaderMap::new)
66            .insert(header_name.into(), header_value.into());
67        self
68    }
69
70    /// Add multiple headers.
71    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
72    where
73        I: Iterator<Item = T>,
74        T: Into<(Option<HeaderName>, HeaderValue)>,
75    {
76        self._headers
77            .get_or_insert(None)
78            .get_or_insert_with(HeaderMap::new)
79            .extend(iter.map(Into::into));
80        self
81    }
82}
83
84impl RestEndpoint for Request<'_> {
85    fn method(&self) -> http::Method {
86        http::Method::HEAD
87    }
88
89    fn endpoint(&self) -> Cow<'static, str> {
90        format!(
91            "{account}/{container}",
92            account = self.account.as_ref(),
93            container = self.container.as_ref(),
94        )
95        .into()
96    }
97
98    fn parameters(&self) -> QueryParams<'_> {
99        QueryParams::default()
100    }
101
102    fn service_type(&self) -> ServiceType {
103        ServiceType::ObjectStore
104    }
105
106    fn response_key(&self) -> Option<Cow<'static, str>> {
107        None
108    }
109
110    /// Returns headers to be set into the request
111    fn request_headers(&self) -> Option<&HeaderMap> {
112        self._headers.as_ref()
113    }
114
115    /// Returns required API version
116    fn api_version(&self) -> Option<ApiVersion> {
117        Some(ApiVersion::new(1, 0))
118    }
119}
120
121#[cfg(test)]
122mod tests {
123    use super::*;
124    use http::{HeaderName, HeaderValue};
125    use httpmock::MockServer;
126    #[cfg(feature = "sync")]
127    use openstack_sdk_core::api::RawQuery;
128    use openstack_sdk_core::test::client::FakeOpenStackClient;
129    use openstack_sdk_core::types::ServiceType;
130
131    #[test]
132    fn test_service_type() {
133        assert_eq!(
134            Request::builder().build().unwrap().service_type(),
135            ServiceType::ObjectStore
136        );
137    }
138
139    #[test]
140    fn test_response_key() {
141        assert!(Request::builder().build().unwrap().response_key().is_none())
142    }
143
144    #[cfg(feature = "sync")]
145    #[test]
146    fn endpoint() {
147        let server = MockServer::start();
148        let client = FakeOpenStackClient::new(server.base_url());
149        let mock = server.mock(|when, then| {
150            when.method(httpmock::Method::HEAD).path(format!(
151                "/{account}/{container}",
152                account = "account",
153                container = "container",
154            ));
155
156            then.status(200).header("content-type", "application/json");
157        });
158
159        let endpoint = Request::builder()
160            .account("account")
161            .container("container")
162            .build()
163            .unwrap();
164        let _ = endpoint.raw_query(&client).unwrap();
165        mock.assert();
166    }
167
168    #[cfg(feature = "sync")]
169    #[test]
170    fn endpoint_headers() {
171        let server = MockServer::start();
172        let client = FakeOpenStackClient::new(server.base_url());
173        let mock = server.mock(|when, then| {
174            when.method(httpmock::Method::HEAD)
175                .path(format!(
176                    "/{account}/{container}",
177                    account = "account",
178                    container = "container",
179                ))
180                .header("foo", "bar")
181                .header("not_foo", "not_bar");
182            then.status(200).header("content-type", "application/json");
183        });
184
185        let endpoint = Request::builder()
186            .account("account")
187            .container("container")
188            .headers(
189                [(
190                    Some(HeaderName::from_static("foo")),
191                    HeaderValue::from_static("bar"),
192                )]
193                .into_iter(),
194            )
195            .header(
196                HeaderName::from_static("not_foo"),
197                HeaderValue::from_static("not_bar"),
198            )
199            .build()
200            .unwrap();
201        let _ = endpoint.raw_query(&client).unwrap();
202        mock.assert();
203    }
204}