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