Skip to main content

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