openstack_sdk/api/load_balancer/v2/l7policy/
get.rs1use derive_builder::Builder;
27use http::{HeaderMap, HeaderName, HeaderValue};
28
29use crate::api::rest_endpoint_prelude::*;
30
31use std::borrow::Cow;
32
33#[derive(Builder, Debug, Clone)]
34#[builder(setter(strip_option))]
35pub struct Request<'a> {
36 #[builder(default, setter(into))]
38 id: Cow<'a, str>,
39
40 #[builder(setter(name = "_headers"), default, private)]
41 _headers: Option<HeaderMap>,
42}
43impl<'a> Request<'a> {
44 pub fn builder() -> RequestBuilder<'a> {
46 RequestBuilder::default()
47 }
48}
49
50impl<'a> RequestBuilder<'a> {
51 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 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 format!("lbaas/l7policies/{id}", id = self.id.as_ref(),).into()
85 }
86
87 fn parameters(&self) -> QueryParams<'_> {
88 QueryParams::default()
89 }
90
91 fn service_type(&self) -> ServiceType {
92 ServiceType::LoadBalancer
93 }
94
95 fn response_key(&self) -> Option<Cow<'static, str>> {
96 Some("l7policy".into())
97 }
98
99 fn request_headers(&self) -> Option<&HeaderMap> {
101 self._headers.as_ref()
102 }
103
104 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::LoadBalancer
126 );
127 }
128
129 #[test]
130 fn test_response_key() {
131 assert_eq!(
132 Request::builder().build().unwrap().response_key().unwrap(),
133 "l7policy"
134 );
135 }
136
137 #[cfg(feature = "sync")]
138 #[test]
139 fn endpoint() {
140 let server = MockServer::start();
141 let client = FakeOpenStackClient::new(server.base_url());
142 let mock = server.mock(|when, then| {
143 when.method(httpmock::Method::GET)
144 .path(format!("/lbaas/l7policies/{id}", id = "id",));
145
146 then.status(200)
147 .header("content-type", "application/json")
148 .json_body(json!({ "l7policy": {} }));
149 });
150
151 let endpoint = Request::builder().id("id").build().unwrap();
152 let _: serde_json::Value = endpoint.query(&client).unwrap();
153 mock.assert();
154 }
155
156 #[cfg(feature = "sync")]
157 #[test]
158 fn endpoint_headers() {
159 let server = MockServer::start();
160 let client = FakeOpenStackClient::new(server.base_url());
161 let mock = server.mock(|when, then| {
162 when.method(httpmock::Method::GET)
163 .path(format!("/lbaas/l7policies/{id}", id = "id",))
164 .header("foo", "bar")
165 .header("not_foo", "not_bar");
166 then.status(200)
167 .header("content-type", "application/json")
168 .json_body(json!({ "l7policy": {} }));
169 });
170
171 let endpoint = Request::builder()
172 .id("id")
173 .headers(
174 [(
175 Some(HeaderName::from_static("foo")),
176 HeaderValue::from_static("bar"),
177 )]
178 .into_iter(),
179 )
180 .header(
181 HeaderName::from_static("not_foo"),
182 HeaderValue::from_static("not_bar"),
183 )
184 .build()
185 .unwrap();
186 let _: serde_json::Value = endpoint.query(&client).unwrap();
187 mock.assert();
188 }
189}