Finally updating this...

This commit is contained in:
Scott Idem
2021-03-05 17:27:16 -05:00
parent 420180f20c
commit 28cf7ecf11
14 changed files with 1235 additions and 108 deletions

View File

@@ -1,12 +1,12 @@
import redis
import datetime, redis
from datetime import datetime, time, timedelta
#from datetime import datetime, time, timedelta
from fastapi import APIRouter, Depends, Header, HTTPException, status
from pydantic import BaseModel, EmailStr, Field
from typing import Dict, List, Optional, Set, Union
from .log import *
from .db import *
from .db_sql import *
async def get_token_header(x_token: str = Header(...)):
@@ -38,78 +38,82 @@ async def get_account_header(x_account_id: str = Header(...)):
return account
#Add the processing time to the response header.
#@app.middleware('http')
#async def add_process_time_header(request: Request, call_next):
#import time
#start_time = time.time()
#response = await call_next(request)
#process_time = time.time() - start_time
#response.headers['X-Process-Time'] = str(process_time)
#return response
#async 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')
#async def get_account_header(x_account_id: str = Header(...)):
#@app.middleware("http")
#async def get_account_header(x_account_id: str = Header(...)):
#return x_account_id
#x_account_id: str = Header(...)
#x_account_id = 'static random ID...'
#response = await call_next(request)
#print(x_account_id)
#return x_account_id
#async def get_account_header(x_account_id: str = Header(...)):
#print('get_account_header(): '+x_account_id+'z9999z')
#return x_account_id+'z9999z'
# Attempt to look up id_random key
# If success then return the id number
# Just return the value if it is an integer
# Check if the id_random value is a string and the correct length
# Attempt to look up id_random key in Redis
# If success then return the ID number
# If not success and there is a table_name then check the database table passed
# If found in database table then store in Redis
# If found in database table then store in Redis and return the ID number
def redis_lookup_id_random(record_id_random=None, table_name=None):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARN, WARNING, ERROR, EXCEPTION, CRITICAL
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if record_id_random is None: return False
if isinstance(record_id_random, bool): return False
if isinstance(record_id_random, int):
return record_id_random
elif isinstance(record_id_random, str):
pass
else:
log.warning(f'Unexpected data type: {str(type(record_id_random))} Expected type is a string 11 or 22 characters long.')
return False
if record_id_random and table_name:
# WARNING: The record_id_random string length should be checked just in case?
if len(record_id_random) < 11:
log.warning(f'The length of id_random is too short: {str(record_id_random)} ({len(record_id_random)} chars)')
return False
elif len(record_id_random) > 22:
log.warning(f'The length of id_random is too long {str(record_id_random)} ({len(record_id_random)} chars)')
return False
else:
pass
else:
log.warning('Missing table_name to select from for id_random')
return False
r = redis.Redis(host='localhost', port=6379, db=7, password=None, decode_responses=True)
key_name = 'record_id:'+record_id_random
record_id = r.get(key_name)
#print('Record ID? '+str(record_id))
log.debug(f'Record ID? {str(record_id)}')
if record_id:
print('TTL for: '+key_name+' : '+str(record_id)+' is '+str(r.ttl(key_name))+' seconds')
return record_id
log.info('The record ID was found using the record_id_random value.')
log.info(f'TTL for: {key_name} : {str(record_id)} is {str(r.ttl(key_name))} seconds')
return int(record_id)
elif table_name:
data = { 'id_random': record_id_random }
sql = """
sql = f"""
SELECT id
FROM `"""+table_name+"""` AS `table`
WHERE table.id_random = :id_random
FROM `{table_name}` AS `table`
WHERE `table`.id_random = :id_random;
"""
if select_results := sql_select(table_name=table_name, record_id_random=record_id_random): # sql_select(sql=sql, data=data)
#print('Record ID random found: '+str(select_results['id']))
record_id = select_results['id']
r.setex(key_name, timedelta(minutes=2), value=record_id)
return record_id
if select_results := sql_select(sql=sql, data=data):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(select_results)
log.debug(type(select_results))
if isinstance(select_results, dict):
log.info(f"""Record ID random found: {str(select_results['id'])}""")
if record_id := select_results.get('id'):
r.setex(key_name, datetime.timedelta(minutes=90), value=record_id)
return int(record_id)
else:
log.setLevel(logging.ERROR) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.error('The SQL result was not what was expected.')
return False
else:
log.setLevel(logging.ERROR) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.error('More than one record may have been found. There may be a duplicate id_random.')
log.error(select_results)
return False
else:
#print('Record ID random was not found')
#log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.info('Record ID random was not found')
return None
else:
print('Missing table_name to select from for id_random')
return False
#return False
log.setLevel(logging.ERROR) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.error('We should not be here. Something unexpected happened.')
return False # Just in case