From 8eece59c47c0e4b378380fea5eebc10f1b62d450 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 28 Feb 2022 20:00:37 +0000 Subject: [PATCH] Begin implementing Yapily API --- handler/sinks/yapily.py | 53 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 handler/sinks/yapily.py diff --git a/handler/sinks/yapily.py b/handler/sinks/yapily.py new file mode 100644 index 0000000..48971e4 --- /dev/null +++ b/handler/sinks/yapily.py @@ -0,0 +1,53 @@ +# Twisted/Klein imports +from twisted.logger import Logger + +# Other library imports +import requests +from requests.auth import HTTPBasicAuth +from json import dumps +from simplejson.errors import JSONDecodeError + +# Project imports +from settings import settings + + +class Yapily(object): + """ + Class to manage calls to Open Banking APIs through Yapily. + """ + + def __init__(self): + self.log = Logger("yapily") + self.auth = HTTPBasicAuth(settings.Yapily.ID, settings.Yapily.Key) + print(self.get_institutions()) + authorisation = self.get_authorisation("monzo_ob", settings.Yapily.CallbackURL) + print("authirisation", authorisation) + + def get_institutions(self, filter_name=None): + """ + Get a list of supported institutions. + """ + + path = f"{settings.Yapily.Base}/institutions" + r = requests.get(path, auth=self.auth) + try: + parsed = r.json() + except JSONDecodeError: + self.log.error("Error parsing institutions response: {content}", content=r.content) + return False + return parsed + + def get_authorisation(self, institution_id, callback_url): + """ + Get an authorisation URL for linking a bank account of an institution. + """ + headers = {"Content-Type": "application/json"} + data = {"applicationUserId": "account-data-and-transactions-tutorial", "institutionId": institution_id, "callback": callback_url} + path = f"{settings.Yapily.Base}/account-auth-requests" + r = requests.post(path, headers=headers, data=dumps(data), auth=self.auth) + try: + parsed = r.json() + except JSONDecodeError: + self.log.error("Error parsing institutions response: {content}", content=r.content) + return False + return parsed