-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_expenses_from_santander_api.py
82 lines (60 loc) · 2.42 KB
/
get_expenses_from_santander_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import abstra.workflows as aw
import os
import requests
import uuid
from datetime import datetime, timedelta
from pandas.tseries.offsets import BDay
# Employer Identification Number of the bank (e.g.: CNPJ in Brazil)
SANTANDER_BANK_ID = os.getenv("SANTANDER_BANK_ID")
# Account number which you want to get the expenses from (on the format routing_number.account_number)
SANTANDER_ACC_NUMBER = os.getenv("SANTANDER_ACC_NUMBER")
SANTANDER_API_KEY = os.getenv("SANTANDER_API_KEY")
end_date = datetime.now() - timedelta(days=1)
start_date = datetime.now() - BDay(30)
def format_response(response_data):
output = {}
output["bank"] = "Banco Santander S. A."
output["data"] = []
for expense in response_data:
output["data"].append({
"id": uuid.uuid4(),
"amount": expense["amount"],
"currency": "BRL",
"date": expense["transactionDate"],
"reason": expense["transactionName"]
})
return output
def get_expenses_from_santander_api():
base_path = "https://trust-open.api.santander.com.br/bank_account_information/v1 - Ambiente de Produção"
endpoint = base_path + f"/banks/{SANTANDER_BANK_ID}/statements/{SANTANDER_ACC_NUMBER}"
querystring = {
"initialDate": start_date.strftime('%Y-%m-%d'),
"finalDate": end_date.strftime('%Y-%m-%d'),
"_limit": "100",
"_offset": "1"
}
headers = {
"Content-Type": "application/json",
"api-key": SANTANDER_API_KEY
}
try:
response = requests.get(endpoint, headers=headers, params=querystring)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Error getting expenses: {e}")
return None
response_total_pages = int(response.json()["_pageable"]["totalPages"])
response_data = response.json()["_content"]
for page in range(2, response_total_pages + 1):
querystring["_offset"] = str(page)
try:
response = requests.get(endpoint, headers=headers, params=querystring)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Error getting expenses: {e}")
return
response_data += response.json()["_content"]
return response_data
response = get_expenses_from_santander_api()
formatted_response = format_response(response)
aw.set_data("api_output", formatted_response)