openstack_sdk/api/object_store/v1/object/
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 object metadata.
19//!
20use derive_builder::Builder;
21use http::{HeaderMap, HeaderName, HeaderValue};
22
23use crate::api::rest_endpoint_prelude::*;
24
25use std::borrow::Cow;
26
27#[derive(Builder, Debug, Clone)]
28#[builder(setter(strip_option))]
29pub struct Request<'a> {
30    /// The unique name for the account. An account is also known as the
31    /// project or tenant.
32    #[builder(default, setter(into))]
33    account: Cow<'a, str>,
34
35    /// The unique (within an account) name for the container. The container
36    /// name must be from 1 to 256 characters long and can start with any
37    /// character and contain any pattern. Character set must be UTF-8. The
38    /// container name cannot contain a slash (/) character because this
39    /// character delimits the container and object name. For example, the path
40    /// /v1/account/www/pages specifies the www container, not the www/pages
41    /// container.
42    #[builder(default, setter(into))]
43    container: Cow<'a, str>,
44
45    /// Overrides the default file name. Object Storage generates a default
46    /// file name for GET temporary URLs that is based on the object name.
47    /// Object Storage returns this value in the Content-Disposition response
48    /// header. Browsers can interpret this file name value as a file
49    /// attachment to save. For more information about temporary URLs, see
50    /// Temporary URL middleware.
51    #[builder(default, setter(into))]
52    filename: Option<Cow<'a, str>>,
53
54    /// If you include the multipart-manifest=get query parameter and the
55    /// object is a large object, the object contents are not returned.
56    /// Instead, the manifest is returned in the X-Object-Manifest response
57    /// header for dynamic large objects or in the response body for static
58    /// large objects.
59    #[builder(default, setter(into))]
60    multipart_manifest: Option<Cow<'a, str>>,
61
62    /// The unique name for the object.
63    #[builder(default, setter(into))]
64    object: Cow<'a, str>,
65
66    /// If you include the symlink=get query parameter and the object is a
67    /// symlink, then the response will include data and metadata from the
68    /// symlink itself rather than from the target.
69    #[builder(default, setter(into))]
70    symlink: Option<Cow<'a, str>>,
71
72    /// The date and time in UNIX Epoch time stamp format or ISO 8601 UTC
73    /// timestamp when the signature for temporary URLs expires. For example,
74    /// 1440619048 or 2015-08-26T19:57:28Z is equivalent to Mon, Wed, 26 Aug
75    /// 2015 19:57:28 GMT. For more information about temporary URLs, see
76    /// Temporary URL middleware.
77    #[builder(default)]
78    temp_url_expires: Option<i32>,
79
80    /// Used with temporary URLs to sign the request with an HMAC-SHA1
81    /// cryptographic signature that defines the allowed HTTP method,
82    /// expiration date, full path to the object, and the secret key for the
83    /// temporary URL. For more information about temporary URLs, see Temporary
84    /// URL middleware.
85    #[builder(default, setter(into))]
86    temp_url_sig: Option<Cow<'a, str>>,
87
88    #[builder(setter(name = "_headers"), default, private)]
89    _headers: Option<HeaderMap>,
90}
91impl<'a> Request<'a> {
92    /// Create a builder for the endpoint.
93    pub fn builder() -> RequestBuilder<'a> {
94        RequestBuilder::default()
95    }
96}
97
98impl<'a> RequestBuilder<'a> {
99    /// Add a single header to the Object.
100    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
101    where
102        K: Into<HeaderName>,
103        V: Into<HeaderValue>,
104    {
105        self._headers
106            .get_or_insert(None)
107            .get_or_insert_with(HeaderMap::new)
108            .insert(header_name.into(), header_value.into());
109        self
110    }
111
112    /// Add multiple headers.
113    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
114    where
115        I: Iterator<Item = T>,
116        T: Into<(Option<HeaderName>, HeaderValue)>,
117    {
118        self._headers
119            .get_or_insert(None)
120            .get_or_insert_with(HeaderMap::new)
121            .extend(iter.map(Into::into));
122        self
123    }
124}
125
126impl RestEndpoint for Request<'_> {
127    fn method(&self) -> http::Method {
128        http::Method::HEAD
129    }
130
131    fn endpoint(&self) -> Cow<'static, str> {
132        format!(
133            "{account}/{container}/{object}",
134            account = self.account.as_ref(),
135            container = self.container.as_ref(),
136            object = self.object.as_ref(),
137        )
138        .into()
139    }
140
141    fn parameters(&self) -> QueryParams<'_> {
142        let mut params = QueryParams::default();
143        params.push_opt("multipart-manifest", self.multipart_manifest.as_ref());
144        params.push_opt("temp_url_sig", self.temp_url_sig.as_ref());
145        params.push_opt("temp_url_expires", self.temp_url_expires);
146        params.push_opt("filename", self.filename.as_ref());
147        params.push_opt("symlink", self.symlink.as_ref());
148
149        params
150    }
151
152    fn service_type(&self) -> ServiceType {
153        ServiceType::ObjectStore
154    }
155
156    fn response_key(&self) -> Option<Cow<'static, str>> {
157        None
158    }
159
160    /// Returns headers to be set into the request
161    fn request_headers(&self) -> Option<&HeaderMap> {
162        self._headers.as_ref()
163    }
164
165    /// Returns required API version
166    fn api_version(&self) -> Option<ApiVersion> {
167        Some(ApiVersion::new(1, 0))
168    }
169}
170
171#[cfg(test)]
172mod tests {
173    use super::*;
174    #[cfg(feature = "sync")]
175    use crate::api::RawQuery;
176    use crate::test::client::FakeOpenStackClient;
177    use crate::types::ServiceType;
178    use http::{HeaderName, HeaderValue};
179    use httpmock::MockServer;
180
181    #[test]
182    fn test_service_type() {
183        assert_eq!(
184            Request::builder().build().unwrap().service_type(),
185            ServiceType::ObjectStore
186        );
187    }
188
189    #[test]
190    fn test_response_key() {
191        assert!(Request::builder().build().unwrap().response_key().is_none())
192    }
193
194    #[cfg(feature = "sync")]
195    #[test]
196    fn endpoint() {
197        let server = MockServer::start();
198        let client = FakeOpenStackClient::new(server.base_url());
199        let mock = server.mock(|when, then| {
200            when.method(httpmock::Method::HEAD).path(format!(
201                "/{account}/{container}/{object}",
202                account = "account",
203                container = "container",
204                object = "object",
205            ));
206
207            then.status(200).header("content-type", "application/json");
208        });
209
210        let endpoint = Request::builder()
211            .account("account")
212            .container("container")
213            .object("object")
214            .build()
215            .unwrap();
216        let _ = endpoint.raw_query(&client).unwrap();
217        mock.assert();
218    }
219
220    #[cfg(feature = "sync")]
221    #[test]
222    fn endpoint_headers() {
223        let server = MockServer::start();
224        let client = FakeOpenStackClient::new(server.base_url());
225        let mock = server.mock(|when, then| {
226            when.method(httpmock::Method::HEAD)
227                .path(format!(
228                    "/{account}/{container}/{object}",
229                    account = "account",
230                    container = "container",
231                    object = "object",
232                ))
233                .header("foo", "bar")
234                .header("not_foo", "not_bar");
235            then.status(200).header("content-type", "application/json");
236        });
237
238        let endpoint = Request::builder()
239            .account("account")
240            .container("container")
241            .object("object")
242            .headers(
243                [(
244                    Some(HeaderName::from_static("foo")),
245                    HeaderValue::from_static("bar"),
246                )]
247                .into_iter(),
248            )
249            .header(
250                HeaderName::from_static("not_foo"),
251                HeaderValue::from_static("not_bar"),
252            )
253            .build()
254            .unwrap();
255        let _ = endpoint.raw_query(&client).unwrap();
256        mock.assert();
257    }
258}