Skip to main content

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