From 21e3303d3dbbdcdc4a52d372a965c0f3aa3bc128 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Thu, 8 Feb 2024 12:44:50 -0500 Subject: [PATCH 1/2] handle ClientDisconnect errors raicsed when reading post body, don't log geo warnings on ip not in db --- app/client.py | 3 ++- app/main.py | 23 ++++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/client.py b/app/client.py index 66be25b..47edad3 100644 --- a/app/client.py +++ b/app/client.py @@ -41,7 +41,8 @@ async def get_spocs(self, session: ClientSession): targeting['country'] = self.geolocation.get_country(geo) targeting['region'] = self.geolocation.get_region(geo) except Exception as e: - logging.warning("Could not target based on geolocation. {0}".format(str(e))) + # logging.warning("Could not target based on geolocation. {0}".format(str(e))) + pass if self.country: targeting['country'] = self.country diff --git a/app/main.py b/app/main.py index 0bd05cb..77da062 100644 --- a/app/main.py +++ b/app/main.py @@ -5,7 +5,9 @@ import uvicorn from starlette.responses import JSONResponse -from fastapi import FastAPI, Request +from starlette.requests import ClientDisconnect +from starlette.status import HTTP_204_NO_CONTENT +from fastapi import FastAPI, Request, Response from app.adzerk.api import Api as AdZerk from app.client import Client @@ -43,14 +45,17 @@ async def lifespan(_: FastAPI) -> None: @app.post('/spocs') async def get_spocs(request: Request): - required_params = set(['version', 'consumer_key', 'pocket_id']) - optional_params = set(['site', 'placements', 'country', 'region']) - req_params = await __get_request_params(request) - if request.client is not None: - client_host = request.client.host - else: - client_host = "" - return await call(client_host, req_params, required_params, optional_params=optional_params) + try: + required_params = set(['version', 'consumer_key', 'pocket_id']) + optional_params = set(['site', 'placements', 'country', 'region']) + req_params = await __get_request_params(request) + if request.client is not None: + client_host = request.client.host + else: + client_host = "" + return await call(client_host, req_params, required_params, optional_params=optional_params) + except ClientDisconnect: + return Response(status_code=HTTP_204_NO_CONTENT) @app.delete('/user') From 5c859c8bc72564bc27e63e70ede153b735a03ef2 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Thu, 8 Feb 2024 15:18:00 -0500 Subject: [PATCH 2/2] review feedback, delete commented out line, use default response from framework for disconnected response --- app/client.py | 1 - app/main.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/client.py b/app/client.py index 47edad3..70df68b 100644 --- a/app/client.py +++ b/app/client.py @@ -41,7 +41,6 @@ async def get_spocs(self, session: ClientSession): targeting['country'] = self.geolocation.get_country(geo) targeting['region'] = self.geolocation.get_region(geo) except Exception as e: - # logging.warning("Could not target based on geolocation. {0}".format(str(e))) pass if self.country: diff --git a/app/main.py b/app/main.py index 77da062..568781f 100644 --- a/app/main.py +++ b/app/main.py @@ -55,7 +55,7 @@ async def get_spocs(request: Request): client_host = "" return await call(client_host, req_params, required_params, optional_params=optional_params) except ClientDisconnect: - return Response(status_code=HTTP_204_NO_CONTENT) + pass @app.delete('/user')