openstack_sdk/api/identity/v3/auth/token/
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//! Validates a token.
19//!
20//! This call is similar to `GET /auth/tokens` but no response body is provided
21//! even in the `X-Subject-Token` header.
22//!
23//! The Identity API returns the same response as when the subject token was
24//! issued by `POST /auth/tokens` even if an error occurs because the token is
25//! not valid. An HTTP `204` response code indicates that the `X-Subject-Token`
26//! is valid.
27//!
28//! Relationship:
29//! `https://docs.openstack.org/api/openstack-identity/3/rel/auth_tokens`
30//!
31use derive_builder::Builder;
32use http::{HeaderMap, HeaderName, HeaderValue};
33
34use crate::api::rest_endpoint_prelude::*;
35
36#[derive(Builder, Debug, Clone)]
37#[builder(setter(strip_option))]
38pub struct Request {
39    #[builder(setter(name = "_headers"), default, private)]
40    _headers: Option<HeaderMap>,
41}
42impl Request {
43    /// Create a builder for the endpoint.
44    pub fn builder() -> RequestBuilder {
45        RequestBuilder::default()
46    }
47}
48
49impl RequestBuilder {
50    /// Add a single header to the Token.
51    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
52    where
53        K: Into<HeaderName>,
54        V: Into<HeaderValue>,
55    {
56        self._headers
57            .get_or_insert(None)
58            .get_or_insert_with(HeaderMap::new)
59            .insert(header_name.into(), header_value.into());
60        self
61    }
62
63    /// Add multiple headers.
64    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
65    where
66        I: Iterator<Item = T>,
67        T: Into<(Option<HeaderName>, HeaderValue)>,
68    {
69        self._headers
70            .get_or_insert(None)
71            .get_or_insert_with(HeaderMap::new)
72            .extend(iter.map(Into::into));
73        self
74    }
75}
76
77impl RestEndpoint for Request {
78    fn method(&self) -> http::Method {
79        http::Method::HEAD
80    }
81
82    fn endpoint(&self) -> Cow<'static, str> {
83        "auth/tokens".to_string().into()
84    }
85
86    fn parameters(&self) -> QueryParams<'_> {
87        QueryParams::default()
88    }
89
90    fn service_type(&self) -> ServiceType {
91        ServiceType::Identity
92    }
93
94    fn response_key(&self) -> Option<Cow<'static, str>> {
95        None
96    }
97
98    /// Returns headers to be set into the request
99    fn request_headers(&self) -> Option<&HeaderMap> {
100        self._headers.as_ref()
101    }
102
103    /// Returns required API version
104    fn api_version(&self) -> Option<ApiVersion> {
105        Some(ApiVersion::new(3, 0))
106    }
107}
108
109#[cfg(test)]
110mod tests {
111    use super::*;
112    #[cfg(feature = "sync")]
113    use crate::api::RawQuery;
114    use crate::test::client::FakeOpenStackClient;
115    use crate::types::ServiceType;
116    use http::{HeaderName, HeaderValue};
117    use httpmock::MockServer;
118
119    #[test]
120    fn test_service_type() {
121        assert_eq!(
122            Request::builder().build().unwrap().service_type(),
123            ServiceType::Identity
124        );
125    }
126
127    #[test]
128    fn test_response_key() {
129        assert!(Request::builder().build().unwrap().response_key().is_none())
130    }
131
132    #[cfg(feature = "sync")]
133    #[test]
134    fn endpoint() {
135        let server = MockServer::start();
136        let client = FakeOpenStackClient::new(server.base_url());
137        let mock = server.mock(|when, then| {
138            when.method(httpmock::Method::HEAD)
139                .path("/auth/tokens".to_string());
140
141            then.status(200).header("content-type", "application/json");
142        });
143
144        let endpoint = Request::builder().build().unwrap();
145        let _ = endpoint.raw_query(&client).unwrap();
146        mock.assert();
147    }
148
149    #[cfg(feature = "sync")]
150    #[test]
151    fn endpoint_headers() {
152        let server = MockServer::start();
153        let client = FakeOpenStackClient::new(server.base_url());
154        let mock = server.mock(|when, then| {
155            when.method(httpmock::Method::HEAD)
156                .path("/auth/tokens".to_string())
157                .header("foo", "bar")
158                .header("not_foo", "not_bar");
159            then.status(200).header("content-type", "application/json");
160        });
161
162        let endpoint = Request::builder()
163            .headers(
164                [(
165                    Some(HeaderName::from_static("foo")),
166                    HeaderValue::from_static("bar"),
167                )]
168                .into_iter(),
169            )
170            .header(
171                HeaderName::from_static("not_foo"),
172                HeaderValue::from_static("not_bar"),
173            )
174            .build()
175            .unwrap();
176        let _ = endpoint.raw_query(&client).unwrap();
177        mock.assert();
178    }
179}