wowtoken.app-backend

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

current.py (2064B)


      1 import datetime
      2 import os
      3 from typing import List, Dict, Tuple
      4 
      5 import boto3
      6 from boto3.dynamodb.conditions import Key
      7 
      8 from wow_token.flavor import Flavor
      9 from wow_token.region import Region
     10 
     11 REGION_MAP = {
     12     'us-west-1': 'us-west-1',
     13     'us-west-2': 'us-west-2',
     14     'us-east-1': 'us-east-1',
     15     'us-east-2': 'us-east-2',
     16     'ap-south-1': 'eu-north-1',
     17     'ap-northeast-3': 'ap-northeast-1',
     18     'ap-northeast-2': 'ap-northeast-1',
     19     'ap-southeast-1': 'ap-southeast-1',
     20     'ap-southeast-2': 'ap-southeast-2',
     21     'ap-northeast-1': 'ap-northeast-1',
     22     'ca-central-1': 'us-east-1',
     23     'eu-central-1': 'eu-north-1',
     24     'eu-west-1': 'eu-west-1',
     25     'eu-west-2': 'eu-west-1',
     26     'eu-west-3': 'eu-west-3',
     27     'eu-north-1': 'eu-north-1',
     28     'sa-east-1': 'sa-east-1',
     29     'eu-south-1': 'eu-north-1'
     30 }
     31 
     32 
     33 def _region_selector():
     34     if os.environ['AWS_REGION'] in REGION_MAP:
     35         local_region = REGION_MAP[os.environ['AWS_REGION']]
     36     else:
     37         local_region = 'eu-central-1'
     38     return local_region
     39 
     40 
     41 class Current:
     42     def __init__(self):
     43         self._ddb = boto3.resource('dynamodb', region_name=_region_selector())
     44         self._tables = {
     45             Flavor.RETAIL: self._ddb.Table('wow-token-price'),
     46             Flavor.CLASSIC: self._ddb.Table('wow-token-classic-price'),
     47         }
     48 
     49     def _ddb_get_current_all(self, flavor: Flavor) -> Dict[Region, Tuple[datetime.datetime, int]]:
     50         response = self._tables[flavor].scan()
     51         data = {}
     52         for item in response['Items']:
     53             region = Region(item['region'])
     54             data[region] = (
     55                 datetime.datetime.fromtimestamp(int(item['current_time']), datetime.UTC),
     56                 int(int(item['price']) / 10_000)  # the raw copper value is what is stored in DynamoDB
     57             )
     58         return data
     59 
     60     def get_current_all(self, flavor: Flavor) -> Dict[Region, Tuple[str, int]]:
     61         data = {}
     62         for region, (timestamp, _) in self._ddb_get_current_all(flavor).items():
     63             data[region] = (timestamp.isoformat(), _)
     64         return data