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