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