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