Update api.py
This commit is contained in:
parent
849964b659
commit
b19a52d1aa
@ -4,12 +4,15 @@ from nullptr.models.waypoint import Waypoint
|
||||
from nullptr.models.marketplace import Marketplace
|
||||
from nullptr.models.jumpgate import Jumpgate
|
||||
from .util import *
|
||||
|
||||
from time import sleep
|
||||
class ApiError(Exception):
|
||||
def __init__(self, msg, code):
|
||||
super().__init__(msg)
|
||||
self.code = code
|
||||
|
||||
class ApiLimitError(Exception):
|
||||
pass
|
||||
|
||||
class Api:
|
||||
def __init__(self, store, agent):
|
||||
self.agent = agent
|
||||
@ -23,13 +26,22 @@ class Api:
|
||||
return self.agent.token
|
||||
|
||||
def request(self, method, path, data=None, need_token=True, params={}):
|
||||
try:
|
||||
return self.request_once(method, path, data, need_token, params)
|
||||
except ApiLimitError:
|
||||
print('oops, hit the limit. take a break')
|
||||
sleep(10)
|
||||
return self.request_once(method, path, data, need_token, params)
|
||||
|
||||
def request_once(self, method, path, data=None, need_token=True, params={}):
|
||||
headers = {}
|
||||
if need_token:
|
||||
headers['Authorization'] = 'Bearer ' + self.token()
|
||||
if method == 'get':
|
||||
params['limit'] = 20
|
||||
r = requests.request(method, self.root+path, json=data, headers=headers, params=params)
|
||||
|
||||
if r.status_code == 429:
|
||||
raise ApiLimitError()
|
||||
result = r.json()
|
||||
self.last_result = result
|
||||
if result is None:
|
||||
|
Loading…
Reference in New Issue
Block a user