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