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