wowtoken.app-backend

git clone https://git.emily.sh/wowtoken-app/wowtoken.app-backend.git
Log | Files | Refs | LICENSE

wow-token-historical-v2.py (2147B)


      1 import json
      2 from typing import Tuple, List
      3 
      4 from wow_token.db.compacted import Compacted
      5 from wow_token.db.recent import Recent
      6 from wow_token.path_handler.math_path_handler import MathPathHandler
      7 from wow_token.path_handler.relative_error import InvalidRelativePathError
      8 from wow_token.path_handler.relative_path_handler import RelativePathHandler
      9 
     10 COMPACTED_DB = Compacted()
     11 RECENT_DB = Recent()
     12 
     13 
     14 def handle_not_implemented_error():
     15     return {
     16         'status': '404',
     17         'statusDescription': 'Not Found',
     18         'headers': {}
     19     }
     20 
     21 def find_function(path) -> str:
     22     split_uri = path.split('/')
     23     i = 0
     24     while split_uri[i] != 'v2':
     25         i += 1
     26     return split_uri[i + 1]
     27 
     28 
     29 def path_handler(path):
     30 
     31     function = find_function(path)
     32     match function:
     33         # This URI takes the form of /v2/{function}
     34         case 'relative':
     35             # This URI takes the form of /v2/{function}/{flavor}/{region}/{range}
     36             # The hottest path will be the relative path, so handle that first
     37             rph = RelativePathHandler(COMPACTED_DB, RECENT_DB)
     38             return rph.path_handler(path)
     39         case 'absolute':
     40             raise NotImplementedError
     41         case 'math':
     42             # This URI takes the form of /v2/math/{math_function}/{flavor}/{region}/{range}
     43             mph = MathPathHandler(COMPACTED_DB, RECENT_DB)
     44             return mph.path_handler(path)
     45         case _:
     46             raise NotImplementedError
     47 
     48 
     49 def lambda_handler(event, context):
     50     uri = event['Records'][0]['cf']['request']['uri']
     51     try :
     52         data = path_handler(uri)
     53         return {
     54             'status': '200',
     55             'statusDescription': 'OK',
     56             'headers': {
     57                 'content-type': [{
     58                     'key': 'Content-Type',
     59                     'value': 'application/json'
     60                 }]
     61             },
     62             'body': json.dumps(data)
     63         }
     64     except (NotImplementedError, InvalidRelativePathError):
     65         return json.dumps(handle_not_implemented_error())
     66 
     67 
     68 def main():
     69     data = path_handler('/v2/math/avg/retail/us/2m.json')
     70     print(data)
     71 
     72 
     73 if __name__ == '__main__':
     74     main()