Handle Agora library exceptions properly

This commit is contained in:
Mark Veidemanis 2022-01-27 11:36:17 +00:00
parent 965bba8d15
commit 72afa1b25b
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 29 additions and 12 deletions

View File

@ -15,6 +15,20 @@ from datetime import datetime
from settings import settings
def handle_exceptions(func):
def inner_function(*args, **kwargs):
try:
rtrn = func(*args, **kwargs)
except (ReadTimeout, ReadError):
return False
if "success" in rtrn:
if "message" in rtrn:
if not rtrn["success"] and rtrn["message"] == "API ERROR":
return False
return inner_function
class Agora(object):
"""
AgoraDesk API handler.
@ -52,11 +66,9 @@ class Agora(object):
self.lc_cheat = LoopingCall(self._update_prices)
self.lc_cheat.start(int(settings.Agora.CheatSec))
@handle_exceptions
def wrap_dashboard(self):
try:
dash = self.agora.dashboard_seller()
except ReadTimeout:
return False
if not dash:
return False
dash_tmp = {}
@ -79,10 +91,7 @@ class Agora(object):
self.dashboard_hook(dash_tmp)
# Get recent messages
try:
self.get_recent_messages()
except ReadTimeout:
pass
return dash_tmp
def get_dashboard(self):
@ -150,6 +159,7 @@ class Agora(object):
current_trades.append(reference)
self.tx.cleanup(current_trades)
@handle_exceptions
def dashboard_release_urls(self):
"""
Get information about our open trades.
@ -183,15 +193,13 @@ class Agora(object):
return dash_tmp
@handle_exceptions
def get_recent_messages(self, send_irc=True):
"""
Get recent messages.
"""
messages_tmp = {}
try:
messages = self.agora.recent_messages()
except ReadError:
return False
if not messages["success"]:
return False
if "data" not in messages["response"]:
@ -230,6 +238,7 @@ class Agora(object):
return messages_tmp
@handle_exceptions
def enum_ad_ids(self, page=0):
ads = self.agora._api_call(api_method="ads", query_values={"page": page})
ads_total = []
@ -244,6 +253,7 @@ class Agora(object):
ads_total.append(ad)
return ads_total
@handle_exceptions
def enum_ads(self, page=0):
ads = self.agora._api_call(api_method="ads", query_values={"page": page})
ads_total = []
@ -272,6 +282,7 @@ class Agora(object):
self.log.debug("Seconds ago date for {date} ^ {now}: {x}", date=date, now=str(now), x=sec_ago_date)
return sec_ago_date < 172800
@handle_exceptions
def enum_public_ads(self, coin, currency, page=0):
# buy-monero-online, buy-bitcoin-online
# Work around Agora weirdness calling it bitcoins
@ -319,6 +330,7 @@ class Agora(object):
"""
deferToThread(self.update_prices, xmr, btc)
@handle_exceptions
def update_prices(self, xmr=True, btc=True):
our_ads = self.enum_ads()
currencies = [x[3].lower() for x in our_ads]
@ -443,6 +455,7 @@ class Agora(object):
self.log.debug("Cheapest ad above our min that is not us: {x}", x=cheapest_ad)
return cheapest_ad[3] - 0.001
@handle_exceptions
def nuke_ads(self):
"""
Delete all of our adverts.
@ -487,6 +500,7 @@ class Agora(object):
max_local = max_usd * rates[currency]
return (min_local, max_local)
@handle_exceptions
def create_ad(self, asset, countrycode, currency):
"""
Post an ad with the given asset in a country with a given currency.
@ -610,6 +624,7 @@ class Agora(object):
# yield False
# yield rtrn
@handle_exceptions
def strip_duplicate_ads(self):
"""
Remove duplicate ads.
@ -631,6 +646,7 @@ class Agora(object):
actioned.append(rtrn["success"])
return all(actioned)
@handle_exceptions
def release_funds(self, contact_id):
"""
Release funds for a contact_id.
@ -647,6 +663,7 @@ class Agora(object):
return rtrn
@handle_exceptions
def withdraw_funds(self):
"""
Withdraw excess funds to our XMR/BTC wallets.