A lot of route common params clean up

This commit is contained in:
Scott Idem
2022-01-05 15:22:49 -05:00
parent 9a51e75892
commit a70f931688
5 changed files with 166 additions and 248 deletions

View File

@@ -28,42 +28,86 @@ async def get_token_header(x_token: str = Header(...)):
# ### BEGIN ### API Lib General ### async get_account_header() ###
# Updated 2022-01-05
async def get_account_header(x_account_id: str = Header(..., min_length=11, max_length=22)) -> dict:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# async def get_account_header(x_account_id: str = Header(..., min_length=11, max_length=22)) -> dict:
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# log.debug(locals())
# log.info(f'The x-account-id header has a value. x-account-id: {x_account_id}')
# if account_id := redis_lookup_id_random(table_name='account', record_id_random=x_account_id):
# log.setLevel(logging.DEBUG)
# log.info(f'Found the x-account-id with the value: {x_account_id}')
# x_account = { 'id': account_id, 'id_random': x_account_id }
# log.debug(x_account)
# return x_account
# else:
# log.warning(f'The x-account-id Account ID was not found. Account ID: {x_account_id}')
# raise HTTPException(status_code=403, detail='The x-account-id Account ID was not found.') # Forbidden
# ### END ### API Lib General ### async get_account_header() ###
# ### BEGIN ### API Lib General ### class Common_Route_Params ###
# Updated 2022-01-05
class Common_Route_Params:
def __init__(
self,
x_account_id: int,
x_account_id_random: str,
limit: int = 10,
enabled: str = 'enabled',
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.limit = limit
self.enabled = enabled
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 ### async route_commons() ###
# Updated 2022-01-05
async def common_route_params(
x_account_id: str = Header(..., min_length=11, max_length=22),
enabled: str = 'enabled', # all, enabled, disabled
limit: int = 100,
by_alias: bool = True,
exclude: Optional[list] = [],
exclude_none: Optional[bool] = True,
exclude_unset: bool = True,
include: Optional[list] = [],
response: Response = Response,
) -> Common_Route_Params:
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
log.info(f'Setting commons values')
log.info(f'The x-account-id header has a value. x-account-id: {x_account_id}')
if account_id := redis_lookup_id_random(table_name='account', record_id_random=x_account_id):
log.setLevel(logging.DEBUG)
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 with the value: {x_account_id}')
account = { 'id': account_id, 'id_random': x_account_id }
log.debug(account)
return account
# x_account = { 'id': account_id, 'id_random': x_account_id }
# log.debug(x_account)
# return x_account
else:
log.warning(f'The x-account-id Account ID was not found. Account ID: {x_account_id}')
raise HTTPException(status_code=403, detail='The x-account-id Account ID was not found.') # Forbidden
# if len(x_account_id) >= 11 and len(x_account_id) <= 22:
# log.info(f'The x-account-id header has a value. x-account-id: {x_account_id}')
# if account_id := redis_lookup_id_random(table_name='account', record_id_random=x_account_id):
# log.setLevel(logging.DEBUG)
# log.info(f'Found the x-account-id with the value: {x_account_id}')
# account = { 'id': account_id, 'id_random': x_account_id }
# x_account_id = account_id
# else:
# log.warning(f'The x-account-id Account ID was not found. Account ID: {x_account_id}')
# raise HTTPException(status_code=403, detail='The x-account-id Account ID was not found.') # Forbidden
# elif x_account_id == '':
# log.info('The x-account-id header was empty.')
# raise HTTPException(status_code=403, detail='The x-account-id header was empty.') # Forbidden
# # account = { 'id': None, 'id_random': None }
# else:
# log.info('The x-account-id header was not valid.')
# raise HTTPException(status_code=403, detail='The x-account-id header was not valid.') # Forbidden
commons = Common_Route_Params( x_account_id=x_account_id, x_account_id_random=x_account_id_random, limit=limit, enabled=enabled, by_alias=by_alias, exclude_unset=exclude_unset, response=response )
# commons = { 'limit': limit, 'enabled': enabled, 'by_alias': by_alias, 'exclude_unset': exclude_unset }
# ### END ### API Lib General ### async get_account_header() ###
log.debug(commons)
return commons
# ### END ### API Lib General ### async route_commons() ###
def secure_hash_string(string:str):