wowtoken.app-backend

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

wow-token-current-v2.py (1697B)


      1 import json
      2 
      3 from wow_token.db.current import Current
      4 from wow_token.flavor import Flavor
      5 
      6 # Current is a global so it's initialized with the Lambda and stays initialized through the lifecycle
      7 # of that Lambda runner
      8 
      9 CURRENT_DB = Current()
     10 
     11 # The URI for the Current function should look like /v2/current/{flavor}
     12 def flavor_from_path(uri: str) -> Flavor:
     13     split_uri = uri.split('/')
     14     if split_uri[-1] == 'classic' or split_uri[-1] == 'classic.json':
     15         return Flavor.CLASSIC
     16     if split_uri[-1] == 'retail' or split_uri[-1] == 'retail.json':
     17         return Flavor.RETAIL
     18     raise NotImplementedError
     19 
     20 
     21 def path_handler(uri: str):
     22     flavor = flavor_from_path(uri)
     23     return CURRENT_DB.get_current_all(flavor)
     24 
     25 
     26 def lambda_handler(event, context):
     27     uri = event['Records'][0]['cf']['request']['uri']
     28     try:
     29         data = path_handler(uri)
     30         response = {
     31             'status': '200',
     32             'statusDescription': 'OK',
     33             'headers': {
     34                 'content-type': [{
     35                     'key': 'Content-Type',
     36                     'value': 'application/json'
     37                 }]
     38             },
     39             'body': json.dumps(data)
     40         }
     41         return response
     42     except NotImplementedError:
     43         return {
     44             'status': '404',
     45             'statusDescription': 'Not Found',
     46             'headers': {
     47                 'content-type': [{
     48                     'key': 'Content-Type',
     49                     'value': 'application/json'
     50                 }]
     51             },
     52             'body': json.dumps({'error': 'Not Found'})
     53         }
     54 
     55 
     56 def main():
     57     print(json.dumps(CURRENT_DB.get_current_all(Flavor.RETAIL)))
     58 
     59 
     60 if __name__ == '__main__':
     61     main()