# 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