openstack_sdk/api/load_balancer/v2/loadbalancer/
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))]
39 id: Cow<'a, str>,
40
41 #[builder(setter(name = "_headers"), default, private)]
42 _headers: Option<HeaderMap>,
43}
44impl<'a> Request<'a> {
45 pub fn builder() -> RequestBuilder<'a> {
47 RequestBuilder::default()
48 }
49}
50
51impl<'a> RequestBuilder<'a> {
52 pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
54 where
55 K: Into<HeaderName>,
56 V: Into<HeaderValue>,
57 {
58 self._headers
59 .get_or_insert(None)
60 .get_or_insert_with(HeaderMap::new)
61 .insert(header_name.into(), header_value.into());
62 self
63 }
64
65 pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
67 where
68 I: Iterator<Item = T>,
69 T: Into<(Option<HeaderName>, HeaderValue)>,
70 {
71 self._headers
72 .get_or_insert(None)
73 .get_or_insert_with(HeaderMap::new)
74 .extend(iter.map(Into::into));
75 self
76 }
77}
78
79impl RestEndpoint for Request<'_> {
80 fn method(&self) -> http::Method {
81 http::Method::GET
82 }
83
84 fn endpoint(&self) -> Cow<'static, str> {
85 format!("lbaas/loadbalancers/{id}", id = self.id.as_ref(),).into()
86 }
87
88 fn parameters(&self) -> QueryParams<'_> {
89 QueryParams::default()
90 }
91
92 fn service_type(&self) -> ServiceType {
93 ServiceType::LoadBalancer
94 }
95
96 fn response_key(&self) -> Option<Cow<'static, str>> {
97 Some("loadbalancer".into())
98 }
99
100 fn request_headers(&self) -> Option<&HeaderMap> {
102 self._headers.as_ref()
103 }
104
105 fn api_version(&self) -> Option<ApiVersion> {
107 Some(ApiVersion::new(2, 0))
108 }
109}
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114 #[cfg(feature = "sync")]
115 use crate::api::Query;
116 use crate::test::client::FakeOpenStackClient;
117 use crate::types::ServiceType;
118 use http::{HeaderName, HeaderValue};
119 use httpmock::MockServer;
120 use serde_json::json;
121
122 #[test]
123 fn test_service_type() {
124 assert_eq!(
125 Request::builder().build().unwrap().service_type(),
126 ServiceType::LoadBalancer
127 );
128 }
129
130 #[test]
131 fn test_response_key() {
132 assert_eq!(
133 Request::builder().build().unwrap().response_key().unwrap(),
134 "loadbalancer"
135 );
136 }
137
138 #[cfg(feature = "sync")]
139 #[test]
140 fn endpoint() {
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(format!("/lbaas/loadbalancers/{id}", id = "id",));
146
147 then.status(200)
148 .header("content-type", "application/json")
149 .json_body(json!({ "loadbalancer": {} }));
150 });
151
152 let endpoint = Request::builder().id("id").build().unwrap();
153 let _: serde_json::Value = endpoint.query(&client).unwrap();
154 mock.assert();
155 }
156
157 #[cfg(feature = "sync")]
158 #[test]
159 fn endpoint_headers() {
160 let server = MockServer::start();
161 let client = FakeOpenStackClient::new(server.base_url());
162 let mock = server.mock(|when, then| {
163 when.method(httpmock::Method::GET)
164 .path(format!("/lbaas/loadbalancers/{id}", id = "id",))
165 .header("foo", "bar")
166 .header("not_foo", "not_bar");
167 then.status(200)
168 .header("content-type", "application/json")
169 .json_body(json!({ "loadbalancer": {} }));
170 });
171
172 let endpoint = Request::builder()
173 .id("id")
174 .headers(
175 [(
176 Some(HeaderName::from_static("foo")),
177 HeaderValue::from_static("bar"),
178 )]
179 .into_iter(),
180 )
181 .header(
182 HeaderName::from_static("not_foo"),
183 HeaderValue::from_static("not_bar"),
184 )
185 .build()
186 .unwrap();
187 let _: serde_json::Value = endpoint.query(&client).unwrap();
188 mock.assert();
189 }
190}