Skip to main content

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