From 18aecc216a6b89afca672b855776739d88048177 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Fri, 24 Dec 2021 17:36:10 +0000 Subject: [PATCH] Check before setting up Revolut callback URL --- handler/revolut.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/handler/revolut.py b/handler/revolut.py index 240f900..d57ebc9 100644 --- a/handler/revolut.py +++ b/handler/revolut.py @@ -99,13 +99,25 @@ class Revolut(object): return False def setup_webhook(self): + webhooks = self.get_webhooks() + if "url" in webhooks.keys(): + if webhooks["url"] == settings.Revolut.WebhookURL: + self.log.info("Webhook exists - skipping setup: {url}", url=webhooks["url"]) + return True # Webhook already exists self.log.info("Setting up webhook: {url}", url=settings.Revolut.WebhookURL) headers = {"Authorization": f"Bearer {self.token}"} data = {"url": settings.Revolut.WebhookURL} r = requests.post(f"{settings.Revolut.Base}/webhook", data=dumps(data), headers=headers) if r.status_code == 204: self.log.info("Set up webhook: {url}", url=settings.Revolut.WebhookURL) - return dumps({"success": True}) + return True else: parsed = r.json() self.log.info("Failed setting up webhook: {parsed}", parsed=parsed) + + def get_webhooks(self): + headers = {"Authorization": f"Bearer {self.token}"} + r = requests.get(f"{settings.Revolut.Base}/webhook", headers=headers) + if r.status_code == 200: + parsed = r.json() + return parsed