Skip to content

Commit

Permalink
Init endpoint_macros template
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubFrejlach committed Feb 28, 2024
1 parent 1a9f156 commit f6b4a9b
Showing 1 changed file with 151 additions and 0 deletions.
151 changes: 151 additions & 0 deletions osidb_bindings/templates/endpoint_macros.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
{#
This is a custom template derived from:
https://github.com/openapi-generators/openapi-python-client/blob/v.0.10.7/openapi_python_client/templates/endpoint_macros.py.jinja
#}
{% macro header_params(endpoint) %}
{% if endpoint.header_parameters %}
{% for parameter in endpoint.header_parameters.values() %}
{% if parameter.required %}
headers["{{ parameter.name | kebabcase}}"] = {{ parameter.python_name }}
{% else %}
if {{ parameter.python_name }} is not UNSET:
headers["{{ parameter.name | kebabcase}}"] = {{ parameter.python_name }}
{% endif %}
{% endfor %}
{% endif %}
{% endmacro %}

{% macro cookie_params(endpoint) %}
{% if endpoint.cookie_parameters %}
{% for parameter in endpoint.cookie_parameters.values() %}
{% if parameter.required %}
cookies["{{ parameter.name}}"] = {{ parameter.python_name }}
{% else %}
if {{ parameter.python_name }} is not UNSET:
cookies["{{ parameter.name}}"] = {{ parameter.python_name }}
{% endif %}
{% endfor %}
{% endif %}
{% endmacro %}


{% macro query_params(endpoint) %}
{% if endpoint.query_parameters %}
{% for property in endpoint.query_parameters.values() %}
{% set destination = "json_" + property.python_name %}
{% if property.template %}
{% from "property_templates/" + property.template import transform %}
{{ transform(property, property.python_name, destination) }}
{% endif %}
{% endfor %}
params: Dict[str, Any] = {
{% for property in endpoint.query_parameters.values() %}
{% if not property.json_is_dict %}
{% if property.template %}
"{{ property.name }}": {{ "json_" + property.python_name }},
{% else %}
"{{ property.name }}": {{ property.python_name }},
{% endif %}
{% endif %}
{% endfor %}
}
{% for property in endpoint.query_parameters.values() %}
{% if property.json_is_dict %}
{% set property_name = "json_" + property.python_name %}
{% if property.required and not property.nullable %}
params.update({{ property_name }})
{% else %}
if {% if not property.required %}not isinstance({{ property_name }}, Unset){% endif %}{% if not property.required and property.nullable %} and {% endif %}{% if property.nullable %}{{ property_name }} is not None{% endif %}:
params.update({{ property_name }})
{% endif %}
{% endif %}
{% endfor %}
params = {k: v for k, v in params.items() if v is not UNSET and v is not None}
{% endif %}
{% endmacro %}

{% macro json_body(endpoint) %}
{% if endpoint.json_body %}
{% set property = endpoint.json_body %}
{% set destination = "json_" + property.python_name %}
{% if property.template %}
{% from "property_templates/" + property.template import transform %}
{{ transform(property, property.python_name, destination) }}
{% endif %}
{% endif %}
{% endmacro %}

{% macro multipart_body(endpoint) %}
{% if endpoint.multipart_body %}
{% set property = endpoint.multipart_body %}
{% set destination = "multipart_" + property.python_name %}
{% if property.template %}
{% from "property_templates/" + property.template import transform_multipart %}
{{ transform_multipart(property, property.python_name, destination) }}
{% endif %}
{% endif %}
{% endmacro %}

{# The all the kwargs passed into an endpoint (and variants thereof)) #}
{% macro arguments(endpoint) %}
{# path parameters #}
{% for parameter in endpoint.path_parameters.values() %}
{{ parameter.to_string() }},
{% endfor %}
*,
{# Proper client based on whether or not the endpoint requires authentication #}
{% if endpoint.requires_security %}
client: AuthenticatedClient,
{% else %}
client: Client,
{% endif %}
{# Form data if any #}
{% if endpoint.form_body_class %}
form_data: {{ endpoint.form_body_class.name }},
{% endif %}
{# Multipart data if any #}
{% if endpoint.multipart_body %}
multipart_data: {{ endpoint.multipart_body.get_type_string() }},
{% endif %}
{# JSON body if any #}
{% if endpoint.json_body %}
json_body: {{ endpoint.json_body.get_type_string() }},
{% endif %}
{# query parameters #}
{% for parameter in endpoint.query_parameters.values() %}
{{ parameter.to_string() }},
{% endfor %}
{% for parameter in endpoint.header_parameters.values() %}
{{ parameter.to_string() }},
{% endfor %}
{# cookie parameters #}
{% for parameter in endpoint.cookie_parameters.values() %}
{{ parameter.to_string() }},
{% endfor %}
{% endmacro %}

{# Just lists all kwargs to endpoints as name=name for passing to other functions #}
{% macro kwargs(endpoint) %}
{% for parameter in endpoint.path_parameters.values() %}
{{ parameter.python_name }}={{ parameter.python_name }},
{% endfor %}
client=client,
{% if endpoint.form_body_class %}
form_data=form_data,
{% endif %}
{% if endpoint.multipart_body %}
multipart_data=multipart_data,
{% endif %}
{% if endpoint.json_body %}
json_body=json_body,
{% endif %}
{% for parameter in endpoint.query_parameters.values() %}
{{ parameter.python_name }}={{ parameter.python_name }},
{% endfor %}
{% for parameter in endpoint.header_parameters.values() %}
{{ parameter.python_name }}={{ parameter.python_name }},
{% endfor %}
{% for parameter in endpoint.cookie_parameters.values() %}
{{ parameter.python_name }}={{ parameter.python_name }},
{% endfor %}
{% endmacro %}

0 comments on commit f6b4a9b

Please sign in to comment.