openstack_sdk/api/image/v2/metadef/namespace/
delete.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
18use derive_builder::Builder;
19use http::{HeaderMap, HeaderName, HeaderValue};
20
21use crate::api::rest_endpoint_prelude::*;
22
23use std::borrow::Cow;
24
25#[derive(Builder, Debug, Clone)]
26#[builder(setter(strip_option))]
27pub struct Request<'a> {
28    /// namespace_name parameter for /v2/metadefs/namespaces/{namespace_name}
29    /// API
30    #[builder(default, setter(into))]
31    namespace_name: Cow<'a, str>,
32
33    #[builder(setter(name = "_headers"), default, private)]
34    _headers: Option<HeaderMap>,
35}
36impl<'a> Request<'a> {
37    /// Create a builder for the endpoint.
38    pub fn builder() -> RequestBuilder<'a> {
39        RequestBuilder::default()
40    }
41}
42
43impl<'a> RequestBuilder<'a> {
44    /// Add a single header to the Namespace.
45    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
46    where
47        K: Into<HeaderName>,
48        V: Into<HeaderValue>,
49    {
50        self._headers
51            .get_or_insert(None)
52            .get_or_insert_with(HeaderMap::new)
53            .insert(header_name.into(), header_value.into());
54        self
55    }
56
57    /// Add multiple headers.
58    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
59    where
60        I: Iterator<Item = T>,
61        T: Into<(Option<HeaderName>, HeaderValue)>,
62    {
63        self._headers
64            .get_or_insert(None)
65            .get_or_insert_with(HeaderMap::new)
66            .extend(iter.map(Into::into));
67        self
68    }
69}
70
71impl RestEndpoint for Request<'_> {
72    fn method(&self) -> http::Method {
73        http::Method::DELETE
74    }
75
76    fn endpoint(&self) -> Cow<'static, str> {
77        format!(
78            "metadefs/namespaces/{namespace_name}",
79            namespace_name = self.namespace_name.as_ref(),
80        )
81        .into()
82    }
83
84    fn parameters(&self) -> QueryParams<'_> {
85        QueryParams::default()
86    }
87
88    fn service_type(&self) -> ServiceType {
89        ServiceType::Image
90    }
91
92    fn response_key(&self) -> Option<Cow<'static, str>> {
93        None
94    }
95
96    /// Returns headers to be set into the request
97    fn request_headers(&self) -> Option<&HeaderMap> {
98        self._headers.as_ref()
99    }
100
101    /// Returns required API version
102    fn api_version(&self) -> Option<ApiVersion> {
103        Some(ApiVersion::new(2, 0))
104    }
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110    #[cfg(feature = "sync")]
111    use crate::api::Query;
112    use crate::test::client::FakeOpenStackClient;
113    use crate::types::ServiceType;
114    use http::{HeaderName, HeaderValue};
115    use httpmock::MockServer;
116    use serde_json::json;
117
118    #[test]
119    fn test_service_type() {
120        assert_eq!(
121            Request::builder().build().unwrap().service_type(),
122            ServiceType::Image
123        );
124    }
125
126    #[test]
127    fn test_response_key() {
128        assert!(Request::builder().build().unwrap().response_key().is_none())
129    }
130
131    #[cfg(feature = "sync")]
132    #[test]
133    fn endpoint() {
134        let server = MockServer::start();
135        let client = FakeOpenStackClient::new(server.base_url());
136        let mock = server.mock(|when, then| {
137            when.method(httpmock::Method::DELETE).path(format!(
138                "/metadefs/namespaces/{namespace_name}",
139                namespace_name = "namespace_name",
140            ));
141
142            then.status(200)
143                .header("content-type", "application/json")
144                .json_body(json!({ "dummy": {} }));
145        });
146
147        let endpoint = Request::builder()
148            .namespace_name("namespace_name")
149            .build()
150            .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::DELETE)
162                .path(format!(
163                    "/metadefs/namespaces/{namespace_name}",
164                    namespace_name = "namespace_name",
165                ))
166                .header("foo", "bar")
167                .header("not_foo", "not_bar");
168            then.status(200)
169                .header("content-type", "application/json")
170                .json_body(json!({ "dummy": {} }));
171        });
172
173        let endpoint = Request::builder()
174            .namespace_name("namespace_name")
175            .headers(
176                [(
177                    Some(HeaderName::from_static("foo")),
178                    HeaderValue::from_static("bar"),
179                )]
180                .into_iter(),
181            )
182            .header(
183                HeaderName::from_static("not_foo"),
184                HeaderValue::from_static("not_bar"),
185            )
186            .build()
187            .unwrap();
188        let _: serde_json::Value = endpoint.query(&client).unwrap();
189        mock.assert();
190    }
191}