获取汇率api使用

1.汇率url

https://www.exchangerate-api.com/docs/supported-currencies

2.调用

class Command(BaseCommand):
    help = "Sync currency pair and exchange rate"

    def add_arguments(self, parser):
        parser.add_argument("--base_currency", type=str, default=["SGD", "HKD", "IDR", "PHP"])

    def handle(self, *args, **options):
        try:
            base_currencys = options["base_currency"]
            time.sleep(2)
            for base_currency in base_currencys:

                url = f"https://open.er-api.com/v6/latest/{base_currency}"

                response = requests.get(url=url)
                if response.ok:
                    result = response.json()
                    data = result["rates"]

                    cache.set(f"{base_currency}_exchange_rates", data)
                    self.stdout.write(self.style.SUCCESS("Successfully sync currency pair and exchange rate"))
                else:
                    self.stdout.write(self.style.SUCCESS("Failed sync currency pair and exchange rate"))


        except Exception as e:
            capture_exception(e)

 

posted @ 2023-03-26 19:36  Οo白麒麟оΟ  阅读(306)  评论(0编辑  收藏  举报