Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add request-making logic to rust template #6

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 30 additions & 20 deletions templates/svix_lib_resource.rs.jinja
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#![allow(unused_imports)]

{% set api_mod_name %}{{ resource.name | to_snake_case }}_api{% endset -%}
{% set resource_type_name = resource.name | to_upper_camel_case -%}

{% if resource.has_post_operation -%}
use super::PostOptions;
{% endif -%}
use crate::{
apis::{{ api_mod_name }},
error::Result,
models::*,
Configuration,
Expand Down Expand Up @@ -105,23 +102,36 @@ impl<'a> {{ resource_type_name }}<'a> {
let PostOptions { idempotency_key } = options.unwrap_or_default();
{% endif -%}

{% set weird_rust_generator_op_id = op.id | replace(".", "-period-") %}
{% set fn_name = weird_rust_generator_op_id | to_snake_case %}
{% set params_struct_name -%}
{{ weird_rust_generator_op_id | to_upper_camel_case }}Params
{%- endset -%}
{{ api_mod_name }}::{{ fn_name }}(
self.cfg,
{{ api_mod_name }}::{{ params_struct_name }} {
{% for p in op.path_params -%}{{ p }},{% endfor %}
{% if op.request_body_schema_name is defined -%}
{{ op.request_body_schema_name | to_snake_case -}},
{% endif -%}
{% for p in op.query_params %}{{ p.name }},{% endfor -%}
{% if has_header_params %}idempotency_key,{% endif %}
}
)
.await
{# make the request #}
crate::request::Request::new(http1::Method::{{ op.method | upper }}, "{{ op.path }}")

{% for p in op.path_params -%}
.with_path_param("{{ p }}", {{ p }})
{% endfor -%}

{% for p in op.query_params -%}
{% if p.required -%}
.with_query_param
{%- else -%}
.with_optional_query_param
{%- endif -%}
("{{ p.name }}", {{ p.name }})
{% endfor -%}

{% if op.request_body_schema_name is defined -%}
.with_body_param({{ op.request_body_schema_name | to_snake_case }})
{% endif -%}

{% if has_header_params -%}
.with_optional_header_param("idempotency-key", idempotency_key)
{% endif -%}

{% if op.response_body_schema_name is undefined -%}
.returns_nothing()
{% endif -%}

.execute(self.cfg)
.await
}
{% endfor %}
}