Begin implementing Yapily API

This commit is contained in:
Mark Veidemanis 2022-02-28 20:00:37 +00:00
parent fde0936faf
commit 8eece59c47
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 53 additions and 0 deletions

53
handler/sinks/yapily.py Normal file
View File

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