Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/customio/ns.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
# set up a logger
logger = logging.getLogger('assembly.customio.ns') # get the logger for this script

class HTTPResponseException(Exception):
class QueryException(Exception):
pass
class HTTPResponseException(QueryException):
pass

class QueryException(Exception):
class RateLimitedException(HTTPResponseException):
pass

class API:
def __init__(self) -> None:
self.rate_limited = False
Expand All @@ -49,14 +52,14 @@ async def _make_request(self, uri:str) -> str:
return await response.text()
elif response.status == 429:
self.rate_limited = True
raise HTTPResponseException(f'Error {response.status}: {response.reason}. NS API rate limited. No retry will be attempted.')
logger.error('Response error {response.status}: {response.reason}. NS API rate limited. No retry will be attempted.')
headers = response.headers
await asyncio.sleep(int(headers['Retry-After']))
self.rate_limited = False
else:
raise HTTPResponseException(f'Error {response.status}: {response.reason}')
else:
raise QueryException('Rate limited. Request blocked.')
raise RateLimitedException('Rate limited. Request blocked.')

async def _query_proposals(self, council: int) -> etree.ElementTree:
council = str(council) # convert to string for URL
Expand All @@ -81,6 +84,8 @@ async def _get_quorum(self) -> int:
numdelegates = int(xmltree.findall('./NUMDELEGATES')[0].text)
quorum = round(numdelegates * 0.06, 1)
return quorum
except RateLimitedException as e:
logger.error(str(e))
except HTTPResponseException as e:
raise QueryException(str(e))

Expand Down Expand Up @@ -117,5 +122,7 @@ async def _query_atvote(self,council:int) -> etree.ElementTree:
return None
else:
return resolutions
except RateLimitedException as e:
logger.error(str(e))
except HTTPResponseException as e:
raise QueryException(str(e))
Loading