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

61 lines
1.7 KiB
Python

import json
from wow_token.db.current import Current
from wow_token.flavor import Flavor
# Current is a global so it's initialized with the Lambda and stays initialized through the lifecycle
# of that Lambda runner
CURRENT_DB = Current()
# The URI for the Current function should look like /v2/current/{flavor}
def flavor_from_path(uri: str) -> Flavor:
split_uri = uri.split('/')
if split_uri[-1] == 'classic' or split_uri[-1] == 'classic.json':
return Flavor.CLASSIC
if split_uri[-1] == 'retail' or split_uri[-1] == 'retail.json':
return Flavor.RETAIL
raise NotImplementedError
def path_handler(uri: str):
flavor = flavor_from_path(uri)
return CURRENT_DB.get_current_all(flavor)
def lambda_handler(event, context):
uri = event['Records'][0]['cf']['request']['uri']
try:
data = path_handler(uri)
response = {
'status': '200',
'statusDescription': 'OK',
'headers': {
'content-type': [{
'key': 'Content-Type',
'value': 'application/json'
}]
},
'body': json.dumps(data)
}
return response
except NotImplementedError:
return {
'status': '404',
'statusDescription': 'Not Found',
'headers': {
'content-type': [{
'key': 'Content-Type',
'value': 'application/json'
}]
},
'body': json.dumps({'error': 'Not Found'})
}
def main():
print(json.dumps(CURRENT_DB.get_current_all(Flavor.RETAIL)))
if __name__ == '__main__':
main()