Skip to main content

openstack_sdk_identity/v3/credential/
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//! Retrieve existing credentials.
19//!
20//! GET /v3/credentials/{credential_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    /// credential_id parameter for /v3/credentials/{credential_id} API
33    #[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    /// Create a builder for the endpoint.
41    pub fn builder() -> RequestBuilder<'a> {
42        RequestBuilder::default()
43    }
44}
45
46impl<'a> RequestBuilder<'a> {
47    /// Add a single header to the Credential.
48    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    /// Add multiple headers.
61    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::HEAD
77    }
78
79    fn endpoint(&self) -> Cow<'static, str> {
80        format!("credentials/{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    /// Returns headers to be set into the request
96    fn request_headers(&self) -> Option<&HeaderMap> {
97        self._headers.as_ref()
98    }
99
100    /// Returns required API version
101    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::RawQuery;
113    use openstack_sdk_core::test::client::FakeOpenStackClient;
114    use openstack_sdk_core::types::ServiceType;
115
116    #[test]
117    fn test_service_type() {
118        assert_eq!(
119            Request::builder().build().unwrap().service_type(),
120            ServiceType::Identity
121        );
122    }
123
124    #[test]
125    fn test_response_key() {
126        assert!(Request::builder().build().unwrap().response_key().is_none())
127    }
128
129    #[cfg(feature = "sync")]
130    #[test]
131    fn endpoint() {
132        let server = MockServer::start();
133        let client = FakeOpenStackClient::new(server.base_url());
134        let mock = server.mock(|when, then| {
135            when.method(httpmock::Method::HEAD)
136                .path(format!("/credentials/{id}", id = "id",));
137
138            then.status(200).header("content-type", "application/json");
139        });
140
141        let endpoint = Request::builder().id("id").build().unwrap();
142        let _ = endpoint.raw_query(&client).unwrap();
143        mock.assert();
144    }
145
146    #[cfg(feature = "sync")]
147    #[test]
148    fn endpoint_headers() {
149        let server = MockServer::start();
150        let client = FakeOpenStackClient::new(server.base_url());
151        let mock = server.mock(|when, then| {
152            when.method(httpmock::Method::HEAD)
153                .path(format!("/credentials/{id}", id = "id",))
154                .header("foo", "bar")
155                .header("not_foo", "not_bar");
156            then.status(200).header("content-type", "application/json");
157        });
158
159        let endpoint = Request::builder()
160            .id("id")
161            .headers(
162                [(
163                    Some(HeaderName::from_static("foo")),
164                    HeaderValue::from_static("bar"),
165                )]
166                .into_iter(),
167            )
168            .header(
169                HeaderName::from_static("not_foo"),
170                HeaderValue::from_static("not_bar"),
171            )
172            .build()
173            .unwrap();
174        let _ = endpoint.raw_query(&client).unwrap();
175        mock.assert();
176    }
177}