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