openstack_sdk/api/network/v2/router/conntrack_helper/
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//! Shows information for a router conntrack helper.
19//!
20//! Use the `fields` query parameter to control which fields are returned in
21//! the response body. For more information, see [Fields](#fields).
22//!
23//! Normal response codes: 200
24//!
25//! Error response codes: 400, 404
26//!
27use derive_builder::Builder;
28use http::{HeaderMap, HeaderName, HeaderValue};
29
30use crate::api::rest_endpoint_prelude::*;
31
32use std::borrow::Cow;
33
34#[derive(Builder, Debug, Clone)]
35#[builder(setter(strip_option))]
36pub struct Request<'a> {
37    /// id parameter for /v2.0/routers/{router_id}/conntrack_helpers/{id} API
38    #[builder(default, setter(into))]
39    id: Cow<'a, str>,
40
41    /// router_id parameter for
42    /// /v2.0/routers/{router_id}/conntrack_helpers/{id} API
43    #[builder(default, setter(into))]
44    router_id: Cow<'a, str>,
45
46    #[builder(setter(name = "_headers"), default, private)]
47    _headers: Option<HeaderMap>,
48}
49impl<'a> Request<'a> {
50    /// Create a builder for the endpoint.
51    pub fn builder() -> RequestBuilder<'a> {
52        RequestBuilder::default()
53    }
54}
55
56impl<'a> RequestBuilder<'a> {
57    /// Add a single header to the Conntrack_Helper.
58    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
59    where
60        K: Into<HeaderName>,
61        V: Into<HeaderValue>,
62    {
63        self._headers
64            .get_or_insert(None)
65            .get_or_insert_with(HeaderMap::new)
66            .insert(header_name.into(), header_value.into());
67        self
68    }
69
70    /// Add multiple headers.
71    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
72    where
73        I: Iterator<Item = T>,
74        T: Into<(Option<HeaderName>, HeaderValue)>,
75    {
76        self._headers
77            .get_or_insert(None)
78            .get_or_insert_with(HeaderMap::new)
79            .extend(iter.map(Into::into));
80        self
81    }
82}
83
84impl RestEndpoint for Request<'_> {
85    fn method(&self) -> http::Method {
86        http::Method::GET
87    }
88
89    fn endpoint(&self) -> Cow<'static, str> {
90        format!(
91            "routers/{router_id}/conntrack_helpers/{id}",
92            id = self.id.as_ref(),
93            router_id = self.router_id.as_ref(),
94        )
95        .into()
96    }
97
98    fn parameters(&self) -> QueryParams<'_> {
99        QueryParams::default()
100    }
101
102    fn service_type(&self) -> ServiceType {
103        ServiceType::Network
104    }
105
106    fn response_key(&self) -> Option<Cow<'static, str>> {
107        Some("conntrack_helper".into())
108    }
109
110    /// Returns headers to be set into the request
111    fn request_headers(&self) -> Option<&HeaderMap> {
112        self._headers.as_ref()
113    }
114
115    /// Returns required API version
116    fn api_version(&self) -> Option<ApiVersion> {
117        Some(ApiVersion::new(2, 0))
118    }
119}
120
121#[cfg(test)]
122mod tests {
123    use super::*;
124    #[cfg(feature = "sync")]
125    use crate::api::Query;
126    use crate::test::client::FakeOpenStackClient;
127    use crate::types::ServiceType;
128    use http::{HeaderName, HeaderValue};
129    use httpmock::MockServer;
130    use serde_json::json;
131
132    #[test]
133    fn test_service_type() {
134        assert_eq!(
135            Request::builder().build().unwrap().service_type(),
136            ServiceType::Network
137        );
138    }
139
140    #[test]
141    fn test_response_key() {
142        assert_eq!(
143            Request::builder().build().unwrap().response_key().unwrap(),
144            "conntrack_helper"
145        );
146    }
147
148    #[cfg(feature = "sync")]
149    #[test]
150    fn endpoint() {
151        let server = MockServer::start();
152        let client = FakeOpenStackClient::new(server.base_url());
153        let mock = server.mock(|when, then| {
154            when.method(httpmock::Method::GET).path(format!(
155                "/routers/{router_id}/conntrack_helpers/{id}",
156                id = "id",
157                router_id = "router_id",
158            ));
159
160            then.status(200)
161                .header("content-type", "application/json")
162                .json_body(json!({ "conntrack_helper": {} }));
163        });
164
165        let endpoint = Request::builder()
166            .id("id")
167            .router_id("router_id")
168            .build()
169            .unwrap();
170        let _: serde_json::Value = endpoint.query(&client).unwrap();
171        mock.assert();
172    }
173
174    #[cfg(feature = "sync")]
175    #[test]
176    fn endpoint_headers() {
177        let server = MockServer::start();
178        let client = FakeOpenStackClient::new(server.base_url());
179        let mock = server.mock(|when, then| {
180            when.method(httpmock::Method::GET)
181                .path(format!(
182                    "/routers/{router_id}/conntrack_helpers/{id}",
183                    id = "id",
184                    router_id = "router_id",
185                ))
186                .header("foo", "bar")
187                .header("not_foo", "not_bar");
188            then.status(200)
189                .header("content-type", "application/json")
190                .json_body(json!({ "conntrack_helper": {} }));
191        });
192
193        let endpoint = Request::builder()
194            .id("id")
195            .router_id("router_id")
196            .headers(
197                [(
198                    Some(HeaderName::from_static("foo")),
199                    HeaderValue::from_static("bar"),
200                )]
201                .into_iter(),
202            )
203            .header(
204                HeaderName::from_static("not_foo"),
205                HeaderValue::from_static("not_bar"),
206            )
207            .build()
208            .unwrap();
209        let _: serde_json::Value = endpoint.query(&client).unwrap();
210        mock.assert();
211    }
212}