Skip to main content

openstack_sdk_identity/v3/os_oauth1/authorize/
set.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//! PUT operation on /v3/OS-OAUTH1/authorize/{request_token_id}
19//!
20use derive_builder::Builder;
21use http::{HeaderMap, HeaderName, HeaderValue};
22
23use openstack_sdk_core::api::rest_endpoint_prelude::*;
24
25use serde_json::Value;
26use std::borrow::Cow;
27use std::collections::BTreeMap;
28
29#[derive(Builder, Debug, Clone)]
30#[builder(setter(strip_option))]
31pub struct Request<'a> {
32    /// request_token_id parameter for
33    /// /v3/OS-OAUTH1/authorize/{request_token_id} API
34    #[builder(default, setter(into))]
35    request_token_id: Cow<'a, str>,
36
37    #[builder(setter(name = "_headers"), default, private)]
38    _headers: Option<HeaderMap>,
39
40    #[builder(setter(name = "_properties"), default, private)]
41    _properties: BTreeMap<Cow<'a, str>, Value>,
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 Authorize.
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    pub fn properties<I, K, V>(&mut self, iter: I) -> &mut Self
78    where
79        I: Iterator<Item = (K, V)>,
80        K: Into<Cow<'a, str>>,
81        V: Into<Value>,
82    {
83        self._properties
84            .get_or_insert_with(BTreeMap::new)
85            .extend(iter.map(|(k, v)| (k.into(), v.into())));
86        self
87    }
88}
89
90impl RestEndpoint for Request<'_> {
91    fn method(&self) -> http::Method {
92        http::Method::PUT
93    }
94
95    fn endpoint(&self) -> Cow<'static, str> {
96        format!(
97            "OS-OAUTH1/authorize/{request_token_id}",
98            request_token_id = self.request_token_id.as_ref(),
99        )
100        .into()
101    }
102
103    fn parameters(&self) -> QueryParams<'_> {
104        QueryParams::default()
105    }
106
107    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
108        let mut params = JsonBodyParams::default();
109
110        for (key, val) in &self._properties {
111            params.push(key.clone(), val.clone());
112        }
113
114        params.into_body()
115    }
116
117    fn service_type(&self) -> ServiceType {
118        ServiceType::Identity
119    }
120
121    fn response_key(&self) -> Option<Cow<'static, str>> {
122        None
123    }
124
125    /// Returns headers to be set into the request
126    fn request_headers(&self) -> Option<&HeaderMap> {
127        self._headers.as_ref()
128    }
129
130    /// Returns required API version
131    fn api_version(&self) -> Option<ApiVersion> {
132        Some(ApiVersion::new(3, 0))
133    }
134}
135
136#[cfg(test)]
137mod tests {
138    use super::*;
139    use http::{HeaderName, HeaderValue};
140    use httpmock::MockServer;
141    #[cfg(feature = "sync")]
142    use openstack_sdk_core::api::Query;
143    use openstack_sdk_core::test::client::FakeOpenStackClient;
144    use openstack_sdk_core::types::ServiceType;
145    use serde_json::json;
146
147    #[test]
148    fn test_service_type() {
149        assert_eq!(
150            Request::builder().build().unwrap().service_type(),
151            ServiceType::Identity
152        );
153    }
154
155    #[test]
156    fn test_response_key() {
157        assert!(Request::builder().build().unwrap().response_key().is_none())
158    }
159
160    #[cfg(feature = "sync")]
161    #[test]
162    fn endpoint() {
163        let server = MockServer::start();
164        let client = FakeOpenStackClient::new(server.base_url());
165        let mock = server.mock(|when, then| {
166            when.method(httpmock::Method::PUT).path(format!(
167                "/OS-OAUTH1/authorize/{request_token_id}",
168                request_token_id = "request_token_id",
169            ));
170
171            then.status(200)
172                .header("content-type", "application/json")
173                .json_body(json!({ "dummy": {} }));
174        });
175
176        let endpoint = Request::builder()
177            .request_token_id("request_token_id")
178            .build()
179            .unwrap();
180        let _: serde_json::Value = endpoint.query(&client).unwrap();
181        mock.assert();
182    }
183
184    #[cfg(feature = "sync")]
185    #[test]
186    fn endpoint_headers() {
187        let server = MockServer::start();
188        let client = FakeOpenStackClient::new(server.base_url());
189        let mock = server.mock(|when, then| {
190            when.method(httpmock::Method::PUT)
191                .path(format!(
192                    "/OS-OAUTH1/authorize/{request_token_id}",
193                    request_token_id = "request_token_id",
194                ))
195                .header("foo", "bar")
196                .header("not_foo", "not_bar");
197            then.status(200)
198                .header("content-type", "application/json")
199                .json_body(json!({ "dummy": {} }));
200        });
201
202        let endpoint = Request::builder()
203            .request_token_id("request_token_id")
204            .headers(
205                [(
206                    Some(HeaderName::from_static("foo")),
207                    HeaderValue::from_static("bar"),
208                )]
209                .into_iter(),
210            )
211            .header(
212                HeaderName::from_static("not_foo"),
213                HeaderValue::from_static("not_bar"),
214            )
215            .build()
216            .unwrap();
217        let _: serde_json::Value = endpoint.query(&client).unwrap();
218        mock.assert();
219    }
220}