From 13c4d58ba698fbd9f94ebc2f964717861e28f91a Mon Sep 17 00:00:00 2001 From: Dmitriy Date: Thu, 5 Jun 2025 17:23:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=B7=D0=B0=D0=BC=D0=B5=D1=87=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/python/api.mustache | 21 +++--- .../resources/python/asyncio/rest.mustache | 71 ++++++++++++++++--- 2 files changed, 72 insertions(+), 20 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/api.mustache b/modules/openapi-generator/src/main/resources/python/api.mustache index c1e166ba12a9..9727267f9336 100644 --- a/modules/openapi-generator/src/main/resources/python/api.mustache +++ b/modules/openapi-generator/src/main/resources/python/api.mustache @@ -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 @@ -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 diff --git a/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache b/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache index 937e76ed5a4c..053ba786bbe8 100644 --- a/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache @@ -2,8 +2,8 @@ {{>partial_header}} + import io -import json import re import ssl from typing import Optional, Union @@ -11,6 +11,14 @@ 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 @@ -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 @@ -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)