refactor: fix bad smells (#1399)

# Description

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context.

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):
This commit is contained in:
Zineb El Bachiri 2023-10-30 10:18:04 +01:00 committed by GitHub
parent 21e226ed6e
commit 82bcf38b16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 84 deletions

View File

@ -20,7 +20,6 @@ async def verify_api_key(
result.data[0]["creation_time"], "%Y-%m-%dT%H:%M:%S"
).date()
# Check if the API key was created in the month of the current date
if (api_key_creation_date.month == current_date.month) and (
api_key_creation_date.year == current_date.year
):
@ -35,7 +34,6 @@ async def get_user_from_api_key(
) -> UserIdentity:
supabase_db = get_supabase_db()
# Lookup the user_id from the api_keys table
user_id_data = supabase_db.get_user_id_by_api_key(api_key)
if not user_id_data.data:
@ -43,7 +41,6 @@ async def get_user_from_api_key(
user_id = user_id_data.data[0]["user_id"]
# Lookup the email from the users table. Todo: remove and use user_id for credentials
email = supabase_db.get_user_email(user_id)
return UserIdentity(email=email, id=user_id)

View File

@ -1,5 +1,7 @@
import os
from utils import handle_request_validation_error
if __name__ == "__main__":
# import needed here when running main.py to debug backend
# you will need to run pip install python-dotenv
@ -7,8 +9,7 @@ if __name__ == "__main__":
load_dotenv()
import sentry_sdk
from fastapi import FastAPI, HTTPException, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from logger import get_logger
from middlewares.cors import add_cors_middleware
@ -40,24 +41,6 @@ async def http_exception_handler(_, exc):
)
# log more details about validation errors (422)
def handle_request_validation_error(app: FastAPI):
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(
request: Request, exc: RequestValidationError
):
exc_str = f"{exc}".replace("\n", " ").replace(" ", " ")
logger.error(request, exc_str)
content = {
"status_code": status.HTTP_422_UNPROCESSABLE_ENTITY,
"message": exc_str,
"data": None,
}
return JSONResponse(
content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY
)
handle_request_validation_error(app)
if __name__ == "__main__":

View File

@ -1,5 +1,7 @@
import os
from utils import handle_request_validation_error
if __name__ == "__main__":
# import needed here when running main.py to debug backend
# you will need to run pip install python-dotenv
@ -7,8 +9,7 @@ if __name__ == "__main__":
load_dotenv()
import sentry_sdk
from fastapi import FastAPI, HTTPException, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from logger import get_logger
from middlewares.cors import add_cors_middleware
@ -41,24 +42,6 @@ async def http_exception_handler(_, exc):
)
# log more details about validation errors (422)
def handle_request_validation_error(app: FastAPI):
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(
request: Request, exc: RequestValidationError
):
exc_str = f"{exc}".replace("\n", " ").replace(" ", " ")
logger.error(request, exc_str)
content = {
"status_code": status.HTTP_422_UNPROCESSABLE_ENTITY,
"message": exc_str,
"data": None,
}
return JSONResponse(
content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY
)
handle_request_validation_error(app)
if __name__ == "__main__":

View File

@ -1,5 +1,7 @@
import os
from utils import handle_request_validation_error
if __name__ == "__main__":
# import needed here when running main.py to debug backend
# you will need to run pip install python-dotenv
@ -8,8 +10,7 @@ if __name__ == "__main__":
load_dotenv()
import pypandoc
import sentry_sdk
from fastapi import FastAPI, HTTPException, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from logger import get_logger
from middlewares.cors import add_cors_middleware
@ -30,8 +31,9 @@ from routes.contact_routes import router as contact_router
logger = get_logger(__name__)
if (os.getenv("DEV_MODE") == "true"):
if os.getenv("DEV_MODE") == "true":
import debugpy
logger.debug("👨‍💻 Running in dev mode")
debugpy.listen(("0.0.0.0", 5678))
@ -79,24 +81,6 @@ async def http_exception_handler(_, exc):
)
# log more details about validation errors (422)
def handle_request_validation_error(app: FastAPI):
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(
request: Request, exc: RequestValidationError
):
exc_str = f"{exc}".replace("\n", " ").replace(" ", " ")
logger.error(request, exc_str)
content = {
"status_code": status.HTTP_422_UNPROCESSABLE_ENTITY,
"message": exc_str,
"data": None,
}
return JSONResponse(
content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY
)
handle_request_validation_error(app)
if __name__ == "__main__":

View File

@ -1,5 +1,7 @@
import os
from utils import handle_request_validation_error
if __name__ == "__main__":
# import needed here when running main.py to debug backend
# you will need to run pip install python-dotenv
@ -8,8 +10,7 @@ if __name__ == "__main__":
load_dotenv()
import pypandoc
import sentry_sdk
from fastapi import FastAPI, HTTPException, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from logger import get_logger
from middlewares.cors import add_cors_middleware
@ -49,24 +50,6 @@ async def http_exception_handler(_, exc):
)
# log more details about validation errors (422)
def handle_request_validation_error(app: FastAPI):
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(
request: Request, exc: RequestValidationError
):
exc_str = f"{exc}".replace("\n", " ").replace(" ", " ")
logger.error(request, exc_str)
content = {
"status_code": status.HTTP_422_UNPROCESSABLE_ENTITY,
"message": exc_str,
"data": None,
}
return JSONResponse(
content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY
)
handle_request_validation_error(app)
if __name__ == "__main__":

View File

@ -0,0 +1 @@
from .handle_request_validation_error import handle_request_validation_error

View File

@ -0,0 +1,23 @@
from fastapi import FastAPI, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from logger import get_logger
logger = get_logger(__name__)
def handle_request_validation_error(app: FastAPI):
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(
request: Request, exc: RequestValidationError
):
exc_str = f"{exc}".replace("\n", " ").replace(" ", " ")
logger.error(request, exc_str)
content = {
"status_code": status.HTTP_422_UNPROCESSABLE_ENTITY,
"message": exc_str,
"data": None,
}
return JSONResponse(
content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY
)