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