openstack_sdk_identity/v3/os_federation/mapping/
delete.rs1use derive_builder::Builder;
23use http::{HeaderMap, HeaderName, HeaderValue};
24
25use openstack_sdk_core::api::rest_endpoint_prelude::*;
26
27use std::borrow::Cow;
28
29#[derive(Builder, Debug, Clone)]
30#[builder(setter(strip_option))]
31pub struct Request<'a> {
32 #[builder(default, setter(into))]
34 id: Cow<'a, str>,
35
36 #[builder(setter(name = "_headers"), default, private)]
37 _headers: Option<HeaderMap>,
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
74impl RestEndpoint for Request<'_> {
75 fn method(&self) -> http::Method {
76 http::Method::DELETE
77 }
78
79 fn endpoint(&self) -> Cow<'static, str> {
80 format!("OS-FEDERATION/mappings/{id}", id = self.id.as_ref(),).into()
81 }
82
83 fn parameters(&self) -> QueryParams<'_> {
84 QueryParams::default()
85 }
86
87 fn service_type(&self) -> ServiceType {
88 ServiceType::Identity
89 }
90
91 fn response_key(&self) -> Option<Cow<'static, str>> {
92 None
93 }
94
95 fn request_headers(&self) -> Option<&HeaderMap> {
97 self._headers.as_ref()
98 }
99
100 fn api_version(&self) -> Option<ApiVersion> {
102 Some(ApiVersion::new(3, 0))
103 }
104}
105
106#[cfg(test)]
107mod tests {
108 use super::*;
109 use http::{HeaderName, HeaderValue};
110 use httpmock::MockServer;
111 #[cfg(feature = "sync")]
112 use openstack_sdk_core::api::Query;
113 use openstack_sdk_core::test::client::FakeOpenStackClient;
114 use openstack_sdk_core::types::ServiceType;
115 use serde_json::json;
116
117 #[test]
118 fn test_service_type() {
119 assert_eq!(
120 Request::builder().build().unwrap().service_type(),
121 ServiceType::Identity
122 );
123 }
124
125 #[test]
126 fn test_response_key() {
127 assert!(Request::builder().build().unwrap().response_key().is_none())
128 }
129
130 #[cfg(feature = "sync")]
131 #[test]
132 fn endpoint() {
133 let server = MockServer::start();
134 let client = FakeOpenStackClient::new(server.base_url());
135 let mock = server.mock(|when, then| {
136 when.method(httpmock::Method::DELETE)
137 .path(format!("/OS-FEDERATION/mappings/{id}", id = "id",));
138
139 then.status(200)
140 .header("content-type", "application/json")
141 .json_body(json!({ "dummy": {} }));
142 });
143
144 let endpoint = Request::builder().id("id").build().unwrap();
145 let _: serde_json::Value = endpoint.query(&client).unwrap();
146 mock.assert();
147 }
148
149 #[cfg(feature = "sync")]
150 #[test]
151 fn endpoint_headers() {
152 let server = MockServer::start();
153 let client = FakeOpenStackClient::new(server.base_url());
154 let mock = server.mock(|when, then| {
155 when.method(httpmock::Method::DELETE)
156 .path(format!("/OS-FEDERATION/mappings/{id}", id = "id",))
157 .header("foo", "bar")
158 .header("not_foo", "not_bar");
159 then.status(200)
160 .header("content-type", "application/json")
161 .json_body(json!({ "dummy": {} }));
162 });
163
164 let endpoint = Request::builder()
165 .id("id")
166 .headers(
167 [(
168 Some(HeaderName::from_static("foo")),
169 HeaderValue::from_static("bar"),
170 )]
171 .into_iter(),
172 )
173 .header(
174 HeaderName::from_static("not_foo"),
175 HeaderValue::from_static("not_bar"),
176 )
177 .build()
178 .unwrap();
179 let _: serde_json::Value = endpoint.query(&client).unwrap();
180 mock.assert();
181 }
182}