Improve Agora error handling

This commit is contained in:
Mark Veidemanis 2022-01-27 12:08:26 +00:00
parent 72afa1b25b
commit f7ad74c510
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 67 additions and 15 deletions

View File

@ -14,6 +14,8 @@ from datetime import datetime
# Project imports
from settings import settings
log = Logger("agora.global")
def handle_exceptions(func):
def inner_function(*args, **kwargs):
@ -21,10 +23,13 @@ def handle_exceptions(func):
rtrn = func(*args, **kwargs)
except (ReadTimeout, ReadError):
return False
if isinstance(rtrn, dict):
if "success" in rtrn:
if "message" in rtrn:
if not rtrn["success"] and rtrn["message"] == "API ERROR":
log.error("API error: {code}", code=rtrn["response"]["error"]["error_code"])
return False
return rtrn
return inner_function
@ -69,7 +74,7 @@ class Agora(object):
@handle_exceptions
def wrap_dashboard(self):
dash = self.agora.dashboard_seller()
if not dash:
if dash is False:
return False
dash_tmp = {}
if "data" not in dash["response"].keys():
@ -100,8 +105,8 @@ class Agora(object):
"""
dash = self.wrap_dashboard()
rtrn = []
if not dash.items():
return
if dash is False:
return False
for contact_id, contact in dash.items():
reference = self.tx.tx_to_ref(contact_id)
buyer = contact["data"]["buyer"]["username"]
@ -168,6 +173,8 @@ class Agora(object):
:rtype: list or bool
"""
dash = self.agora.dashboard_seller()
if dash is False:
return False
dash_tmp = []
if "data" not in dash["response"]:
self.log.error("Data not in dashboard response: {content}", content=dash)
@ -200,6 +207,8 @@ class Agora(object):
"""
messages_tmp = {}
messages = self.agora.recent_messages()
if messages is False:
return False
if not messages["success"]:
return False
if "data" not in messages["response"]:
@ -241,6 +250,8 @@ class Agora(object):
@handle_exceptions
def enum_ad_ids(self, page=0):
ads = self.agora._api_call(api_method="ads", query_values={"page": page})
if ads is False:
return False
ads_total = []
if not ads["success"]:
return False
@ -249,13 +260,21 @@ class Agora(object):
if "pagination" in ads["response"]:
if "next" in ads["response"]["pagination"]:
page += 1
for ad in self.enum_ad_ids(page):
ads_iter = self.enum_ad_ids(page)
if ads_iter is None:
return False
if ads_iter is False:
return False
for ad in ads_iter:
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})
if ads is False:
print("enum ads is false for", page)
return False
ads_total = []
if not ads["success"]:
return False
@ -264,7 +283,15 @@ class Agora(object):
if "pagination" in ads["response"]:
if "next" in ads["response"]["pagination"]:
page += 1
for ad in self.enum_ads(page):
ads_iter = self.enum_ads(page)
print("ADS ITER", ads_iter)
if ads_iter is None:
print("enum ads iter is none for", page)
return False
if ads_iter is False:
print("enum ads iter is false for", page)
return False
for ad in ads_iter:
ads_total.append([ad[0], ad[1], ad[2], ad[3]])
return ads_total
@ -289,7 +316,8 @@ class Agora(object):
if coin == "bitcoin":
coin = "bitcoins"
ads = self.agora._api_call(api_method=f"buy-{coin}-online/{currency}/REVOLUT", query_values={"page": page})
if not ads["success"]:
if ads is False:
print("public ads is false for", coin, currency)
return False
for ad in ads["response"]["data"]["ad_list"]:
if ad["data"]["online_provider"] == "REVOLUT":
@ -301,7 +329,14 @@ class Agora(object):
if "pagination" in ads["response"]:
if "next" in ads["response"]["pagination"]:
page += 1
for ad in self.enum_public_ads(coin, currency, page):
ads_iter = self.enum_public_ads(coin, currency, page)
if ads_iter is None:
print("public ads iter is false for", coin, currency, page)
return False
if ads_iter is False:
print("public ads iter is none for", coin, currency, page)
return False
for ad in ads_iter:
yield [ad[0], ad[1], ad[2]]
def wrap_public_ads(self, asset, currency, rates=None):
@ -312,7 +347,11 @@ class Agora(object):
coin = "monero"
elif asset == "BTC":
coin = "bitcoin"
ads = list(self.enum_public_ads(coin, currency.upper()))
ads_obj = self.enum_public_ads(coin, currency.upper())
if ads_obj is False:
print("wrap public ads is false for", asset, currency)
return False
ads = list(ads_obj)
if not rates:
# Set the price based on the asset
base_currency_price = self.cg.get_price(ids=coin, vs_currencies=currency)[coin][currency.lower()]
@ -333,6 +372,12 @@ class Agora(object):
@handle_exceptions
def update_prices(self, xmr=True, btc=True):
our_ads = self.enum_ads()
if our_ads is None:
print("our ads is none for", xmr, btc)
return False
if our_ads is False:
print("our ads is false for", xmr, btc)
return False
currencies = [x[3].lower() for x in our_ads]
public_ad_dict_xmr = {}
public_ad_dict_btc = {}
@ -343,8 +388,14 @@ class Agora(object):
rates_btc = rates_crypto["bitcoin"][currency]
if xmr:
public_ad_dict_xmr[currency] = self.wrap_public_ads("XMR", currency, rates=rates_xmr)
if public_ad_dict_xmr[currency] is False:
print("public ad dict xmr is false for", currency)
return False
if btc:
public_ad_dict_btc[currency] = self.wrap_public_ads("BTC", currency, rates=rates_btc)
if public_ad_dict_btc[currency] is False:
print("public ad dict btc is false for", currency)
return False
except KeyError:
self.log.error("Error getting public ads for currency {currency}", currency=currency)
continue
@ -370,7 +421,8 @@ class Agora(object):
# Get all of our ads
our_ads_list = [ad for ad in public_ads if ad[1] == settings.Agora.Username]
if not len(our_ads_list) == 1:
self.log.error("More ads than expected: {ads}", ads=our_ads_list)
if not len(set([x[3] for x in our_ads_list])) == 1:
self.log.error("Our ads have different margins: {ads}", ads=our_ads_list)
# Get one from the list, they're probably the same, if not we will deal with the other
# ones in a later pass
@ -464,7 +516,7 @@ class Agora(object):
"""
ads = self.enum_ad_ids()
return_ids = []
if not ads:
if ads is False:
return False
for ad_id in ads:
rtrn = self.agora.ad_delete(ad_id)
@ -580,7 +632,7 @@ class Agora(object):
for asset in loads(settings.Agora.AssetList):
for currency, countrycode in loads(settings.Agora.DistList):
rtrn = self.create_ad(asset, countrycode, currency)
if not rtrn:
if rtrn is False:
return False
yield rtrn