Skip to main content

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