Пример #1
0
 def _check_updates(self):
     response = get("https://api.ratesapi.io/api/latest")
     if response.status_code == 200:
         storage = Storage(storage_name)
         latest_api_date = json.loads(response.text)["date"]
         logging.debug(f"API date: {latest_api_date}")
         rates = storage.get_dict()
         latest_storage_date = rates.get("last_update")
         logging.debug(f"Storage date: {latest_storage_date}")
         if latest_storage_date:
             if (
                 latest_storage_date < latest_api_date
                 or [
                     currency
                     for currency, rates in rates["currencies"].items()
                     if rates["rates"].keys() != supported_currencies.keys()
                 ]
                 or rates["currencies"].keys() != supported_currencies.keys()
             ):
                 return "update"
             else:
                 return "ok"
         else:
             return "empty"
     else:
         return "failed"
Пример #2
0
 def convert(self, pretiffy=False):
     if self.input_currency and self.output_currency:
         if len(self.input_currency) > 1:
             return (
                 "Too many input currency options! Please insert a currency code/symbol matching the unique currency.",
                 400,
             )
         else:
             input_dict = {
                 "amount": self.amount,
                 "currency": self.input_currency[0]
             }
             storage = Storage(storage_name)
             rates = storage.get_dict()
             try:
                 rates = rates["currencies"][
                     self.input_currency[0]]["rates"]
                 logging.info(rates)
             except KeyError:
                 rates = {}
             if rates:
                 output_dict = {
                     currency:
                     round(rates[currency] *
                           self.amount, 2) if currency in rates else None
                     for currency in self.output_currency
                 }
                 return (
                     json.dumps(
                         {
                             "input": input_dict,
                             "output": output_dict
                         },
                         indent=4,
                         sort_keys=True,
                     ) if pretiffy else {
                         "input": input_dict,
                         "output": output_dict
                     },
                     200,
                 )
             else:
                 return (
                     "Missing currency exchange rates. Please try to update the storage.",
                     400,
                 )
     else:
         return (
             "Unsupported currency symbol/code! Please check inserted values with supported values help and try again!",
             400,
         )