]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - portfolio.py
Add Balance class tests
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / portfolio.py
index 507f79642c62aa64343251747706173d9bf31bb8..1d8bfd5f122775df881bcbbdd3a76e8422740896 100644 (file)
@@ -32,8 +32,14 @@ class Portfolio:
         urllib3.disable_warnings()
         http = urllib3.PoolManager()
 
-        r = http.request("GET", cls.URL)
-        cls.data = json.loads(r.data)
+        try:
+            r = http.request("GET", cls.URL)
+        except Exception:
+            return
+        try:
+            cls.data = json.loads(r.data)
+        except json.JSONDecodeError:
+            cls.data = None
 
     @classmethod
     def parse_cryptoportfolio(cls):
@@ -214,6 +220,10 @@ class Balance:
         self.free = Amount(currency, free_value)
         self.used = Amount(currency, used_value)
 
+    @classmethod
+    def from_hash(cls, currency, hash_):
+        return cls(currency, hash_["total"], hash_["free"], hash_["used"])
+
     @classmethod
     def in_currency(cls, other_currency, market, action="average", type="total"):
         amounts = {}
@@ -228,10 +238,6 @@ class Balance:
     def currencies(cls):
         return cls.known_balances.keys()
 
-    @classmethod
-    def from_hash(cls, currency, hash_):
-        return cls(currency, hash_["total"], hash_["free"], hash_["used"])
-
     @classmethod
     def _fill_balances(cls, hash_):
         for key in hash_:
@@ -264,9 +270,6 @@ class Balance:
         new_repartition = cls.dispatch_assets(total_base_value)
         Trade.compute_trades(values_in_base, new_repartition, market=market)
 
-    def __int__(self):
-        return int(self.total)
-
     def __repr__(self):
         return "Balance({} [{}/{}/{}])".format(self.currency, str(self.free), str(self.used), str(self.total))