From db58d9267c567f23e496236c72c2eb6da4ec71df Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Mon, 30 Jan 2023 15:06:02 -0500 Subject: [PATCH] General clean up --- app/lib_general.py | 68 +++++++++++++++++++++++++++++++++++++++ app/log.py | 39 +++++++++++++--------- app/routers/aether_cfg.py | 21 ++++++------ 3 files changed, 103 insertions(+), 25 deletions(-) diff --git a/app/lib_general.py b/app/lib_general.py index 0061503..78e51b4 100644 --- a/app/lib_general.py +++ b/app/lib_general.py @@ -26,6 +26,74 @@ def get_token_header(x_token: str = 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 ### # Updated 2022-01-05 class Common_Route_Params: diff --git a/app/log.py b/app/log.py index b9aebf7..d8ebd05 100644 --- a/app/log.py +++ b/app/log.py @@ -28,22 +28,30 @@ logging.config.dictConfig({ 'class': 'logging.handlers.RotatingFileHandler', 'formatter': 'long', 'filename': settings.LOG_PATH['app'], - 'maxBytes': 5120000, # 5120000 = 5 MB - 'backupCount': 5 + 'maxBytes': 5242880, # 524,2880 = 5 MB + 'backupCount': 10 }, - 'log_file_warning': { - 'level': 'WARNING', - 'class': 'logging.handlers.RotatingFileHandler', - 'formatter': 'long', - 'filename': settings.LOG_PATH['app_warning'], - 'maxBytes': 512000, # 512000 = .512 MB - 'backupCount': 5 - }, - 'test_handler': { - 'class': 'logging.StreamHandler', - 'level': 'INFO', - 'formatter': 'short', - } + # 'log_file_warning': { + # 'level': 'WARNING', + # 'class': 'logging.handlers.RotatingFileHandler', + # 'formatter': 'long', + # 'filename': settings.LOG_PATH['app_warning'], + # 'maxBytes': 512000, # 524,288 = 512KB + # 'backupCount': 5 + # }, + # 'test_handler': { + # 'class': 'logging.StreamHandler', + # 'level': 'INFO', + # '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': { # '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': ['console'], 'propagate': True}, # 'uvicorn.access': {'handlers': ['access'], 'level': 'INFO', 'propagate': False}, + # 'gunicorn': {'handlers': ['console'], 'level': 'INFO'}, }, 'root': { 'handlers': ['log_file_all'], #, 'log_file_all', 'log_file_warning'], diff --git a/app/routers/aether_cfg.py b/app/routers/aether_cfg.py index a5c48b4..cf10eaa 100644 --- a/app/routers/aether_cfg.py +++ b/app/routers/aether_cfg.py @@ -4,7 +4,7 @@ from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, Resp from pydantic import BaseModel, EmailStr, Field 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.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), # commons: Common_Route_Params = Depends(common_route_params), - x_account_id: str = Header(None, min_length=11, max_length=22), - response: Response = Response, + commons: Common_Route_Params_No_Account_ID = Depends(common_route_params_no_account_id), + # 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.debug(locals()) @@ -35,9 +36,9 @@ async def get_aether_cfg_obj( as_list = False, max_count = 1, ): - return mk_resp(data=sql_select_result) + return mk_resp(data=sql_select_result, response=commons.response) 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) @@ -45,11 +46,11 @@ async def get_aether_flask_cfg_obj( aether_flask_cfg_id: int, # 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), - x_account_id: str = Header(None, min_length=11, max_length=22), - response: Response = Response, + commons: Common_Route_Params_No_Account_ID = Depends(common_route_params_no_account_id), ): - log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if sql_select_result := sql_select( @@ -58,6 +59,6 @@ async def get_aether_flask_cfg_obj( as_list = False, max_count = 1, ): - return mk_resp(data=sql_select_result) + return mk_resp(data=sql_select_result, response=commons.response) else: - return mk_resp(data=None, status_code=404) + return mk_resp(data=None, status_code=404, response=commons.response)