General clean up
This commit is contained in:
@@ -26,6 +26,74 @@ def get_token_header(x_token: str = Header(...)):
|
|||||||
# ### END ### API Lib General ### async get_token_header() ###
|
# ### END ### API Lib General ### async get_token_header() ###
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# ### BEGIN ### API Lib General ### class Common_Route_Params ###
|
||||||
|
# Updated 2023-01-30
|
||||||
|
class Common_Route_Params_No_Account_ID:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
x_account_id: int = None,
|
||||||
|
x_account_id_random: str = None,
|
||||||
|
enabled: str = 'enabled',
|
||||||
|
limit: int = 10,
|
||||||
|
offset: int = 0,
|
||||||
|
by_alias: bool = True,
|
||||||
|
exclude_unset: bool = True,
|
||||||
|
response = None,
|
||||||
|
):
|
||||||
|
self.x_account_id = x_account_id
|
||||||
|
self.x_account_id_random = x_account_id_random
|
||||||
|
self.enabled = enabled
|
||||||
|
self.limit = limit
|
||||||
|
self.offset = offset
|
||||||
|
self.by_alias = by_alias
|
||||||
|
self.exclude_unset = exclude_unset
|
||||||
|
self.response = response
|
||||||
|
# log.debug(response)
|
||||||
|
# ### END ### API Lib General ### class Common_Route_Params ###
|
||||||
|
|
||||||
|
|
||||||
|
# ### BEGIN ### API Lib General ### common_route_params() ###
|
||||||
|
# Updated 2023-01-30
|
||||||
|
@logger_reset # This breaks things for some reason when the function is async. Do not use async def common_route_params()!
|
||||||
|
def common_route_params_no_account_id(
|
||||||
|
x_account_id: str = Header(None, min_length=11, max_length=22),
|
||||||
|
enabled: str = 'enabled', # all, enabled, disabled
|
||||||
|
limit: int = 100,
|
||||||
|
offset: int = 0,
|
||||||
|
by_alias: bool = True,
|
||||||
|
exclude_none: Optional[bool] = True,
|
||||||
|
exclude_unset: bool = True,
|
||||||
|
# NOTE: Uncommenting either exclude or include breaks the JSON body format. I do not know why? Should be: {} Becomes this: {"obj_name": {"data_name": "data_value"}} -STI 2022-01-05
|
||||||
|
# exclude: Optional[list] = [], # Leaving this and include commented out
|
||||||
|
# include: Optional[list] = [], # Leaving this and exclude commented out
|
||||||
|
response: Response = Response,
|
||||||
|
log_lvl: int = logging.WARNING, # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
|
) -> Common_Route_Params_No_Account_ID:
|
||||||
|
log.setLevel(log_lvl)
|
||||||
|
log.debug(locals())
|
||||||
|
|
||||||
|
log.info(f'Setting commons values: x_account_id, x_account_id_random, limit, offset, enabled, by_alias, exclude_unset, response')
|
||||||
|
|
||||||
|
x_account_id_random = x_account_id
|
||||||
|
|
||||||
|
if x_account_id := redis_lookup_id_random(table_name='account', record_id_random=x_account_id):
|
||||||
|
log.info(f'Found the x-account-id header with the value: {x_account_id}')
|
||||||
|
elif x_account_id is None:
|
||||||
|
log.warning(f'No x-account-id header value passed')
|
||||||
|
else:
|
||||||
|
log.warning(f'The x-account-id header was found, but the Account ID was not found or is not valid. Account ID: {x_account_id}')
|
||||||
|
raise HTTPException(status_code=403, detail='The x-account-id Account ID was not found.') # Forbidden
|
||||||
|
|
||||||
|
commons = Common_Route_Params_No_Account_ID( x_account_id=x_account_id, x_account_id_random=x_account_id_random, limit=limit, offset=offset, enabled=enabled, by_alias=by_alias, exclude_unset=exclude_unset, response=response )
|
||||||
|
|
||||||
|
log.debug(commons)
|
||||||
|
|
||||||
|
return commons
|
||||||
|
# ### END ### API Lib General ### async common_route_params() ###
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# ### BEGIN ### API Lib General ### class Common_Route_Params ###
|
# ### BEGIN ### API Lib General ### class Common_Route_Params ###
|
||||||
# Updated 2022-01-05
|
# Updated 2022-01-05
|
||||||
class Common_Route_Params:
|
class Common_Route_Params:
|
||||||
|
|||||||
39
app/log.py
39
app/log.py
@@ -28,22 +28,30 @@ logging.config.dictConfig({
|
|||||||
'class': 'logging.handlers.RotatingFileHandler',
|
'class': 'logging.handlers.RotatingFileHandler',
|
||||||
'formatter': 'long',
|
'formatter': 'long',
|
||||||
'filename': settings.LOG_PATH['app'],
|
'filename': settings.LOG_PATH['app'],
|
||||||
'maxBytes': 5120000, # 5120000 = 5 MB
|
'maxBytes': 5242880, # 524,2880 = 5 MB
|
||||||
'backupCount': 5
|
'backupCount': 10
|
||||||
},
|
},
|
||||||
'log_file_warning': {
|
# 'log_file_warning': {
|
||||||
'level': 'WARNING',
|
# 'level': 'WARNING',
|
||||||
'class': 'logging.handlers.RotatingFileHandler',
|
# 'class': 'logging.handlers.RotatingFileHandler',
|
||||||
'formatter': 'long',
|
# 'formatter': 'long',
|
||||||
'filename': settings.LOG_PATH['app_warning'],
|
# 'filename': settings.LOG_PATH['app_warning'],
|
||||||
'maxBytes': 512000, # 512000 = .512 MB
|
# 'maxBytes': 512000, # 524,288 = 512KB
|
||||||
'backupCount': 5
|
# 'backupCount': 5
|
||||||
},
|
# },
|
||||||
'test_handler': {
|
# 'test_handler': {
|
||||||
'class': 'logging.StreamHandler',
|
# 'class': 'logging.StreamHandler',
|
||||||
'level': 'INFO',
|
# 'level': 'INFO',
|
||||||
'formatter': 'short',
|
# 'formatter': 'short',
|
||||||
}
|
# },
|
||||||
|
# 'test_handler_all_rotate': {
|
||||||
|
# 'class': 'logging.handlers.RotatingFileHandler',
|
||||||
|
# 'level': 'NOTSET',
|
||||||
|
# 'formatter': 'short',
|
||||||
|
# 'filename': '/logs/test_rotate.log',
|
||||||
|
# 'maxBytes': 100000, # 5120000 = 5 MB
|
||||||
|
# 'backupCount': 2,
|
||||||
|
# }
|
||||||
},
|
},
|
||||||
'loggers': {
|
'loggers': {
|
||||||
# 'uvicorn': {'handlers': ['default'], 'level': 'INFO'},
|
# 'uvicorn': {'handlers': ['default'], 'level': 'INFO'},
|
||||||
@@ -51,6 +59,7 @@ logging.config.dictConfig({
|
|||||||
# 'uvicorn.error': {'level': 'INFO', 'handlers': ['default'], 'propagate': True},
|
# 'uvicorn.error': {'level': 'INFO', 'handlers': ['default'], 'propagate': True},
|
||||||
# 'uvicorn.error': {'level': 'INFO', 'handlers': ['console'], 'propagate': True},
|
# 'uvicorn.error': {'level': 'INFO', 'handlers': ['console'], 'propagate': True},
|
||||||
# 'uvicorn.access': {'handlers': ['access'], 'level': 'INFO', 'propagate': False},
|
# 'uvicorn.access': {'handlers': ['access'], 'level': 'INFO', 'propagate': False},
|
||||||
|
# 'gunicorn': {'handlers': ['console'], 'level': 'INFO'},
|
||||||
},
|
},
|
||||||
'root': {
|
'root': {
|
||||||
'handlers': ['log_file_all'], #, 'log_file_all', 'log_file_warning'],
|
'handlers': ['log_file_all'], #, 'log_file_all', 'log_file_warning'],
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, Resp
|
|||||||
from pydantic import BaseModel, EmailStr, Field
|
from pydantic import BaseModel, EmailStr, Field
|
||||||
from typing import Dict, List, Optional, Set, Union
|
from typing import Dict, List, Optional, Set, Union
|
||||||
|
|
||||||
from app.lib_general import log, logging, common_route_params, Common_Route_Params
|
from app.lib_general import log, logging, common_route_params, Common_Route_Params, common_route_params_no_account_id, Common_Route_Params_No_Account_ID
|
||||||
from app.config import settings
|
from app.config import settings
|
||||||
from app.db_sql import sql_enable_part, sql_insert, sql_update, sql_insert_or_update, sql_limit_offset_part, sql_select, sql_delete, redis_lookup_id_random
|
from app.db_sql import sql_enable_part, sql_insert, sql_update, sql_insert_or_update, sql_limit_offset_part, sql_select, sql_delete, redis_lookup_id_random
|
||||||
|
|
||||||
@@ -23,8 +23,9 @@ async def get_aether_cfg_obj(
|
|||||||
# aether_cfg_id: str = Query(..., min_length=1, max_length=22),
|
# aether_cfg_id: str = Query(..., min_length=1, max_length=22),
|
||||||
|
|
||||||
# commons: Common_Route_Params = Depends(common_route_params),
|
# commons: Common_Route_Params = Depends(common_route_params),
|
||||||
x_account_id: str = Header(None, min_length=11, max_length=22),
|
commons: Common_Route_Params_No_Account_ID = Depends(common_route_params_no_account_id),
|
||||||
response: Response = Response,
|
# x_account_id: str = Header(None, min_length=11, max_length=22),
|
||||||
|
# response: Response = Response,
|
||||||
):
|
):
|
||||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
log.debug(locals())
|
log.debug(locals())
|
||||||
@@ -35,9 +36,9 @@ async def get_aether_cfg_obj(
|
|||||||
as_list = False,
|
as_list = False,
|
||||||
max_count = 1,
|
max_count = 1,
|
||||||
):
|
):
|
||||||
return mk_resp(data=sql_select_result)
|
return mk_resp(data=sql_select_result, response=commons.response)
|
||||||
else:
|
else:
|
||||||
return mk_resp(data=None, status_code=404)
|
return mk_resp(data=None, status_code=404, response=commons.response)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/aether/flask/cfg/{aether_flask_cfg_id}', response_model=Resp_Body_Base)
|
@router.get('/aether/flask/cfg/{aether_flask_cfg_id}', response_model=Resp_Body_Base)
|
||||||
@@ -45,11 +46,11 @@ async def get_aether_flask_cfg_obj(
|
|||||||
aether_flask_cfg_id: int,
|
aether_flask_cfg_id: int,
|
||||||
# aether_flask_cfg_id: str = Query(..., min_length=1, max_length=22),
|
# aether_flask_cfg_id: str = Query(..., min_length=1, max_length=22),
|
||||||
|
|
||||||
|
# NOTE: The x_account_id header value is not required.
|
||||||
# commons: Common_Route_Params = Depends(common_route_params),
|
# commons: Common_Route_Params = Depends(common_route_params),
|
||||||
x_account_id: str = Header(None, min_length=11, max_length=22),
|
commons: Common_Route_Params_No_Account_ID = Depends(common_route_params_no_account_id),
|
||||||
response: Response = Response,
|
|
||||||
):
|
):
|
||||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
log.debug(locals())
|
log.debug(locals())
|
||||||
|
|
||||||
if sql_select_result := sql_select(
|
if sql_select_result := sql_select(
|
||||||
@@ -58,6 +59,6 @@ async def get_aether_flask_cfg_obj(
|
|||||||
as_list = False,
|
as_list = False,
|
||||||
max_count = 1,
|
max_count = 1,
|
||||||
):
|
):
|
||||||
return mk_resp(data=sql_select_result)
|
return mk_resp(data=sql_select_result, response=commons.response)
|
||||||
else:
|
else:
|
||||||
return mk_resp(data=None, status_code=404)
|
return mk_resp(data=None, status_code=404, response=commons.response)
|
||||||
|
|||||||
Reference in New Issue
Block a user