import html2text, time from typing import Dict, List, Optional, Set, Union import logging from fastapi import Header, HTTPException, Response, status from app.log import logger_reset from app.config import settings from app.db_sql import redis_lookup_id_random, sql_select from app.lib_email import send_email from app.lib_export import create_export_file, return_full_tmp_path from app.lib_jwt import sign_jwt, decode_jwt from app.lib_hash import secure_hash_string, verify_secure_hash_string log = logging.getLogger(__name__) # ### BEGIN ### API Lib General ### async get_token_header() ### def get_token_header(x_token: str = Header(...)): if x_token != 'fake-super-secret-token': raise HTTPException(status_code=400, detail='X-Token header invalid') # ### 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 = None, x_account_id_random: str|None = None, x_no_account_id_token: str|None = None, enabled: str = 'enabled', limit: int = 10, offset: int = 0, by_alias: bool = True, exclude_unset: bool = False, response = None, ): self.x_account_id = x_account_id self.x_account_id_random = x_account_id_random self.x_no_account_id_token = x_no_account_id_token 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 = False, # 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: def __init__( self, x_account_id: int, x_account_id_random: str, x_no_account_id_token: str|None = None, enabled: str = 'enabled', limit: int = 10, offset: int = 0, by_alias: bool = True, exclude_unset: bool = False, response = None, ): self.x_account_id = x_account_id self.x_account_id_random = x_account_id_random self.x_no_account_id_token = x_no_account_id_token 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 2022-02-15 @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( # x_account_id: str = Header(..., min_length=11, max_length=22), # NOTE WARNING: Commented out 2023-08-17 x_account_id: str = Header(None, min_length=11, max_length=22), # NOTE WARNING: Changed to this 2023-08-17 x_no_account_id: str = Header(None, min_length=11, max_length=22), # NOTE WARNING: Changed to this 2023-08-17 # x_aether_api_key: Optional[str] = Header(..., min_length=11, max_length=22), # x_aether_api_token: Optional[str] = Header(..., min_length=11, max_length=22), # x_aether_jwt_token: Optional[str] = Header(..., min_length=11, max_length=50), x_no_account_id_token: str|None = None, # NOTE: Not a header value! Added 2023-08-17 enabled: str = 'enabled', # all, enabled, disabled limit: int = 100, offset: int = 0, by_alias: bool = True, exclude_unset: bool = False, exclude_defaults: Optional[bool] = False, exclude_none: Optional[bool] = False, # 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|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: 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}') 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 elif x_no_account_id and len(x_no_account_id) > 10: log.warning(f'Found the x_no_account_id header param with the value: {x_no_account_id}') x_account_id = None x_account_id_random = '--- NOT SET ---' elif x_no_account_id_token and len(x_no_account_id_token) > 10: # NOTE: Not a header value! # NOTE WARNING: This token should be verified and able to be disabled quickly. log.warning(f'Found the x_no_account_id_token URL param with the value: {x_no_account_id_token}') if x_account_id := redis_lookup_id_random(table_name='account', record_id_random=x_no_account_id_token): log.info(f'Found the x-account-id header with the value: {x_account_id}') x_account_id_random = x_no_account_id_token else: x_account_id = 0 x_account_id_random = '' x_account_id = 0 x_account_id_random = '--- NOT SET ---' else: log.warning(f'The x-account-id and x-no-account-id-token headers were not found.') raise HTTPException(status_code=403, detail='The x-account-id and x-no-account-id-token headers were not found.') # Forbidden if x_account_id: commons = Common_Route_Params( x_account_id=x_account_id, x_account_id_random=x_account_id_random, x_no_account_id_token=x_no_account_id_token, limit=limit, offset=offset, enabled=enabled, by_alias=by_alias, exclude_unset=exclude_unset, response=response ) else: commons = Common_Route_Params_No_Account_ID( x_account_id=None, x_account_id_random=None, x_no_account_id_token=x_no_account_id_token, 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_Min ### # Updated 2022-01-05 # NOTE: Is this essentially the same as Common_Route_Params_No_Account_ID above? class Common_Route_Params_Min: 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 = False, 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_Min ### # ### BEGIN ### API Lib General ### common_route_params_min() ### # Updated 2022-02-15 # NOTE: Is this essentially the same as common_route_params_no_account_id above? @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_min( 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 = False, # 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: 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') log.debug(f'X Account ID: {x_account_id}') if x_account_id: 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}') 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 else: x_account_id_random = None commons = Common_Route_Params_Min( 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_min() ###