Skip to main content

openstack_sdk_identity/v3/auth/token/
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//! Validates and shows information for a token, including its expiration date
19//! and authorization scope.
20//!
21//! Pass your own token in the `X-Auth-Token` request header.
22//!
23//! Pass the token that you want to validate in the `X-Subject-Token` request
24//! header.
25//!
26//! Relationship:
27//! `https://docs.openstack.org/api/openstack-identity/3/rel/auth_tokens`
28//!
29use derive_builder::Builder;
30use http::{HeaderMap, HeaderName, HeaderValue};
31
32use openstack_sdk_core::api::rest_endpoint_prelude::*;
33
34use std::borrow::Cow;
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::GET
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        Some("token".into())
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    use http::{HeaderName, HeaderValue};
113    use httpmock::MockServer;
114    #[cfg(feature = "sync")]
115    use openstack_sdk_core::api::Query;
116    use openstack_sdk_core::test::client::FakeOpenStackClient;
117    use openstack_sdk_core::types::ServiceType;
118    use serde_json::json;
119
120    #[test]
121    fn test_service_type() {
122        assert_eq!(
123            Request::builder().build().unwrap().service_type(),
124            ServiceType::Identity
125        );
126    }
127
128    #[test]
129    fn test_response_key() {
130        assert_eq!(
131            Request::builder().build().unwrap().response_key().unwrap(),
132            "token"
133        );
134    }
135
136    #[cfg(feature = "sync")]
137    #[test]
138    fn endpoint() {
139        let server = MockServer::start();
140        let client = FakeOpenStackClient::new(server.base_url());
141        let mock = server.mock(|when, then| {
142            when.method(httpmock::Method::GET)
143                .path("/auth/tokens".to_string());
144
145            then.status(200)
146                .header("content-type", "application/json")
147                .json_body(json!({ "token": {} }));
148        });
149
150        let endpoint = Request::builder().build().unwrap();
151        let _: serde_json::Value = endpoint.query(&client).unwrap();
152        mock.assert();
153    }
154
155    #[cfg(feature = "sync")]
156    #[test]
157    fn endpoint_headers() {
158        let server = MockServer::start();
159        let client = FakeOpenStackClient::new(server.base_url());
160        let mock = server.mock(|when, then| {
161            when.method(httpmock::Method::GET)
162                .path("/auth/tokens".to_string())
163                .header("foo", "bar")
164                .header("not_foo", "not_bar");
165            then.status(200)
166                .header("content-type", "application/json")
167                .json_body(json!({ "token": {} }));
168        });
169
170        let endpoint = Request::builder()
171            .headers(
172                [(
173                    Some(HeaderName::from_static("foo")),
174                    HeaderValue::from_static("bar"),
175                )]
176                .into_iter(),
177            )
178            .header(
179                HeaderName::from_static("not_foo"),
180                HeaderValue::from_static("not_bar"),
181            )
182            .build()
183            .unwrap();
184        let _: serde_json::Value = endpoint.query(&client).unwrap();
185        mock.assert();
186    }
187}