Check before setting up Revolut callback URL

This commit is contained in:
Mark Veidemanis 2021-12-24 17:36:10 +00:00
parent 480e1298cd
commit 18aecc216a
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 13 additions and 1 deletions

View File

@ -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