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