Change signature for adding request headers

Static strings are heavily complicating the headers management. Switch
to Into<HeaderName> and Into<HeaderValue> instead.
Change-Id: Iea470a5dd5950f155469e5de0ec4a3c3f70f39ae
Signed-off-by: Artem Goncharov <artem.goncharov@gmail.com>
This commit is contained in:
Artem Goncharov
2025年07月18日 16:58:27 +02:00
parent a14fe0a8a4
commit 171d3b39ad

View File

@@ -0,0 +1,15 @@
{%- if type_manager.get_parameters("header") %}
/// Header parameters
#[derive(Args)]
struct HeaderParameters {
{%- for name, param in type_manager.parameters | dictsort %}
{%- if param.location == "header" %}
{{ macros.docstring(param.description_with_defaults(), indent=4) }}
{{ param.clap_macros }}
{{ param.local_name}}: {{ param.type_hint }},
{%- endif %}
{%- endfor %}
}
{%- endif %}

View File

@@ -131,7 +131,7 @@ impl {{ target_class_name }}Command {
let mut find_builder = find::{{ sdk_struct_name }}::builder();
{{ macros.set_cli_path_parameters(type_manager, "find_builder", find_mode=True) }}
{%- if microversion %}
find_builder.header("OpenStack-API-Version", "{{ "volume" if service_type == "block-storage" else service_type }} {{ microversion }}");
find_builder.header(http::header::HeaderName::from_static("OpenStack-API-Version"), http::header::HeaderValue::from_static("{{ "volume" if service_type == "block-storage" else service_type }} {{ microversion }}"));
{%- endif %}
let find_ep = find_builder
.build()
@@ -143,7 +143,7 @@ impl {{ target_class_name }}Command {
{% set builder_mutable = type_manager.is_operation_supporting_params() %}
let {{ "mut" if builder_mutable }} ep_builder = {{ sdk_mod_path[-1] }}::Request::builder();
{%- if microversion %}
ep_builder.header("OpenStack-API-Version", "{{ "volume" if service_type == "block-storage" else service_type }} {{ microversion }}");
ep_builder.header(http::header::HeaderName::from_static("OpenStack-API-Version"), http::header::HeaderValue::from_static("{{ "volume" if service_type == "block-storage" else service_type }} {{ microversion }}"));
{%- endif %}
{{ macros.set_cli_path_parameters(type_manager, "ep_builder") }}
{% include 'rust_cli/set_query_parameters.j2' %}
@@ -151,7 +151,8 @@ impl {{ target_class_name }}Command {
{%- if operation_type == "upload" and body_types|length == 1 and body_types[0] != "*/*" %}
// The only supported media type
ep_builder.header("content-type", "{{ body_types[0] }}");
ep_builder.header(http::header::CONTENT_TYPE,
http::header::HeaderValue::from_static("{{ body_types[0] }}"));
{%- endif %}
let ep = ep_builder

View File

@@ -0,0 +1,26 @@
{%- import 'rust_macros.j2' as macros with context -%}
{%- if type_manager.get_parameters("header") %}
// Set header parameters
{%- for (k, v) in type_manager.get_parameters("header") %}
{%- if v.data_type.__class__.__name__ == "BooleanFlag" and v.data_type.original_data_type.__class__.__name__ == "Null" %}
{%- if v.is_required %}
if self.query.{{ v.local_name }} {
ep_builder.{{ v.remote_name }}(serde_json::Value::Null);
}
{%- else %}
if let Some(true) = self.query.{{ v.local_name }} {
ep_builder.{{ v.remote_name }}(serde_json::Value::Null);
}
{%- endif %}
{%- elif not v.is_required %}
if let Some(val) = &self.query.{{ v.local_name }} {
{{ macros.set_request_data_from_input(type_manager, "ep_builder", v, "val")}}
}
{%- else %}
{{ macros.set_request_data_from_input(type_manager, "ep_builder", v, "&self.query." + v.local_name )}}
{%- endif %}
{%- endfor %}
{%- endif %}

View File

@@ -55,12 +55,15 @@ impl<'a> Request<'a> {
impl<'a> RequestBuilder<'a> {
/// Add a single header to the Volume.
pub fn header(&mut self, header_name: &'static str, header_value: &'static str) -> &mut Self
where {
pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
where
K: Into<HeaderName>,
V: Into<HeaderValue>
{
self._headers
.get_or_insert(None)
.get_or_insert_with(HeaderMap::new)
.insert(header_name, HeaderValue::from_static(header_value));
.insert(header_name.into(), header_value.into());
self
}

View File

@@ -100,12 +100,15 @@ impl{{ type_manager.get_request_static_lifetimes(request) }} RequestBuilder{{ ty
{%- endif %}
/// Add a single header to the {{ class_name }}.
pub fn header(&mut self, header_name: &'static str, header_value: &'static str) -> &mut Self
where {
pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
where
K: Into<HeaderName>,
V: Into<HeaderValue>
{
self._headers
.get_or_insert(None)
.get_or_insert_with(HeaderMap::new)
.insert(header_name, HeaderValue::from_static(header_value));
.insert(header_name.into(), header_value.into());
self
}
@@ -406,7 +409,10 @@ mod tests {
)]
.into_iter(),
)
.header("not_foo", "not_bar")
.header(
HeaderName::from_static("not_foo"),
HeaderValue::from_static("not_bar")
)
.build()
.unwrap();
{%- if method.upper() != "HEAD" %}
Reference in New Issue
openstack/codegenerator
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.

The note is not visible to the blocked user.