Skip to main content

openstack_sdk_identity/v3/group/user/
get.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13// SPDX-License-Identifier: Apache-2.0
14//
15// WARNING: This file is automatically generated from OpenAPI schema using
16// `openstack-codegenerator`.
17
18//! Check if a user is in a group.
19//!
20//! GET/HEAD /groups/{group_id}/users/{user_id}
21//!
22use 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    /// group_id parameter for /v3/groups/{group_id}/users/{user_id} API
33    #[builder(default, setter(into))]
34    group_id: Cow<'a, str>,
35
36    /// user_id parameter for /v3/groups/{group_id}/users/{user_id} API
37    #[builder(default, setter(into))]
38    id: Cow<'a, str>,
39
40    #[builder(setter(name = "_headers"), default, private)]
41    _headers: Option<HeaderMap>,
42}
43impl<'a> Request<'a> {
44    /// Create a builder for the endpoint.
45    pub fn builder() -> RequestBuilder<'a> {
46        RequestBuilder::default()
47    }
48}
49
50impl<'a> RequestBuilder<'a> {
51    /// Add a single header to the User.
52    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
53    where
54        K: Into<HeaderName>,
55        V: Into<HeaderValue>,
56    {
57        self._headers
58            .get_or_insert(None)
59            .get_or_insert_with(HeaderMap::new)
60            .insert(header_name.into(), header_value.into());
61        self
62    }
63
64    /// Add multiple headers.
65    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
66    where
67        I: Iterator<Item = T>,
68        T: Into<(Option<HeaderName>, HeaderValue)>,
69    {
70        self._headers
71            .get_or_insert(None)
72            .get_or_insert_with(HeaderMap::new)
73            .extend(iter.map(Into::into));
74        self
75    }
76}
77
78impl RestEndpoint for Request<'_> {
79    fn method(&self) -> http::Method {
80        http::Method::GET
81    }
82
83    fn endpoint(&self) -> Cow<'static, str> {
84        format!(
85            "groups/{group_id}/users/{id}",
86            group_id = self.group_id.as_ref(),
87            id = self.id.as_ref(),
88        )
89        .into()
90    }
91
92    fn parameters(&self) -> QueryParams<'_> {
93        QueryParams::default()
94    }
95
96    fn service_type(&self) -> ServiceType {
97        ServiceType::Identity
98    }
99
100    fn response_key(&self) -> Option<Cow<'static, str>> {
101        None
102    }
103
104    /// Returns headers to be set into the request
105    fn request_headers(&self) -> Option<&HeaderMap> {
106        self._headers.as_ref()
107    }
108
109    /// Returns required API version
110    fn api_version(&self) -> Option<ApiVersion> {
111        Some(ApiVersion::new(3, 0))
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118    use http::{HeaderName, HeaderValue};
119    use httpmock::MockServer;
120    #[cfg(feature = "sync")]
121    use openstack_sdk_core::api::Query;
122    use openstack_sdk_core::test::client::FakeOpenStackClient;
123    use openstack_sdk_core::types::ServiceType;
124    use serde_json::json;
125
126    #[test]
127    fn test_service_type() {
128        assert_eq!(
129            Request::builder().build().unwrap().service_type(),
130            ServiceType::Identity
131        );
132    }
133
134    #[test]
135    fn test_response_key() {
136        assert!(Request::builder().build().unwrap().response_key().is_none())
137    }
138
139    #[cfg(feature = "sync")]
140    #[test]
141    fn endpoint() {
142        let server = MockServer::start();
143        let client = FakeOpenStackClient::new(server.base_url());
144        let mock = server.mock(|when, then| {
145            when.method(httpmock::Method::GET).path(format!(
146                "/groups/{group_id}/users/{id}",
147                group_id = "group_id",
148                id = "id",
149            ));
150
151            then.status(200)
152                .header("content-type", "application/json")
153                .json_body(json!({ "dummy": {} }));
154        });
155
156        let endpoint = Request::builder()
157            .group_id("group_id")
158            .id("id")
159            .build()
160            .unwrap();
161        let _: serde_json::Value = endpoint.query(&client).unwrap();
162        mock.assert();
163    }
164
165    #[cfg(feature = "sync")]
166    #[test]
167    fn endpoint_headers() {
168        let server = MockServer::start();
169        let client = FakeOpenStackClient::new(server.base_url());
170        let mock = server.mock(|when, then| {
171            when.method(httpmock::Method::GET)
172                .path(format!(
173                    "/groups/{group_id}/users/{id}",
174                    group_id = "group_id",
175                    id = "id",
176                ))
177                .header("foo", "bar")
178                .header("not_foo", "not_bar");
179            then.status(200)
180                .header("content-type", "application/json")
181                .json_body(json!({ "dummy": {} }));
182        });
183
184        let endpoint = Request::builder()
185            .group_id("group_id")
186            .id("id")
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}