Skip to content

hw openapigenerator #21388

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 11 additions & 10 deletions modules/openapi-generator/src/main/resources/python/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ from typing_extensions import Annotated
{{import}}
{{/imports}}

import allure
from {{packageName}}.api_client import ApiClient, RequestSerialized
from {{packageName}}.api_response import ApiResponse
from {{packageName}}.rest import RESTResponseType
Expand All @@ -29,19 +30,19 @@ class {{classname}}:
self.api_client = api_client
{{#operation}}


@validate_call
{{#asyncio}}async {{/asyncio}}def {{operationId}}{{>partial_api_args}} -> {{{returnType}}}{{^returnType}}None{{/returnType}}:
{{>partial_api}}
response_data = {{#asyncio}}await {{/asyncio}}self.api_client.call_api(
*_param,
_request_timeout=_request_timeout
)
{{#asyncio}}await {{/asyncio}}response_data.read()
return self.api_client.response_deserialize(
response_data=response_data,
response_types_map=_response_types_map,
).data
with allure.step("{{httpMethod}} {{{path}}}"):
response_data = {{#asyncio}}await {{/asyncio}}self.api_client.call_api(
*_param,
_request_timeout=_request_timeout
)
{{#asyncio}}await {{/asyncio}}response_data.read()
return self.api_client.response_deserialize(
response_data=response_data,
response_types_map=_response_types_map,
).data


@validate_call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

{{>partial_header}}


import io
import json
import re
import ssl
from typing import Optional, Union

import aiohttp
import aiohttp_retry

from json import JSONDecodeError
import json
import asyncio
import structlog
import curlify2
import allure
from unittest.mock import MagicMock

from {{packageName}}.exceptions import ApiException, ApiValueError

RESTResponseType = aiohttp.ClientResponse
Expand Down Expand Up @@ -74,13 +82,13 @@ class RESTClientObject:
await self.retry_client.close()

async def request(
self,
method,
url,
headers=None,
body=None,
post_params=None,
_request_timeout=None
self,
method,
url,
headers=None,
body=None,
post_params=None,
_request_timeout=None
):
"""Execute request

Expand Down Expand Up @@ -198,6 +206,49 @@ class RESTClientObject:
)
pool_manager = self.retry_client

r = await pool_manager.request(**args)

return await self._logged_request(pool_manager, **args)

async def _logged_request(self, pool_manager, **args) -> RESTResponse:
log = structlog.get_logger().bind(service='api')
json_data = args.get("data") if args.get("data") else "{}"
json_dict = json.loads(json_data)
log.msg(
'request',
method=args.get("method"),
url=args.get("url"),
headers=args.get("headers"),
json=json_dict
)
request = MagicMock(**args, body=json_data)
curl = curlify2.Curlify(request=request).to_curl()
print(curl)
allure.attach(curl, name="curl", attachment_type=allure.attachment_type.TEXT)
allure.attach(json.dumps(json_dict, indent=4), name="request", attachment_type=allure.attachment_type.JSON)
log.msg("request", **args)
try:
r = await pool_manager.request(**args)
except RuntimeError as e:
if 'Event loop is closed' in e.args[0]:
asyncio.run(self.pool_manager.__aexit__(None, None, None))
else:
log_response = r.__dict__
data = await r.read()
try:
json_data = json.loads(data)
attachment_type = allure.attachment_type.JSON
except JSONDecodeError:
json_data = str(data)
attachment_type = allure.attachment_type.TEXT

log.msg(
'response',
method=args.get("method"),
url=log_response.get("url"),
headers=dict(log_response.get("_headers", {})),
json=json_data,
status_code=log_response.get('status')
)
content = json.dumps(json_data, indent=4) if attachment_type == allure.attachment_type.JSON else json_data
allure.attach(content, name="response", attachment_type=attachment_type)
# smest
return RESTResponse(r)