chore: delete unused deprecated methods
Browse files- app/core/api_response.py +0 -104
app/core/api_response.py
DELETED
|
@@ -1,104 +0,0 @@
|
|
| 1 |
-
from functools import wraps
|
| 2 |
-
from warnings import deprecated
|
| 3 |
-
from fastapi import HTTPException, Request, status
|
| 4 |
-
from loguru import logger
|
| 5 |
-
import os
|
| 6 |
-
import json
|
| 7 |
-
from typing import Dict
|
| 8 |
-
from environs import Env
|
| 9 |
-
|
| 10 |
-
env = Env()
|
| 11 |
-
env.read_env()
|
| 12 |
-
|
| 13 |
-
USE_MOCK = env.bool("USE_MOCK", False)
|
| 14 |
-
MOCK_DIR = env.str("MOCK_DIR", "resources/mock")
|
| 15 |
-
|
| 16 |
-
# Deprecated code. because we are using mongomock-motor for database_type=embedded
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
@deprecated("This function is deprecated. Use database_type=embedded with mongomock-motor instead.")
|
| 20 |
-
def get_mock_response(url_path: str, python_module_name: str, python_method_name: str) -> Dict:
|
| 21 |
-
"""Get mock response from JSON file."""
|
| 22 |
-
logger.trace(f"BEGIN: url_path: {url_path} python_module_name: {python_module_name} python_method_name: {python_method_name}")
|
| 23 |
-
file_path = None
|
| 24 |
-
try:
|
| 25 |
-
filename = python_module_name + "_" + python_method_name
|
| 26 |
-
filename = filename.replace(".", "_")
|
| 27 |
-
filename = filename.replace("__", "_")
|
| 28 |
-
filename = filename.replace("/", "_")
|
| 29 |
-
filename = filename.replace(":", "_")
|
| 30 |
-
filename = filename.replace(" ", "_")
|
| 31 |
-
filename = filename.replace("-", "_")
|
| 32 |
-
filename = filename.replace("app_api_", "")
|
| 33 |
-
logger.trace(f"FileName : {filename}")
|
| 34 |
-
# Load mock response
|
| 35 |
-
file_path = os.path.join(MOCK_DIR, f"{filename}.json")
|
| 36 |
-
|
| 37 |
-
logger.warning(f"Mock file path: {file_path}")
|
| 38 |
-
with open(file_path, "r") as f:
|
| 39 |
-
result = json.load(f)
|
| 40 |
-
logger.trace(f"END: result: {result}")
|
| 41 |
-
return result
|
| 42 |
-
except FileNotFoundError:
|
| 43 |
-
raise HTTPException(
|
| 44 |
-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 45 |
-
detail=f"{file_path} mock file not found for endpoint: {url_path} [{python_module_name} {python_method_name}]",
|
| 46 |
-
)
|
| 47 |
-
except Exception as e:
|
| 48 |
-
raise HTTPException(
|
| 49 |
-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 50 |
-
detail=f"{file_path} error loading mock response: {str(e)}",
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
def api_response():
|
| 55 |
-
"""Decorator to handle API request and response"""
|
| 56 |
-
|
| 57 |
-
def decorator(func):
|
| 58 |
-
logger.trace(f"BEGIN: decorator: {func}")
|
| 59 |
-
|
| 60 |
-
@wraps(func)
|
| 61 |
-
async def wrapper(request: Request, *args, **kwargs):
|
| 62 |
-
logger.trace(f"BEGIN: wrapper: {request}")
|
| 63 |
-
logger.trace(f"func: {func.__name__}")
|
| 64 |
-
logger.trace(f"args: {args}")
|
| 65 |
-
logger.trace(f"kwargs: {kwargs}")
|
| 66 |
-
if not request:
|
| 67 |
-
logger.error("Request object not found in args or kwargs")
|
| 68 |
-
raise HTTPException(
|
| 69 |
-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 70 |
-
detail="Request object not found",
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
logger.trace(f"USE_MOCK value: {USE_MOCK}")
|
| 74 |
-
logger.trace(f"Request path: {request.url.path}")
|
| 75 |
-
logger.trace(f"Request method: {request.method}")
|
| 76 |
-
|
| 77 |
-
if USE_MOCK:
|
| 78 |
-
python_method_name = func.__name__
|
| 79 |
-
python_module_name = func.__module__
|
| 80 |
-
logger.warning(f"Using mock response for {request.url.path} [{request.method}] > {python_module_name}.{python_method_name}")
|
| 81 |
-
try:
|
| 82 |
-
result = get_mock_response(
|
| 83 |
-
request.url.path,
|
| 84 |
-
python_module_name,
|
| 85 |
-
python_method_name,
|
| 86 |
-
)
|
| 87 |
-
logger.trace(f"Mock response: {result}")
|
| 88 |
-
return result
|
| 89 |
-
except Exception as e:
|
| 90 |
-
logger.error(f"Error getting mock response: {str(e)}")
|
| 91 |
-
raise
|
| 92 |
-
else:
|
| 93 |
-
logger.warning("Using real response")
|
| 94 |
-
try:
|
| 95 |
-
result = await func(*args, **kwargs)
|
| 96 |
-
logger.trace(f"Real response: {result}")
|
| 97 |
-
return result
|
| 98 |
-
except Exception as e:
|
| 99 |
-
logger.error(f"Error getting real response: {str(e)}")
|
| 100 |
-
raise
|
| 101 |
-
|
| 102 |
-
return wrapper
|
| 103 |
-
|
| 104 |
-
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|