Skip to content

feat: add support for nullable parameters in API calls in python #1098

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions templates/python/base/params.twig
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
api_params = {}
nullable_params = []

{% if method.parameters.all | length %}
{% for parameter in method.parameters.all %}
{% if parameter.required and not parameter.nullable %}
if {{ parameter.name | escapeKeyword | caseSnake }} is None:
raise {{spec.title | caseUcfirst}}Exception('Missing required parameter: "{{ parameter.name | escapeKeyword | caseSnake }}"')

{% endif %}
{% if parameter.nullable %}
nullable_params.append('{{ parameter.name }}')

{% endif %}
{% endfor %}
{% for parameter in method.parameters.path %}
Expand Down
2 changes: 1 addition & 1 deletion templates/python/base/requests/api.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
{% for key, header in method.headers %}
'{{ key }}': '{{ header }}',
{% endfor %}
}, api_params{% if method.type == 'webAuth' %}, response_type='location'{% endif %})
}, api_params, nullable_params{% if method.type == 'webAuth' %}, response_type='location'{% endif %})
8 changes: 6 additions & 2 deletions templates/python/package/client.py.twig
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@ class Client:
return self
{% endfor %}

def call(self, method, path='', headers=None, params=None, response_type='json'):
def call(self, method, path='', headers=None, params=None, nullable_params=None, response_type='json'):
if headers is None:
headers = {}

if params is None:
params = {}

params = {k: v for k, v in params.items() if v is not None} # Remove None values from params dictionary
if nullable_params is None:
nullable_params = []

# Only filter out None values for non-nullable params
params = {k: v for k, v in params.items() if v is not None or k in nullable_params}

data = {}
files = {}
Expand Down