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