From de37993a03c1b0ef7eff26592dffe369054a709a Mon Sep 17 00:00:00 2001 From: Zhbr Date: Fri, 12 Jan 2024 20:21:23 +0300 Subject: [PATCH 1/3] Adding param request_timeout, that manages timeout async_timeout.timeout --- fastapi_gateway_ultra/core.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/fastapi_gateway_ultra/core.py b/fastapi_gateway_ultra/core.py index 4438373..36da31d 100644 --- a/fastapi_gateway_ultra/core.py +++ b/fastapi_gateway_ultra/core.py @@ -48,6 +48,7 @@ def route( name: Optional[str] = None, callbacks: Optional[List[BaseRoute]] = None, openapi_extra: Optional[Dict[str, Any]] = None, + request_timeout: int = 60, ): """ @@ -98,6 +99,7 @@ def route( https://fastapi.tiangolo.com/advanced/openapi-callbacks/ :param openapi_extra: See documentation for details - https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/ + :param request_timeout: request timeout to inner service, default is 60 sec :return: wrapped endpoint result as is """ @@ -133,10 +135,8 @@ def wrapper(f): async def inner(request: Request, response: Response, **kwargs): scope = request.scope scope_method = scope["method"].lower() - content_type = str(request.headers.get('Content-Type')) - request_form = ( - await request.form() if 'x-www-form-urlencoded' in content_type else None - ) + content_type = str(request.headers.get("Content-Type")) + request_form = await request.form() if "x-www-form-urlencoded" in content_type else None prepare_microservice_path = f"{service_url}{gateway_path}" if service_path: @@ -146,7 +146,6 @@ async def inner(request: Request, response: Response, **kwargs): request_body = await unzip_body_object( necessary_params=body_params, all_params=kwargs, - ) request_query = await unzip_query_params( @@ -168,16 +167,13 @@ async def inner(request: Request, response: Response, **kwargs): ) try: - ( - resp_data, - status_code_from_service, - microservice_headers, - ) = await make_request( + (resp_data, status_code_from_service, microservice_headers,) = await make_request( url=microservice_url, method=scope_method, data=request_data, query=request_query, headers=request_headers, + timeout=request_timeout, ) except ClientConnectorError: From 836ee056eb27c5fc34bdf586e556dfabacf356c3 Mon Sep 17 00:00:00 2001 From: Yaroslav Radaykin Date: Thu, 27 Feb 2025 18:14:15 +0300 Subject: [PATCH 2/3] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D1=82=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D1=83?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi_gateway_ultra/core.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/fastapi_gateway_ultra/core.py b/fastapi_gateway_ultra/core.py index 36da31d..20565f4 100644 --- a/fastapi_gateway_ultra/core.py +++ b/fastapi_gateway_ultra/core.py @@ -194,6 +194,13 @@ async def inner(request: Request, response: Response, **kwargs): ) response.headers.update(service_headers) + + if status_code_from_service >= 400: + raise HTTPException( + status_code=status_code_from_service, + detail=resp_data + ) + response.status_code = status_code_from_service return resp_data From ce02a7708b8c649fc221e9354fd3d61c442dda53 Mon Sep 17 00:00:00 2001 From: Yaroslav Radaykin Date: Thu, 27 Feb 2025 18:19:11 +0300 Subject: [PATCH 3/3] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D1=8F=D0=B5=D1=82=20=D0=BE=D1=82=D0=B2=D0=B5=D1=82=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi_gateway_ultra/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastapi_gateway_ultra/core.py b/fastapi_gateway_ultra/core.py index 20565f4..befaac5 100644 --- a/fastapi_gateway_ultra/core.py +++ b/fastapi_gateway_ultra/core.py @@ -198,7 +198,7 @@ async def inner(request: Request, response: Response, **kwargs): if status_code_from_service >= 400: raise HTTPException( status_code=status_code_from_service, - detail=resp_data + detail=resp_data.get("detail", resp_data) ) response.status_code = status_code_from_service