openstack_sdk/api/image/v2/schema/tasks/
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//! *(Since Images v2.2)*
19//!
20//! Shows a JSON schema document that represents a list of *tasks*.
21//!
22//! An tasks list entity is a container of entities containing abbreviated
23//! information about individual tasks.
24//!
25//! The following schema is solely an example. Consider only the response to
26//! the API call as authoritative.
27//!
28//! Normal response codes: 200
29//!
30//! Error response codes: 401
31//!
32use derive_builder::Builder;
33use http::{HeaderMap, HeaderName, HeaderValue};
34
35use crate::api::rest_endpoint_prelude::*;
36
37#[derive(Builder, Debug, Clone)]
38#[builder(setter(strip_option))]
39pub struct Request {
40    #[builder(setter(name = "_headers"), default, private)]
41    _headers: Option<HeaderMap>,
42}
43impl Request {
44    /// Create a builder for the endpoint.
45    pub fn builder() -> RequestBuilder {
46        RequestBuilder::default()
47    }
48}
49
50impl RequestBuilder {
51    /// Add a single header to the Tasks.
52    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
53    where
54        K: Into<HeaderName>,
55        V: Into<HeaderValue>,
56    {
57        self._headers
58            .get_or_insert(None)
59            .get_or_insert_with(HeaderMap::new)
60            .insert(header_name.into(), header_value.into());
61        self
62    }
63
64    /// Add multiple headers.
65    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
66    where
67        I: Iterator<Item = T>,
68        T: Into<(Option<HeaderName>, HeaderValue)>,
69    {
70        self._headers
71            .get_or_insert(None)
72            .get_or_insert_with(HeaderMap::new)
73            .extend(iter.map(Into::into));
74        self
75    }
76}
77
78impl RestEndpoint for Request {
79    fn method(&self) -> http::Method {
80        http::Method::GET
81    }
82
83    fn endpoint(&self) -> Cow<'static, str> {
84        "schemas/tasks".to_string().into()
85    }
86
87    fn parameters(&self) -> QueryParams<'_> {
88        QueryParams::default()
89    }
90
91    fn service_type(&self) -> ServiceType {
92        ServiceType::Image
93    }
94
95    fn response_key(&self) -> Option<Cow<'static, str>> {
96        None
97    }
98
99    /// Returns headers to be set into the request
100    fn request_headers(&self) -> Option<&HeaderMap> {
101        self._headers.as_ref()
102    }
103
104    /// Returns required API version
105    fn api_version(&self) -> Option<ApiVersion> {
106        Some(ApiVersion::new(2, 0))
107    }
108}
109
110#[cfg(test)]
111mod tests {
112    use super::*;
113    #[cfg(feature = "sync")]
114    use crate::api::Query;
115    use crate::test::client::FakeOpenStackClient;
116    use crate::types::ServiceType;
117    use http::{HeaderName, HeaderValue};
118    use httpmock::MockServer;
119    use serde_json::json;
120
121    #[test]
122    fn test_service_type() {
123        assert_eq!(
124            Request::builder().build().unwrap().service_type(),
125            ServiceType::Image
126        );
127    }
128
129    #[test]
130    fn test_response_key() {
131        assert!(Request::builder().build().unwrap().response_key().is_none())
132    }
133
134    #[cfg(feature = "sync")]
135    #[test]
136    fn endpoint() {
137        let server = MockServer::start();
138        let client = FakeOpenStackClient::new(server.base_url());
139        let mock = server.mock(|when, then| {
140            when.method(httpmock::Method::GET)
141                .path("/schemas/tasks".to_string());
142
143            then.status(200)
144                .header("content-type", "application/json")
145                .json_body(json!({ "dummy": {} }));
146        });
147
148        let endpoint = Request::builder().build().unwrap();
149        let _: serde_json::Value = endpoint.query(&client).unwrap();
150        mock.assert();
151    }
152
153    #[cfg(feature = "sync")]
154    #[test]
155    fn endpoint_headers() {
156        let server = MockServer::start();
157        let client = FakeOpenStackClient::new(server.base_url());
158        let mock = server.mock(|when, then| {
159            when.method(httpmock::Method::GET)
160                .path("/schemas/tasks".to_string())
161                .header("foo", "bar")
162                .header("not_foo", "not_bar");
163            then.status(200)
164                .header("content-type", "application/json")
165                .json_body(json!({ "dummy": {} }));
166        });
167
168        let endpoint = Request::builder()
169            .headers(
170                [(
171                    Some(HeaderName::from_static("foo")),
172                    HeaderValue::from_static("bar"),
173                )]
174                .into_iter(),
175            )
176            .header(
177                HeaderName::from_static("not_foo"),
178                HeaderValue::from_static("not_bar"),
179            )
180            .build()
181            .unwrap();
182        let _: serde_json::Value = endpoint.query(&client).unwrap();
183        mock.assert();
184    }
185}