Skip to content

fix: Basic types as JSON bodies and responses #550

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

Merged
merged 1 commit into from
Dec 18, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
no_response_tests_no_response_get,
octet_stream_tests_octet_stream_get,
post_form_data,
post_tests_json_body_string,
test_inline_objects,
token_with_cookie_auth_token_with_cookie_get,
unsupported_content_tests_unsupported_content_get,
Expand Down Expand Up @@ -86,6 +87,13 @@ def json_body_tests_json_body_post(cls) -> types.ModuleType:
"""
return json_body_tests_json_body_post

@classmethod
def post_tests_json_body_string(cls) -> types.ModuleType:
"""
Json Body Which is String
"""
return post_tests_json_body_string

@classmethod
def defaults_tests_defaults_post(cls) -> types.ModuleType:
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
from typing import Any, Dict, Optional, Union, cast

import httpx

from ...client import Client
from ...models.http_validation_error import HTTPValidationError
from ...types import Response


def _get_kwargs(
*,
client: Client,
json_body: str,
) -> Dict[str, Any]:
url = "{}/tests/json_body/string".format(client.base_url)

headers: Dict[str, Any] = client.get_headers()
cookies: Dict[str, Any] = client.get_cookies()

json_json_body = json_body

return {
"url": url,
"headers": headers,
"cookies": cookies,
"timeout": client.get_timeout(),
"json": json_json_body,
}


def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidationError, str]]:
if response.status_code == 200:
response_200 = cast(str, response.json())
return response_200
if response.status_code == 422:
response_422 = HTTPValidationError.from_dict(response.json())

return response_422
return None


def _build_response(*, response: httpx.Response) -> Response[Union[HTTPValidationError, str]]:
return Response(
status_code=response.status_code,
content=response.content,
headers=response.headers,
parsed=_parse_response(response=response),
)


def sync_detailed(
*,
client: Client,
json_body: str,
) -> Response[Union[HTTPValidationError, str]]:
kwargs = _get_kwargs(
client=client,
json_body=json_body,
)

response = httpx.post(
verify=client.verify_ssl,
**kwargs,
)

return _build_response(response=response)


def sync(
*,
client: Client,
json_body: str,
) -> Optional[Union[HTTPValidationError, str]]:
""" """

return sync_detailed(
client=client,
json_body=json_body,
).parsed


async def asyncio_detailed(
*,
client: Client,
json_body: str,
) -> Response[Union[HTTPValidationError, str]]:
kwargs = _get_kwargs(
client=client,
json_body=json_body,
)

async with httpx.AsyncClient(verify=client.verify_ssl) as _client:
response = await _client.post(**kwargs)

return _build_response(response=response)


async def asyncio(
*,
client: Client,
json_body: str,
) -> Optional[Union[HTTPValidationError, str]]:
""" """

return (
await asyncio_detailed(
client=client,
json_body=json_body,
)
).parsed
40 changes: 40 additions & 0 deletions end_to_end_tests/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,46 @@
}
}
},
"/tests/json_body/string": {
"post": {
"tags": [
"tests"
],
"summary": "Json Body Which is String",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "success",
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
},
"/tests/defaults": {
"post": {
"tags": [
Expand Down
2 changes: 2 additions & 0 deletions openapi_python_client/templates/endpoint_macros.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ params = {k: v for k, v in params.items() if v is not UNSET and v is not None}
{% if property.template %}
{% from "property_templates/" + property.template import transform %}
{{ transform(property, property.python_name, destination) }}
{% else %}
{{ destination }} = {{ property.python_name }}
{% endif %}
{% endif %}
{% endmacro %}
Expand Down
2 changes: 1 addition & 1 deletion openapi_python_client/templates/endpoint_module.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _parse_response(*, response: httpx.Response) -> Optional[{{ return_string }}
{% from "property_templates/" + response.prop.template import construct %}
{{ construct(response.prop, response.source) | indent(8) }}
{% else %}
{{ response.prop.python_name }} = {{ response.source }}
{{ response.prop.python_name }} = cast({{ response.prop.get_type_string() }}, {{ response.source }})
{% endif %}
return {{ response.prop.python_name }}
{% endfor %}
Expand Down