This repository has been archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resolvers.py
53 lines (43 loc) · 1.73 KB
/
resolvers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from .api import Api
from .config.vehicles import VEHICLES
from .config.weapons import WEAPONS
from .exceptions import VehicleNotFound, WeaponNotFound
class Resolver:
def __init__(self):
self.url = ''
self.method = ''
self.api = Api()
def setup(self, username, platform, api_map=None, **kwargs):
"""
Prepare the request with the correct url and method
substitute the `{username}` and `{platform}` with
the correct parameters
"""
url = api_map['url']
method = api_map['method']
if 'platform' in url:
url = url.replace('{platform}', 'platform={}'.format(platform))
if 'username' in url:
url = url.replace('{username}', 'displayName={}'.format(username))
if '{vehicle} in url' and 'vehicle' in kwargs:
url = url.replace('{vehicle}', 'vehicleId={}'.format(resolve_vehicle(kwargs['vehicle'])))
if '{weapon}'in url and 'weapon' in kwargs:
url = url.replace('{weapon}', 'weaponId={}'.format(resolve_weapon(kwargs['weapon'])))
self.method = method
self.url = url
def resolve(self, username, api_key, platform, api_map=None, **kwargs):
"""
Make the request
"""
self.setup(username, platform, api_map=api_map, **kwargs)
return self.api.request(url=self.url, method=self.method, api_key=api_key)
def resolve_vehicle(vehicle_name):
try:
return VEHICLES[vehicle_name]
except KeyError:
raise VehicleNotFound('Vehicle not found in configuration file')
def resolve_weapon(weapon_name):
try:
return WEAPONS[weapon_name]
except KeyError:
raise WeaponNotFound('Weapon not found in configuration file')