Skip to content

Commit

Permalink
Fix dict in response
Browse files Browse the repository at this point in the history
  • Loading branch information
artsmolin committed Feb 2, 2024
1 parent 009a11a commit 089da83
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 10 deletions.
4 changes: 2 additions & 2 deletions examples/petstore/client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# Generator info:
# GitHub Page: https://github.com/artsmolin/pythogen
# Version: 0.2.37
# Version: 0.2.38
# ==============================================================================

# jinja2: lstrip_blocks: "True"
Expand Down Expand Up @@ -836,7 +836,7 @@ async def getInventory(
meta.response = resp

if response.status_code == 200:
return GetinventoryResponse200.model_validate(response.json())
return response.json()

async def getOrderById(
self,
Expand Down
4 changes: 2 additions & 2 deletions examples/petstore/client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# Generator info:
# GitHub Page: https://github.com/artsmolin/pythogen
# Version: 0.2.37
# Version: 0.2.38
# ==============================================================================

# jinja2: lstrip_blocks: "True"
Expand Down Expand Up @@ -828,7 +828,7 @@ def getInventory(
meta.response = resp

if response.status_code == 200:
return GetinventoryResponse200.model_validate(response.json())
return response.json()

def getOrderById(
self,
Expand Down
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 = "pythogen"
version = "0.2.37"
version = "0.2.38"
description = "Generator of python HTTP-clients from OpenApi specification."
homepage = "https://github.com/artsmolin/pythogen"
repository = "https://github.com/artsmolin/pythogen"
Expand Down
5 changes: 5 additions & 0 deletions pythogen/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ def iterresponsemap(responses: models.ResponsesObject) -> list[tuple[str, str]]:
mapping.append((code, mapper))
continue

if response.schema.type == models.Type.object and response.schema.is_empty_object:
mapper = "response.json()"
mapping.append((code, mapper))
continue

if response.schema.type == models.Type.object:
mapper = f"{classname(response.schema.id)}.model_validate(response.json())"
mapping.append((code, mapper))
Expand Down
74 changes: 73 additions & 1 deletion tests/clients/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# Generator info:
# GitHub Page: https://github.com/artsmolin/pythogen
# Version: 0.2.37
# Version: 0.2.38
# ==============================================================================

# jinja2: lstrip_blocks: "True"
Expand Down Expand Up @@ -988,6 +988,78 @@ async def get_object_no_ref_schema(
if response.status_code == 200:
return GetObjectNoRefSchemaResponse200.model_validate(response.json())

async def get_empty_object(
self,
*,
auth: BasicAuth | None = None,
content: str | bytes | None = None,
meta: PythogenMetaBox | None = None,
) -> dict[Any, Any] | None:
"""
GET /get-empty-object
Operation ID: get_empty_object
Summary: Get Empty Object
Description: None
"""

method = "get"

path = "/get-empty-object"

url = f"{self.base_url}{path}"

params = None

headers_ = self.headers.copy()

if auth is None:
auth_ = DEFAULT_AUTH
elif isinstance(auth, httpx.Auth):
auth_ = auth
else:
auth_ = (auth.username, auth.password)

try:
response = await self.client.request(
method, url, headers=headers_, params=params, content=content, auth=auth_
)
except Exception as exc:
if self.metrics_integration:
if self.metrics_integration.shadow_path():
metrics_path = "/get-empty-object"
else:
metrics_path = path
self.metrics_integration.on_request_error(self.client_name, exc, method, metrics_path)

raise exc

if self.metrics_integration:
if self.metrics_integration.shadow_path():
metrics_path = "/get-empty-object"
else:
metrics_path = path
self.metrics_integration.on_request_success(self.client_name, response, method, metrics_path)

req = RequestBox(
client_name=self.client_name,
method=method,
url=url,
params=params,
headers=headers_,
content=content,
)

resp = ResponseBox(
status_code=response.status_code,
)

if meta:
meta.request = req
meta.response = resp

if response.status_code == 200:
return response.json()

async def get_object(
self,
*,
Expand Down
74 changes: 73 additions & 1 deletion tests/clients/async_client_with_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# Generator info:
# GitHub Page: https://github.com/artsmolin/pythogen
# Version: 0.2.37
# Version: 0.2.38
# ==============================================================================

# jinja2: lstrip_blocks: "True"
Expand Down Expand Up @@ -991,6 +991,78 @@ async def get_object_no_ref_schema(
if response.status_code == 200:
return GetObjectNoRefSchemaResponse200.model_validate(response.json())

async def get_empty_object(
self,
*,
auth: BasicAuth | None = None,
content: str | bytes | None = None,
meta: PythogenMetaBox | None = None,
) -> dict[Any, Any] | None:
"""
GET /get-empty-object
Operation ID: get_empty_object
Summary: Get Empty Object
Description: None
"""

method = "get"

path = "/get-empty-object"

url = f"{self.base_url}{path}"

params = None

headers_ = self.headers.copy()

if auth is None:
auth_ = DEFAULT_AUTH
elif isinstance(auth, httpx.Auth):
auth_ = auth
else:
auth_ = (auth.username, auth.password)

try:
response = await self.client.request(
method, url, headers=headers_, params=params, content=content, auth=auth_
)
except Exception as exc:
if self.metrics_integration:
if self.metrics_integration.shadow_path():
metrics_path = "/get-empty-object"
else:
metrics_path = path
self.metrics_integration.on_request_error(self.client_name, exc, method, metrics_path)

raise exc

if self.metrics_integration:
if self.metrics_integration.shadow_path():
metrics_path = "/get-empty-object"
else:
metrics_path = path
self.metrics_integration.on_request_success(self.client_name, response, method, metrics_path)

req = RequestBox(
client_name=self.client_name,
method=method,
url=url,
params=params,
headers=headers_,
content=content,
)

resp = ResponseBox(
status_code=response.status_code,
)

if meta:
meta.request = req
meta.response = resp

if response.status_code == 200:
return response.json()

async def get_object(
self,
*,
Expand Down
74 changes: 73 additions & 1 deletion tests/clients/async_client_with_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# Generator info:
# GitHub Page: https://github.com/artsmolin/pythogen
# Version: 0.2.37
# Version: 0.2.38
# ==============================================================================

# jinja2: lstrip_blocks: "True"
Expand Down Expand Up @@ -1020,6 +1020,78 @@ async def get_object_no_ref_schema(
if response.status_code == 200:
return GetObjectNoRefSchemaResponse200.model_validate(response.json())

async def get_empty_object(
self,
*,
auth: BasicAuth | None = None,
content: str | bytes | None = None,
meta: PythogenMetaBox | None = None,
) -> dict[Any, Any] | None:
"""
GET /get-empty-object
Operation ID: get_empty_object
Summary: Get Empty Object
Description: None
"""

method = "get"

path = "/get-empty-object"

url = f"{self.base_url}{path}"

params = None

headers_ = self.headers.copy()

if auth is None:
auth_ = DEFAULT_AUTH
elif isinstance(auth, httpx.Auth):
auth_ = auth
else:
auth_ = (auth.username, auth.password)

try:
response = await self.client.request(
method, url, headers=headers_, params=params, content=content, auth=auth_
)
except Exception as exc:
if self.metrics_integration:
if self.metrics_integration.shadow_path():
metrics_path = "/get-empty-object"
else:
metrics_path = path
self.metrics_integration.on_request_error(self.client_name, exc, method, metrics_path)

raise exc

if self.metrics_integration:
if self.metrics_integration.shadow_path():
metrics_path = "/get-empty-object"
else:
metrics_path = path
self.metrics_integration.on_request_success(self.client_name, response, method, metrics_path)

req = RequestBox(
client_name=self.client_name,
method=method,
url=url,
params=params,
headers=headers_,
content=content,
)

resp = ResponseBox(
status_code=response.status_code,
)

if meta:
meta.request = req
meta.response = resp

if response.status_code == 200:
return response.json()

async def get_object(
self,
*,
Expand Down
Loading

0 comments on commit 089da83

Please sign in to comment.