From ded27b984238bdd0505bd271793705cf77a39c4f Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 28 Feb 2022 20:02:50 +0000 Subject: [PATCH] Begin implementing Nordigen API --- handler/sinks/nordigen.py | 66 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 handler/sinks/nordigen.py diff --git a/handler/sinks/nordigen.py b/handler/sinks/nordigen.py new file mode 100644 index 0000000..6cb145b --- /dev/null +++ b/handler/sinks/nordigen.py @@ -0,0 +1,66 @@ +# Twisted/Klein imports +from twisted.logger import Logger + +# Other library imports +import requests +from json import dumps +from simplejson.errors import JSONDecodeError + +# Project imports +from settings import settings + + +class Nordigen(object): + """ + Class to manage calls to Open Banking APIs through Nordigen. + """ + + def __init__(self): + self.log = Logger("nordigen") + # self.get_access_token() + + def get_access_token(self): + """ + Get an access token. + :return: True or False + :rtype: bool + """ + headers = {"accept": "application/json", "Content-Type": "application/json"} + data = { + "secret_id": settings.Nordigen.ID, + "secret_key": settings.Nordigen.Key, + } + path = f"{settings.Nordigen.Base}/token/new/" + r = requests.post(path, headers=headers, data=dumps(data)) + try: + parsed = r.json() + except JSONDecodeError: + self.log.error("Error parsing access token response: {content}", content=r.content) + return False + if "access" in parsed: + settings.Nordigen.Access = parsed["access"] + settings.write() + + print(dumps(self.get_institutions(""), indent=2)) + + def get_institutions(self, country, filter_name=None): + """ + Get a list of supported institutions. + """ + if not len(country) == 2: + return False + headers = {"accept": "application/json", "Authorization": f"Bearer {settings.Nordigen.Access}"} + path = f"{settings.Nordigen.Base}/institutions/?country={country}" + r = requests.get(path, headers=headers) + try: + parsed = r.json() + except JSONDecodeError: + self.log.error("Error parsing institutions response: {content}", content=r.content) + return False + new_list = [] + if filter_name: + for i in parsed: + if filter_name in i["name"]: + new_list.append(i) + return new_list + return parsed