Skip to content

Commit

Permalink
delete and put now return serialized response
Browse files Browse the repository at this point in the history
  • Loading branch information
gacou54 committed Aug 17, 2023
1 parent 4257d19 commit 02712ec
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "simple-openapi-client"
version = "0.4.0"
version = "0.5.0"
description = "OpenAPI Python client generator that follows the KISS principle."
authors = ["Gabriel Couture <[email protected]>"]
license = "BSD-3-Clause"
Expand Down
26 changes: 18 additions & 8 deletions simple_openapi_client/templates/async_client.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class {{ CLIENT }}(httpx.AsyncClient):
route: str,
params: Optional[QueryParamTypes] = None,
headers: Optional[HeaderTypes] = None,
cookies: Optional[CookieTypes] = None) -> Optional[httpx.Response]:
cookies: Optional[CookieTypes] = None) -> Union[Dict, List, str, bytes, int, httpx.Response]:
"""DELETE to specified route

Parameters
Expand All @@ -121,16 +121,21 @@ class {{ CLIENT }}(httpx.AsyncClient):

Returns
-------
Optional[httpx.Response]
If the HTTP DELETE request fails, HTTPError is raised, or return httpx.Response.
Union[Dict, List, str, bytes, int, httpx.Response]
Serialized response of the HTTP DELETE request or httpx.Response.
"""
response = await self.delete(route, params=params, headers=headers, cookies=cookies)

if self.return_raw_response:
return response

if 200 <= response.status_code < 300:
return
if 'application/json' in response.headers['content-type']:
return response.json()
elif 'text/plain' in response.headers['content-type']:
return response.text
else:
return response.content

raise httpx.HTTPError(f'HTTP code: {response.status_code}, with content: {response.text}')

Expand Down Expand Up @@ -186,7 +191,7 @@ class {{ CLIENT }}(httpx.AsyncClient):
json: Optional[Any] = None,
params: Optional[QueryParamTypes] = None,
headers: Optional[HeaderTypes] = None,
cookies: Optional[CookieTypes] = None) -> Optional[httpx.Response]:
cookies: Optional[CookieTypes] = None) -> Union[Dict, List, str, bytes, int, httpx.Response]:
"""PUT to specified route

Parameters
Expand All @@ -204,15 +209,20 @@ class {{ CLIENT }}(httpx.AsyncClient):

Returns
-------
Optional[httpx.Response]
If the HTTP PUT request fails, HTTPError is raised, or return httpx.Response.
Union[Dict, List, str, bytes, int, httpx.Response]
Serialized response of the HTTP PUT request or httpx.Response.
"""
response = await self.put(route, content=content, data=data, files=files, json=json, params=params, headers=headers, cookies=cookies)

if self.return_raw_response:
return response

if 200 <= response.status_code < 300:
return
if 'application/json' in response.headers['content-type']:
return response.json()
elif 'text/plain' in response.headers['content-type']:
return response.text
else:
return response.content

raise httpx.HTTPError(f'HTTP code: {response.status_code}, with text: {response.text}')
4 changes: 2 additions & 2 deletions simple_openapi_client/templates/async_method.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{% endif -%}
{%- if HEADERS -%}headers: HeaderTypes = None,
{% endif -%}
) -> {% if METHOD not in ['delete', 'put'] %}Union[Dict, List, str, bytes, int, httpx.Response]{% else %}Optional[httpx.Response]{% endif %}:
) -> Union[Dict, List, str, bytes, int, httpx.Response]:
"""(async) {{ OPERATION.summary }}

{{ OPERATION.description }}
Expand Down Expand Up @@ -83,7 +83,7 @@

Returns
-------
{% if METHOD not in ['delete', 'put'] %}Union[Dict, List, str, bytes, int, httpx.Response]{% else %}Optional[httpx.Response]{% endif %}
Union[Dict, List, str, bytes, int, httpx.Response]
{%- for response in RESPONSES %}
{{ response.description }}
{%- endfor %}
Expand Down
28 changes: 18 additions & 10 deletions simple_openapi_client/templates/client.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class {{ CLIENT }}(httpx.Client):
route: str,
params: Optional[QueryParamTypes] = None,
headers: Optional[HeaderTypes] = None,
cookies: Optional[CookieTypes] = None) -> Optional[httpx.Response]:
cookies: Optional[CookieTypes] = None) -> Union[Dict, List, str, bytes, int, httpx.Response]:
"""DELETE to specified route

Parameters
Expand All @@ -121,16 +121,21 @@ class {{ CLIENT }}(httpx.Client):

Returns
-------
Optional[httpx.Response]
If the HTTP DELETE request fails, HTTPError is raised, or return httpx.Response.
Union[Dict, List, str, bytes, int, httpx.Response]
Serialized response of the HTTP DELETE request or httpx.Response.
"""
response = self.delete(route, params=params, headers=headers, cookies=cookies)

if self.return_raw_response:
return response

if 200 <= response.status_code < 300:
return
if 'application/json' in response.headers['content-type']:
return response.json()
elif 'text/plain' in response.headers['content-type']:
return response.text
else:
return response.content

raise httpx.HTTPError(f'HTTP code: {response.status_code}, with content: {response.text}')

Expand Down Expand Up @@ -186,7 +191,7 @@ class {{ CLIENT }}(httpx.Client):
json: Optional[Any] = None,
params: Optional[QueryParamTypes] = None,
headers: Optional[HeaderTypes] = None,
cookies: Optional[CookieTypes] = None) -> Optional[httpx.Response]:
cookies: Optional[CookieTypes] = None) -> Union[Dict, List, str, bytes, int, httpx.Response]:
"""PUT to specified route

Parameters
Expand All @@ -204,17 +209,20 @@ class {{ CLIENT }}(httpx.Client):

Returns
-------
Optional[httpx.Response]
If the HTTP PUT request fails, HTTPError is raised, or return httpx.Response.
Union[Dict, List, str, bytes, int, httpx.Response]
Serialized response of the HTTP PUT request or httpx.Response.
"""
response = self.put(route, content=content, data=data, files=files, json=json, params=params, headers=headers, cookies=cookies)

if self.return_raw_response:
return response

if 200 <= response.status_code < 300:



if 'application/json' in response.headers['content-type']:
return response.json()
elif 'text/plain' in response.headers['content-type']:
return response.text
else:
return response.content

raise httpx.HTTPError(f'HTTP code: {response.status_code}, with text: {response.text}')
4 changes: 2 additions & 2 deletions simple_openapi_client/templates/method.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{% endif -%}
{%- if HEADERS -%}headers: HeaderTypes = None,
{% endif -%}
) -> {% if METHOD not in ['delete', 'put'] %}Union[Dict, List, str, bytes, int, httpx.Response]{% else %}Optional[httpx.Response]{% endif %}:
) -> Union[Dict, List, str, bytes, int, httpx.Response]:
"""{{ OPERATION.summary }}

{{ OPERATION.description }}
Expand Down Expand Up @@ -83,7 +83,7 @@

Returns
-------
{% if METHOD not in ['delete', 'put'] %}Union[Dict, List, str, bytes, int, httpx.Response]{% else %}Optional[httpx.Response]{% endif %}
Union[Dict, List, str, bytes, int, httpx.Response]
{%- for response in RESPONSES %}
{{ response.description }}
{%- endfor %}
Expand Down

0 comments on commit 02712ec

Please sign in to comment.