-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
241 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import yaml | ||
from datetime import date | ||
from os import path | ||
import requests | ||
|
||
from beancount.ingest import importer | ||
from beancount.core import data | ||
from beancount.core import amount | ||
from beancount.core.number import D | ||
|
||
|
||
class HttpServiceException(Exception): | ||
pass | ||
|
||
|
||
class Importer(importer.ImporterProtocol): | ||
"""An importer for Nordigen API (e.g. for Revolut).""" | ||
|
||
def identify(self, file): | ||
return 'nordigen.yaml' == path.basename(file.name) | ||
|
||
def file_account(self, file): | ||
return '' | ||
|
||
def extract(self, file, existing_entries): | ||
with open(file.name, 'r') as f: | ||
config = yaml.safe_load(f) | ||
token = config['token'] | ||
headers = {'Authorization': 'Token ' + token} | ||
|
||
entries = [] | ||
for account in config['accounts']: | ||
accountId = account['id'] | ||
assetAccount = account['asset_account'] | ||
r = requests.get(f'https://ob.nordigen.com/api/accounts/{accountId}/transactions/', headers=headers) | ||
try: | ||
r.raise_for_status() | ||
except requests.exceptions.HTTPError as e: | ||
raise HttpServiceException(e, e.response.text) | ||
|
||
transactions = sorted(r.json()['transactions']["booked"], key=lambda trx: trx['bookingDate']) | ||
for trx in transactions: | ||
metakv = { | ||
'nordref': trx['transactionId'], | ||
} | ||
if 'currencyExchange' in trx: | ||
instructedAmount = trx['currencyExchange']['instructedAmount'] | ||
metakv['original'] = instructedAmount['currency'] + ' ' + instructedAmount['amount'] | ||
meta = data.new_metadata('', 0, metakv) | ||
trxDate = date.fromisoformat(trx['bookingDate']) | ||
entry = data.Transaction( | ||
meta, | ||
trxDate, | ||
'*', | ||
'', | ||
' '.join(trx['remittanceInformationUnstructuredArray']), | ||
data.EMPTY_SET, | ||
data.EMPTY_SET, | ||
[ | ||
data.Posting(assetAccount, amount.Amount(D(str(trx['transactionAmount']['amount'])), trx['transactionAmount']['currency']), None, None, None, None), | ||
] | ||
) | ||
entries.append(entry) | ||
|
||
return entries |
136 changes: 136 additions & 0 deletions
136
src/tariochbctools/importers/nordigen/nordigen_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import argparse | ||
import requests | ||
import sys | ||
|
||
|
||
def build_header(token): | ||
return {'Authorization': 'Token ' + token} | ||
|
||
|
||
def check_result(result): | ||
try: | ||
result.raise_for_status() | ||
except requests.exceptions.HTTPError as e: | ||
raise Exception(e, e.response.text) | ||
|
||
|
||
def list_bank(token, country): | ||
r = requests.get('https://ob.nordigen.com/api/aspsps/', params={'country': country}, headers=build_header(token)) | ||
check_result(r) | ||
|
||
for asp in r.json(): | ||
print(asp['name'] + ': ' + asp['id']) | ||
|
||
|
||
def create_link(token, userId, bank): | ||
if not bank: | ||
raise Exception('Please specify --bank it is required for create_link') | ||
headers = build_header(token) | ||
requisitionId = _find_requisition_id(token, userId) | ||
if not requisitionId: | ||
r = requests.post('https://ob.nordigen.com/api/requisitions/', data={ | ||
'redirect': 'http://localhost', | ||
'enduser_id': userId, | ||
'reference': userId, | ||
}, headers=build_header(token)) | ||
check_result(r) | ||
requisitionId = r.json()['id'] | ||
|
||
r = requests.post(f'https://ob.nordigen.com/api/requisitions/{requisitionId}/links/', data={'aspsp_id': bank}, headers=headers) | ||
check_result(r) | ||
link = r.json()['initiate'] | ||
print(f'Go to {link} for connecting to your bank.') | ||
|
||
|
||
def list_accounts(token): | ||
headers = build_header(token) | ||
r = requests.get('https://ob.nordigen.com/api/requisitions/', headers=headers) | ||
check_result(r) | ||
for req in r.json()['results']: | ||
print(req['enduser_id'] + ': ' + req['id']) | ||
for account in req['accounts']: | ||
ra = requests.get(f'https://ob.nordigen.com/api/accounts/{account}', headers=headers) | ||
check_result(ra) | ||
acc = ra.json() | ||
asp = acc['aspsp_identifier'] | ||
iban = acc['iban'] | ||
|
||
ra = requests.get(f'https://ob.nordigen.com/api/accounts/{account}/details', headers=headers) | ||
check_result(ra) | ||
accDetails = ra.json()['account'] | ||
|
||
currency = accDetails['currency'] | ||
owner = accDetails['ownerName'] if 'ownerName' in accDetails else '-' | ||
print(f'{account}: {asp} {owner} {iban} {currency}') | ||
|
||
|
||
def delete_user(token, userId): | ||
requisitionId = _find_requisition_id(token, userId) | ||
if requisitionId: | ||
r = requests.delete(f'https://ob.nordigen.com/api/requisitions/{requisitionId}', headers=build_header(token)) | ||
check_result(r) | ||
|
||
|
||
def _find_requisition_id(token, userId): | ||
headers = build_header(token) | ||
r = requests.get('https://ob.nordigen.com/api/requisitions/', headers=headers) | ||
check_result(r) | ||
for req in r.json()['results']: | ||
if req['enduser_id'] == userId: | ||
return req['id'] | ||
|
||
return None | ||
|
||
|
||
def parse_args(args): | ||
parser = argparse.ArgumentParser( | ||
description="nordigen-config" | ||
) | ||
parser.add_argument( | ||
'--token', | ||
required=True, | ||
help='API Token, can be generated on Nordigen website', | ||
) | ||
parser.add_argument( | ||
'--country', | ||
default='GB', | ||
help='Country Code for list_bank', | ||
) | ||
parser.add_argument( | ||
'--userId', | ||
default='beancount', | ||
help='UserId for create_link and delete_user', | ||
) | ||
parser.add_argument( | ||
'--bank', | ||
help='Bank to connect to, see list_banks', | ||
) | ||
parser.add_argument( | ||
'mode', | ||
choices=[ | ||
'list_banks', | ||
'create_link', | ||
'list_accounts', | ||
'delete_user', | ||
], | ||
) | ||
return parser.parse_args(args) | ||
|
||
|
||
def main(args): | ||
args = parse_args(args) | ||
|
||
if args.mode == 'list_banks': | ||
list_bank(args.token, args.country) | ||
elif args.mode == 'create_link': | ||
create_link(args.token, args.userId, args.bank) | ||
elif args.mode == 'list_accounts': | ||
list_accounts(args.token) | ||
elif args.mode == 'delete_user': | ||
delete_user(args.token, args.userId) | ||
|
||
|
||
def run(): | ||
"""Entry point for console_scripts | ||
""" | ||
main(sys.argv[1:]) |