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