wowtoken.app-backend/wow-token-historical-v2.py

74 lines
2.1 KiB
Python

import json
from typing import Tuple, List
from wow_token.db.compacted import Compacted
from wow_token.db.recent import Recent
from wow_token.path_handler.math_path_handler import MathPathHandler
from wow_token.path_handler.relative_error import InvalidRelativePathError
from wow_token.path_handler.relative_path_handler import RelativePathHandler
COMPACTED_DB = Compacted()
RECENT_DB = Recent()
def handle_not_implemented_error():
return {
'status': '404',
'statusDescription': 'Not Found',
'headers': {}
}
def find_function(path) -> str:
split_uri = path.split('/')
i = 0
while split_uri[i] != 'v2':
i += 1
return split_uri[i + 1]
def path_handler(path):
function = find_function(path)
match function:
# This URI takes the form of /v2/{function}
case 'relative':
# This URI takes the form of /v2/{function}/{flavor}/{region}/{range}
# The hottest path will be the relative path, so handle that first
rph = RelativePathHandler(COMPACTED_DB, RECENT_DB)
return rph.path_handler(path)
case 'absolute':
raise NotImplementedError
case 'math':
# This URI takes the form of /v2/math/{math_function}/{flavor}/{region}/{range}
mph = MathPathHandler(COMPACTED_DB, RECENT_DB)
return mph.path_handler(path)
case _:
raise NotImplementedError
def lambda_handler(event, context):
uri = event['Records'][0]['cf']['request']['uri']
try :
data = path_handler(uri)
return {
'status': '200',
'statusDescription': 'OK',
'headers': {
'content-type': [{
'key': 'Content-Type',
'value': 'application/json'
}]
},
'body': json.dumps(data)
}
except (NotImplementedError, InvalidRelativePathError):
return json.dumps(handle_not_implemented_error())
def main():
data = path_handler('/v2/math/avg/retail/us/2m.json')
print(data)
if __name__ == '__main__':
main()