mulasharp 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mulasharp-0.1.0/LICENSE +21 -0
- mulasharp-0.1.0/PKG-INFO +192 -0
- mulasharp-0.1.0/README.md +171 -0
- mulasharp-0.1.0/pyproject.toml +42 -0
- mulasharp-0.1.0/setup.cfg +4 -0
- mulasharp-0.1.0/src/mulasharp/__init__.py +36 -0
- mulasharp-0.1.0/src/mulasharp/client.py +164 -0
- mulasharp-0.1.0/src/mulasharp/errors.py +21 -0
- mulasharp-0.1.0/src/mulasharp/models.py +116 -0
- mulasharp-0.1.0/src/mulasharp/providers/__init__.py +41 -0
- mulasharp-0.1.0/src/mulasharp/providers/base.py +43 -0
- mulasharp-0.1.0/src/mulasharp/providers/gocardless.py +304 -0
- mulasharp-0.1.0/src/mulasharp/providers/mock.py +126 -0
- mulasharp-0.1.0/src/mulasharp/providers/plaid.py +229 -0
- mulasharp-0.1.0/src/mulasharp/providers/tink.py +205 -0
- mulasharp-0.1.0/src/mulasharp/providers/truelayer.py +225 -0
- mulasharp-0.1.0/src/mulasharp/py.typed +0 -0
- mulasharp-0.1.0/src/mulasharp/store.py +307 -0
- mulasharp-0.1.0/src/mulasharp/utils.py +56 -0
- mulasharp-0.1.0/src/mulasharp.egg-info/PKG-INFO +192 -0
- mulasharp-0.1.0/src/mulasharp.egg-info/SOURCES.txt +25 -0
- mulasharp-0.1.0/src/mulasharp.egg-info/dependency_links.txt +1 -0
- mulasharp-0.1.0/src/mulasharp.egg-info/requires.txt +3 -0
- mulasharp-0.1.0/src/mulasharp.egg-info/top_level.txt +1 -0
- mulasharp-0.1.0/tests/test_gocardless.py +127 -0
- mulasharp-0.1.0/tests/test_mulasharp.py +150 -0
- mulasharp-0.1.0/tests/test_providers.py +140 -0
mulasharp-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 REDX Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
mulasharp-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mulasharp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Open banking sync SDK for Python: connections, normalized transactions, dedupe, and incremental sync
|
|
5
|
+
Author: REDX Contributors
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: open-banking,bank,transactions,sync,fintech,plaid,gocardless
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
13
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
14
|
+
Classifier: Typing :: Typed
|
|
15
|
+
Requires-Python: >=3.9
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Provides-Extra: dev
|
|
19
|
+
Requires-Dist: pytest>=7.4; extra == "dev"
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
|
|
22
|
+
# Mulasharp
|
|
23
|
+
|
|
24
|
+
If you’ve ever tried to pull bank transactions into an app, you already know the annoying part isn’t “call the API”.
|
|
25
|
+
|
|
26
|
+
It’s everything after that: bank consent flows, expired connections, weird provider payloads, duplicate transactions showing up again, and figuring out what actually changed since the last sync.
|
|
27
|
+
|
|
28
|
+
Mulasharp is a small Python SDK that handles that messy middle bit.
|
|
29
|
+
|
|
30
|
+
You plug in a provider adapter (GoCardless, Plaid, TrueLayer, Tink, or your own), and Mulasharp gives you clean account/transaction objects, local storage, dedupe, and incremental sync.
|
|
31
|
+
|
|
32
|
+
## What it’s good for
|
|
33
|
+
|
|
34
|
+
- **Keeping track of bank connections** — know what’s connected, what’s pending, and when a user needs to reconnect.
|
|
35
|
+
- **Getting clean data** — accounts and transactions come back as normal Python objects, not provider-shaped blobs.
|
|
36
|
+
- **Avoiding duplicates** — transactions are saved with a stable ID (or a fingerprint when the provider doesn’t give one).
|
|
37
|
+
- **Syncing without redoing everything** — cursors are stored per account, so the next sync can pick up where it left off.
|
|
38
|
+
- **Starting simple** — there’s a small SQLite store built in, which is perfect for scripts and small apps.
|
|
39
|
+
|
|
40
|
+
## Install (local/dev)
|
|
41
|
+
|
|
42
|
+
`ash
|
|
43
|
+
pip install -e .
|
|
44
|
+
`
|
|
45
|
+
|
|
46
|
+
## Quick start (real provider: GoCardless)
|
|
47
|
+
|
|
48
|
+
`python
|
|
49
|
+
from mulasharp import MulaSharpClient, GoCardlessProvider, SyncStore
|
|
50
|
+
|
|
51
|
+
provider = GoCardlessProvider(
|
|
52
|
+
secret_id="YOUR_GOCARDLESS_SECRET_ID",
|
|
53
|
+
secret_key="YOUR_GOCARDLESS_SECRET_KEY",
|
|
54
|
+
)
|
|
55
|
+
store = SyncStore("mulasharp.db")
|
|
56
|
+
client = MulaSharpClient(provider, store)
|
|
57
|
+
|
|
58
|
+
# 1) Start the bank-link flow (the user has to complete this)
|
|
59
|
+
conn = client.connect(
|
|
60
|
+
user_id="user_123",
|
|
61
|
+
institution_id="REVOLUT_REVOGB21", # pick from GoCardless institutions
|
|
62
|
+
redirect="https://yourapp.com/callback",
|
|
63
|
+
)
|
|
64
|
+
print(conn.provider_data["auth_link"]) # send the user here
|
|
65
|
+
|
|
66
|
+
# 2) After the user finishes bank auth, refresh and sync
|
|
67
|
+
conn = client.refresh_connection(conn.id)
|
|
68
|
+
accounts = client.sync_accounts(conn.id)
|
|
69
|
+
result = client.sync_transactions(conn.id)
|
|
70
|
+
|
|
71
|
+
for tx in client.list_transactions(accounts[0].id):
|
|
72
|
+
print(tx.date, tx.amount, tx.currency, tx.description)
|
|
73
|
+
`
|
|
74
|
+
|
|
75
|
+
## Quick start (mock provider)
|
|
76
|
+
|
|
77
|
+
`python
|
|
78
|
+
from mulasharp import MulaSharpClient, MockProvider, SyncStore
|
|
79
|
+
|
|
80
|
+
provider = MockProvider()
|
|
81
|
+
store = SyncStore("mulasharp.db")
|
|
82
|
+
client = MulaSharpClient(provider, store)
|
|
83
|
+
|
|
84
|
+
# 1) Create a connection (a real provider would start an auth/consent flow here)
|
|
85
|
+
conn = client.connect(user_id="user_123")
|
|
86
|
+
|
|
87
|
+
# 2) Add some fake account + transaction data
|
|
88
|
+
acct = provider.add_account(conn.id, name="Main checking", currency="USD")
|
|
89
|
+
provider.add_transaction(acct.id, id="tx_1", amount="-24.90", currency="USD", date="2026-04-01", description="Coffee shop")
|
|
90
|
+
provider.add_transaction(acct.id, id="tx_2", amount="1500.00", currency="USD", date="2026-04-02", description="Salary")
|
|
91
|
+
|
|
92
|
+
# 3) Sync (deduped, cursor stored)
|
|
93
|
+
result = client.sync_transactions(conn.id)
|
|
94
|
+
print(result.to_dict())
|
|
95
|
+
# {'connection_id': ..., 'accounts': 1, 'transactions_seen': 2, 'inserted': 2, 'updated': 0, ...}
|
|
96
|
+
|
|
97
|
+
# 4) Read normalized transactions from the local store
|
|
98
|
+
for tx in client.list_transactions(acct.id):
|
|
99
|
+
print(tx.date, tx.amount, tx.currency, tx.description)
|
|
100
|
+
`
|
|
101
|
+
|
|
102
|
+
Run sync again later ? it won't create duplicates (and you can use sync_all(user_id=...) to sync every active connection):
|
|
103
|
+
|
|
104
|
+
`python
|
|
105
|
+
result = client.sync_transactions(conn.id)
|
|
106
|
+
# inserted: 0, updated: 2 (same transactions seen again)
|
|
107
|
+
`
|
|
108
|
+
|
|
109
|
+
## The main pieces
|
|
110
|
+
|
|
111
|
+
- MulaSharpClient ? the main API (connect, sync_accounts, sync_transactions, sync_all, list_transactions)
|
|
112
|
+
- Provider ? the adapter interface for real aggregators
|
|
113
|
+
- SyncStore ? SQLite storage for connections, accounts, transactions, and cursors
|
|
114
|
+
- Models: Connection, Account, Transaction
|
|
115
|
+
|
|
116
|
+
## Supported providers
|
|
117
|
+
|
|
118
|
+
- **GoCardless Bank Account Data** (GoCardlessProvider) ? consent/requisition link, accounts, booked/pending transactions. Alias: NordigenProvider /
|
|
119
|
+
ordigen (GoCardless acquired Nordigen).
|
|
120
|
+
- **Plaid** (PlaidProvider) ? Link token, public token exchange, accounts, transactions sync
|
|
121
|
+
- **TrueLayer** (TrueLayerProvider) ? auth link, code exchange, accounts, transactions
|
|
122
|
+
- **Tink** (TinkProvider) ? Tink Link URL, code exchange, accounts, transactions
|
|
123
|
+
- **MockProvider** ? handy for tests and local development
|
|
124
|
+
|
|
125
|
+
`python
|
|
126
|
+
from mulasharp import available_providers, get_provider
|
|
127
|
+
|
|
128
|
+
print(available_providers())
|
|
129
|
+
# ['gocardless', 'mock', 'nordigen', 'plaid', 'tink', 'truelayer']
|
|
130
|
+
|
|
131
|
+
provider = get_provider("plaid", client_id="...", secret="...", environment="sandbox")
|
|
132
|
+
`
|
|
133
|
+
|
|
134
|
+
The same sync/dedupe/store workflow works across providers. You'll still need real credentials and the provider's normal user-consent flow.
|
|
135
|
+
|
|
136
|
+
### Adding another provider
|
|
137
|
+
|
|
138
|
+
There are plenty of other aggregators out there (Yapily, Salt Edge, Finicity, MX, Belvo, etc.). Instead of pretending to cover every API perfectly, Mulasharp keeps it simple: implement the Provider interface and map the provider's data to Account/Transaction.
|
|
139
|
+
|
|
140
|
+
`python
|
|
141
|
+
from mulasharp import Provider
|
|
142
|
+
|
|
143
|
+
class MyProvider(Provider):
|
|
144
|
+
name = "myprovider"
|
|
145
|
+
# implement create_connection/get_connection/list_accounts/list_transactions
|
|
146
|
+
`
|
|
147
|
+
|
|
148
|
+
## Writing a real provider
|
|
149
|
+
|
|
150
|
+
Implement the Provider interface and map the provider's payloads to Mulasharp models.
|
|
151
|
+
|
|
152
|
+
`python
|
|
153
|
+
from mulasharp import Provider, Connection, Account, Transaction
|
|
154
|
+
|
|
155
|
+
class MyAggregatorProvider(Provider):
|
|
156
|
+
name = "myagg"
|
|
157
|
+
|
|
158
|
+
def create_connection(self, user_id: str, **kwargs):
|
|
159
|
+
# call aggregator API, return Connection (status may be "pending")
|
|
160
|
+
...
|
|
161
|
+
|
|
162
|
+
def get_connection(self, connection_id: str):
|
|
163
|
+
...
|
|
164
|
+
|
|
165
|
+
def list_accounts(self, connection):
|
|
166
|
+
...
|
|
167
|
+
|
|
168
|
+
def list_transactions(self, connection, account, *, date_from=None, date_to=None, cursor=None):
|
|
169
|
+
# return (list[Transaction], next_cursor)
|
|
170
|
+
...
|
|
171
|
+
`
|
|
172
|
+
|
|
173
|
+
A few practical notes for real providers:
|
|
174
|
+
- **Amount sign:** Mulasharp uses positive = money in,
|
|
175
|
+
egative = money out.
|
|
176
|
+
- **IDs:** use the provider's transaction ID as uid when it exists. If it doesn't, use a fingerprint.
|
|
177
|
+
- **Consent expiry:** expose expires_at and status so your app knows when to ask the user to reconnect.
|
|
178
|
+
- **Raw payloads:** keep the original provider payload in
|
|
179
|
+
aw so debugging doesn't turn into guesswork.
|
|
180
|
+
|
|
181
|
+
## A few honest notes
|
|
182
|
+
|
|
183
|
+
This is an MVP for the sync workflow, not a magic “connect to every bank on earth” button.
|
|
184
|
+
|
|
185
|
+
Provider APIs change, and what works depends on country, bank, and permissions. The built-in adapters cover the common auth + accounts + transactions flow, but you should still test with your own credentials and map any extra fields your app cares about.
|
|
186
|
+
|
|
187
|
+
## Development
|
|
188
|
+
|
|
189
|
+
`ash
|
|
190
|
+
pip install -e .[dev]
|
|
191
|
+
pytest
|
|
192
|
+
`
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# Mulasharp
|
|
2
|
+
|
|
3
|
+
If you’ve ever tried to pull bank transactions into an app, you already know the annoying part isn’t “call the API”.
|
|
4
|
+
|
|
5
|
+
It’s everything after that: bank consent flows, expired connections, weird provider payloads, duplicate transactions showing up again, and figuring out what actually changed since the last sync.
|
|
6
|
+
|
|
7
|
+
Mulasharp is a small Python SDK that handles that messy middle bit.
|
|
8
|
+
|
|
9
|
+
You plug in a provider adapter (GoCardless, Plaid, TrueLayer, Tink, or your own), and Mulasharp gives you clean account/transaction objects, local storage, dedupe, and incremental sync.
|
|
10
|
+
|
|
11
|
+
## What it’s good for
|
|
12
|
+
|
|
13
|
+
- **Keeping track of bank connections** — know what’s connected, what’s pending, and when a user needs to reconnect.
|
|
14
|
+
- **Getting clean data** — accounts and transactions come back as normal Python objects, not provider-shaped blobs.
|
|
15
|
+
- **Avoiding duplicates** — transactions are saved with a stable ID (or a fingerprint when the provider doesn’t give one).
|
|
16
|
+
- **Syncing without redoing everything** — cursors are stored per account, so the next sync can pick up where it left off.
|
|
17
|
+
- **Starting simple** — there’s a small SQLite store built in, which is perfect for scripts and small apps.
|
|
18
|
+
|
|
19
|
+
## Install (local/dev)
|
|
20
|
+
|
|
21
|
+
`ash
|
|
22
|
+
pip install -e .
|
|
23
|
+
`
|
|
24
|
+
|
|
25
|
+
## Quick start (real provider: GoCardless)
|
|
26
|
+
|
|
27
|
+
`python
|
|
28
|
+
from mulasharp import MulaSharpClient, GoCardlessProvider, SyncStore
|
|
29
|
+
|
|
30
|
+
provider = GoCardlessProvider(
|
|
31
|
+
secret_id="YOUR_GOCARDLESS_SECRET_ID",
|
|
32
|
+
secret_key="YOUR_GOCARDLESS_SECRET_KEY",
|
|
33
|
+
)
|
|
34
|
+
store = SyncStore("mulasharp.db")
|
|
35
|
+
client = MulaSharpClient(provider, store)
|
|
36
|
+
|
|
37
|
+
# 1) Start the bank-link flow (the user has to complete this)
|
|
38
|
+
conn = client.connect(
|
|
39
|
+
user_id="user_123",
|
|
40
|
+
institution_id="REVOLUT_REVOGB21", # pick from GoCardless institutions
|
|
41
|
+
redirect="https://yourapp.com/callback",
|
|
42
|
+
)
|
|
43
|
+
print(conn.provider_data["auth_link"]) # send the user here
|
|
44
|
+
|
|
45
|
+
# 2) After the user finishes bank auth, refresh and sync
|
|
46
|
+
conn = client.refresh_connection(conn.id)
|
|
47
|
+
accounts = client.sync_accounts(conn.id)
|
|
48
|
+
result = client.sync_transactions(conn.id)
|
|
49
|
+
|
|
50
|
+
for tx in client.list_transactions(accounts[0].id):
|
|
51
|
+
print(tx.date, tx.amount, tx.currency, tx.description)
|
|
52
|
+
`
|
|
53
|
+
|
|
54
|
+
## Quick start (mock provider)
|
|
55
|
+
|
|
56
|
+
`python
|
|
57
|
+
from mulasharp import MulaSharpClient, MockProvider, SyncStore
|
|
58
|
+
|
|
59
|
+
provider = MockProvider()
|
|
60
|
+
store = SyncStore("mulasharp.db")
|
|
61
|
+
client = MulaSharpClient(provider, store)
|
|
62
|
+
|
|
63
|
+
# 1) Create a connection (a real provider would start an auth/consent flow here)
|
|
64
|
+
conn = client.connect(user_id="user_123")
|
|
65
|
+
|
|
66
|
+
# 2) Add some fake account + transaction data
|
|
67
|
+
acct = provider.add_account(conn.id, name="Main checking", currency="USD")
|
|
68
|
+
provider.add_transaction(acct.id, id="tx_1", amount="-24.90", currency="USD", date="2026-04-01", description="Coffee shop")
|
|
69
|
+
provider.add_transaction(acct.id, id="tx_2", amount="1500.00", currency="USD", date="2026-04-02", description="Salary")
|
|
70
|
+
|
|
71
|
+
# 3) Sync (deduped, cursor stored)
|
|
72
|
+
result = client.sync_transactions(conn.id)
|
|
73
|
+
print(result.to_dict())
|
|
74
|
+
# {'connection_id': ..., 'accounts': 1, 'transactions_seen': 2, 'inserted': 2, 'updated': 0, ...}
|
|
75
|
+
|
|
76
|
+
# 4) Read normalized transactions from the local store
|
|
77
|
+
for tx in client.list_transactions(acct.id):
|
|
78
|
+
print(tx.date, tx.amount, tx.currency, tx.description)
|
|
79
|
+
`
|
|
80
|
+
|
|
81
|
+
Run sync again later ? it won't create duplicates (and you can use sync_all(user_id=...) to sync every active connection):
|
|
82
|
+
|
|
83
|
+
`python
|
|
84
|
+
result = client.sync_transactions(conn.id)
|
|
85
|
+
# inserted: 0, updated: 2 (same transactions seen again)
|
|
86
|
+
`
|
|
87
|
+
|
|
88
|
+
## The main pieces
|
|
89
|
+
|
|
90
|
+
- MulaSharpClient ? the main API (connect, sync_accounts, sync_transactions, sync_all, list_transactions)
|
|
91
|
+
- Provider ? the adapter interface for real aggregators
|
|
92
|
+
- SyncStore ? SQLite storage for connections, accounts, transactions, and cursors
|
|
93
|
+
- Models: Connection, Account, Transaction
|
|
94
|
+
|
|
95
|
+
## Supported providers
|
|
96
|
+
|
|
97
|
+
- **GoCardless Bank Account Data** (GoCardlessProvider) ? consent/requisition link, accounts, booked/pending transactions. Alias: NordigenProvider /
|
|
98
|
+
ordigen (GoCardless acquired Nordigen).
|
|
99
|
+
- **Plaid** (PlaidProvider) ? Link token, public token exchange, accounts, transactions sync
|
|
100
|
+
- **TrueLayer** (TrueLayerProvider) ? auth link, code exchange, accounts, transactions
|
|
101
|
+
- **Tink** (TinkProvider) ? Tink Link URL, code exchange, accounts, transactions
|
|
102
|
+
- **MockProvider** ? handy for tests and local development
|
|
103
|
+
|
|
104
|
+
`python
|
|
105
|
+
from mulasharp import available_providers, get_provider
|
|
106
|
+
|
|
107
|
+
print(available_providers())
|
|
108
|
+
# ['gocardless', 'mock', 'nordigen', 'plaid', 'tink', 'truelayer']
|
|
109
|
+
|
|
110
|
+
provider = get_provider("plaid", client_id="...", secret="...", environment="sandbox")
|
|
111
|
+
`
|
|
112
|
+
|
|
113
|
+
The same sync/dedupe/store workflow works across providers. You'll still need real credentials and the provider's normal user-consent flow.
|
|
114
|
+
|
|
115
|
+
### Adding another provider
|
|
116
|
+
|
|
117
|
+
There are plenty of other aggregators out there (Yapily, Salt Edge, Finicity, MX, Belvo, etc.). Instead of pretending to cover every API perfectly, Mulasharp keeps it simple: implement the Provider interface and map the provider's data to Account/Transaction.
|
|
118
|
+
|
|
119
|
+
`python
|
|
120
|
+
from mulasharp import Provider
|
|
121
|
+
|
|
122
|
+
class MyProvider(Provider):
|
|
123
|
+
name = "myprovider"
|
|
124
|
+
# implement create_connection/get_connection/list_accounts/list_transactions
|
|
125
|
+
`
|
|
126
|
+
|
|
127
|
+
## Writing a real provider
|
|
128
|
+
|
|
129
|
+
Implement the Provider interface and map the provider's payloads to Mulasharp models.
|
|
130
|
+
|
|
131
|
+
`python
|
|
132
|
+
from mulasharp import Provider, Connection, Account, Transaction
|
|
133
|
+
|
|
134
|
+
class MyAggregatorProvider(Provider):
|
|
135
|
+
name = "myagg"
|
|
136
|
+
|
|
137
|
+
def create_connection(self, user_id: str, **kwargs):
|
|
138
|
+
# call aggregator API, return Connection (status may be "pending")
|
|
139
|
+
...
|
|
140
|
+
|
|
141
|
+
def get_connection(self, connection_id: str):
|
|
142
|
+
...
|
|
143
|
+
|
|
144
|
+
def list_accounts(self, connection):
|
|
145
|
+
...
|
|
146
|
+
|
|
147
|
+
def list_transactions(self, connection, account, *, date_from=None, date_to=None, cursor=None):
|
|
148
|
+
# return (list[Transaction], next_cursor)
|
|
149
|
+
...
|
|
150
|
+
`
|
|
151
|
+
|
|
152
|
+
A few practical notes for real providers:
|
|
153
|
+
- **Amount sign:** Mulasharp uses positive = money in,
|
|
154
|
+
egative = money out.
|
|
155
|
+
- **IDs:** use the provider's transaction ID as uid when it exists. If it doesn't, use a fingerprint.
|
|
156
|
+
- **Consent expiry:** expose expires_at and status so your app knows when to ask the user to reconnect.
|
|
157
|
+
- **Raw payloads:** keep the original provider payload in
|
|
158
|
+
aw so debugging doesn't turn into guesswork.
|
|
159
|
+
|
|
160
|
+
## A few honest notes
|
|
161
|
+
|
|
162
|
+
This is an MVP for the sync workflow, not a magic “connect to every bank on earth” button.
|
|
163
|
+
|
|
164
|
+
Provider APIs change, and what works depends on country, bank, and permissions. The built-in adapters cover the common auth + accounts + transactions flow, but you should still test with your own credentials and map any extra fields your app cares about.
|
|
165
|
+
|
|
166
|
+
## Development
|
|
167
|
+
|
|
168
|
+
`ash
|
|
169
|
+
pip install -e .[dev]
|
|
170
|
+
pytest
|
|
171
|
+
`
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "mulasharp"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Open banking sync SDK for Python: connections, normalized transactions, dedupe, and incremental sync"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "REDX Contributors" }]
|
|
13
|
+
dependencies = []
|
|
14
|
+
keywords = ["open-banking", "bank", "transactions", "sync", "fintech", "plaid", "gocardless"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
21
|
+
"Topic :: Office/Business :: Financial",
|
|
22
|
+
"Typing :: Typed",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.optional-dependencies]
|
|
26
|
+
dev = ["pytest>=7.4"]
|
|
27
|
+
|
|
28
|
+
[tool.setuptools]
|
|
29
|
+
package-dir = {"" = "src"}
|
|
30
|
+
include-package-data = true
|
|
31
|
+
|
|
32
|
+
[tool.setuptools.packages.find]
|
|
33
|
+
where = ["src"]
|
|
34
|
+
include = ["mulasharp*"]
|
|
35
|
+
|
|
36
|
+
[tool.setuptools.package-data]
|
|
37
|
+
mulasharp = ["py.typed"]
|
|
38
|
+
|
|
39
|
+
[tool.pytest.ini_options]
|
|
40
|
+
testpaths = ["tests"]
|
|
41
|
+
pythonpath = ["src"]
|
|
42
|
+
addopts = "-ra"
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
from .client import MulaSharpClient, SyncResult
|
|
2
|
+
from .errors import MulaSharpError, ConnectionExpired, NotFound, ProviderError, ValidationError
|
|
3
|
+
from .models import Account, Connection, Transaction
|
|
4
|
+
from .providers.base import Provider
|
|
5
|
+
from .providers.gocardless import GoCardlessProvider, NordigenProvider
|
|
6
|
+
from .providers.mock import MockProvider
|
|
7
|
+
from .providers.plaid import PlaidProvider
|
|
8
|
+
from .providers.tink import TinkProvider
|
|
9
|
+
from .providers.truelayer import TrueLayerProvider
|
|
10
|
+
from .providers import available_providers, get_provider
|
|
11
|
+
from .store import SyncStore
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"MulaSharpClient",
|
|
15
|
+
"SyncResult",
|
|
16
|
+
"MulaSharpError",
|
|
17
|
+
"ProviderError",
|
|
18
|
+
"ConnectionExpired",
|
|
19
|
+
"NotFound",
|
|
20
|
+
"ValidationError",
|
|
21
|
+
"Connection",
|
|
22
|
+
"Account",
|
|
23
|
+
"Transaction",
|
|
24
|
+
"Provider",
|
|
25
|
+
"MockProvider",
|
|
26
|
+
"GoCardlessProvider",
|
|
27
|
+
"NordigenProvider",
|
|
28
|
+
"PlaidProvider",
|
|
29
|
+
"TrueLayerProvider",
|
|
30
|
+
"TinkProvider",
|
|
31
|
+
"available_providers",
|
|
32
|
+
"get_provider",
|
|
33
|
+
"SyncStore",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from datetime import datetime, timezone
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from .models import Account, Connection, Transaction
|
|
8
|
+
from .providers.base import Provider
|
|
9
|
+
from .store import SyncStore
|
|
10
|
+
from .errors import NotFound
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass
|
|
14
|
+
class SyncResult:
|
|
15
|
+
connection_id: str
|
|
16
|
+
accounts: int
|
|
17
|
+
transactions_seen: int
|
|
18
|
+
inserted: int
|
|
19
|
+
updated: int
|
|
20
|
+
cursor: str | None = None
|
|
21
|
+
|
|
22
|
+
def to_dict(self) -> dict[str, Any]:
|
|
23
|
+
return {
|
|
24
|
+
"connection_id": self.connection_id,
|
|
25
|
+
"accounts": self.accounts,
|
|
26
|
+
"transactions_seen": self.transactions_seen,
|
|
27
|
+
"inserted": self.inserted,
|
|
28
|
+
"updated": self.updated,
|
|
29
|
+
"cursor": self.cursor,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class MulaSharpClient:
|
|
34
|
+
"""High-level client for connecting banks and syncing transactions."""
|
|
35
|
+
|
|
36
|
+
def __init__(self, provider: Provider, store: SyncStore):
|
|
37
|
+
self.provider = provider
|
|
38
|
+
self.store = store
|
|
39
|
+
|
|
40
|
+
def connect(self, user_id: str, **kwargs: Any) -> Connection:
|
|
41
|
+
"""Create a connection via the provider and store it."""
|
|
42
|
+
conn = self.provider.create_connection(user_id, **kwargs)
|
|
43
|
+
self.store.save_connection(conn)
|
|
44
|
+
return conn
|
|
45
|
+
|
|
46
|
+
def refresh_connection(self, connection_id: str) -> Connection:
|
|
47
|
+
conn = self.provider.get_connection(connection_id)
|
|
48
|
+
self.store.save_connection(conn)
|
|
49
|
+
return conn
|
|
50
|
+
|
|
51
|
+
def list_connections(self, *, user_id: str | None = None) -> list[Connection]:
|
|
52
|
+
return self.store.list_connections(user_id=user_id)
|
|
53
|
+
|
|
54
|
+
def sync_accounts(self, connection_id: str) -> list[Account]:
|
|
55
|
+
conn = self._require_connection(connection_id)
|
|
56
|
+
accounts = self.provider.list_accounts(conn)
|
|
57
|
+
for account in accounts:
|
|
58
|
+
self.store.upsert_account(account)
|
|
59
|
+
return accounts
|
|
60
|
+
|
|
61
|
+
def sync_transactions(
|
|
62
|
+
self,
|
|
63
|
+
connection_id: str,
|
|
64
|
+
*,
|
|
65
|
+
account_id: str | None = None,
|
|
66
|
+
date_from: str | None = None,
|
|
67
|
+
date_to: str | None = None,
|
|
68
|
+
full_refresh: bool = False,
|
|
69
|
+
) -> SyncResult:
|
|
70
|
+
"""Sync transactions for a connection (or one account).
|
|
71
|
+
|
|
72
|
+
Uses a stored cursor per account unless full_refresh=True.
|
|
73
|
+
Deduplication is handled by the store using Transaction.uid.
|
|
74
|
+
"""
|
|
75
|
+
conn = self._require_connection(connection_id)
|
|
76
|
+
accounts = self.store.list_accounts(connection_id)
|
|
77
|
+
if not accounts:
|
|
78
|
+
accounts = self.sync_accounts(connection_id)
|
|
79
|
+
if account_id:
|
|
80
|
+
accounts = [a for a in accounts if a.id == account_id]
|
|
81
|
+
|
|
82
|
+
total_seen = 0
|
|
83
|
+
total_inserted = 0
|
|
84
|
+
total_updated = 0
|
|
85
|
+
last_cursor: str | None = None
|
|
86
|
+
|
|
87
|
+
for account in accounts:
|
|
88
|
+
cursor_key = f"txn_cursor:{self.provider.name}:{account.id}"
|
|
89
|
+
cursor = None if full_refresh else self.store.get_cursor(cursor_key)
|
|
90
|
+
while True:
|
|
91
|
+
batch, next_cursor = self.provider.list_transactions(
|
|
92
|
+
conn,
|
|
93
|
+
account,
|
|
94
|
+
date_from=date_from,
|
|
95
|
+
date_to=date_to,
|
|
96
|
+
cursor=cursor,
|
|
97
|
+
)
|
|
98
|
+
if batch:
|
|
99
|
+
counts = self.store.upsert_transactions(batch)
|
|
100
|
+
total_seen += counts["total"]
|
|
101
|
+
total_inserted += counts["inserted"]
|
|
102
|
+
total_updated += counts["updated"]
|
|
103
|
+
if next_cursor:
|
|
104
|
+
self.store.set_cursor(cursor_key, next_cursor)
|
|
105
|
+
last_cursor = next_cursor
|
|
106
|
+
cursor = next_cursor
|
|
107
|
+
continue
|
|
108
|
+
# no next cursor: save a "synced" marker when we got a cursor-based page
|
|
109
|
+
if cursor is None:
|
|
110
|
+
self.store.set_cursor(cursor_key, "synced:" + datetime.now(timezone.utc).isoformat())
|
|
111
|
+
break
|
|
112
|
+
|
|
113
|
+
return SyncResult(
|
|
114
|
+
connection_id=connection_id,
|
|
115
|
+
accounts=len(accounts),
|
|
116
|
+
transactions_seen=total_seen,
|
|
117
|
+
inserted=total_inserted,
|
|
118
|
+
updated=total_updated,
|
|
119
|
+
cursor=last_cursor,
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
def list_accounts(self, connection_id: str) -> list[Account]:
|
|
123
|
+
return self.store.list_accounts(connection_id)
|
|
124
|
+
|
|
125
|
+
def list_transactions(self, account_id: str, **kwargs: Any) -> list[Transaction]:
|
|
126
|
+
return self.store.list_transactions(account_id, **kwargs)
|
|
127
|
+
|
|
128
|
+
def sync_all(self, *, user_id: str | None = None, **kwargs: Any) -> list[SyncResult]:
|
|
129
|
+
"""Sync transactions for all stored connections (optionally for one user)."""
|
|
130
|
+
results: list[SyncResult] = []
|
|
131
|
+
for conn in self.store.list_connections(user_id=user_id):
|
|
132
|
+
if conn.needs_reconnect:
|
|
133
|
+
continue
|
|
134
|
+
results.append(self.sync_transactions(conn.id, **kwargs))
|
|
135
|
+
return results
|
|
136
|
+
|
|
137
|
+
def delete_connection(self, connection_id: str) -> bool:
|
|
138
|
+
return self.store.delete_connection(connection_id)
|
|
139
|
+
|
|
140
|
+
def _require_connection(self, connection_id: str) -> Connection:
|
|
141
|
+
conn = self.store.get_connection(connection_id)
|
|
142
|
+
if conn is None:
|
|
143
|
+
# try provider (useful when provider owns the record) and persist
|
|
144
|
+
try:
|
|
145
|
+
conn = self.provider.get_connection(connection_id)
|
|
146
|
+
except Exception as e:
|
|
147
|
+
raise NotFound(f"Connection not found: {connection_id}") from e
|
|
148
|
+
self.store.save_connection(conn)
|
|
149
|
+
# expire local status if past expires_at
|
|
150
|
+
if conn.expires_at and conn.status == "active":
|
|
151
|
+
now = datetime.now(timezone.utc)
|
|
152
|
+
exp = conn.expires_at if conn.expires_at.tzinfo else conn.expires_at.replace(tzinfo=timezone.utc)
|
|
153
|
+
if exp <= now:
|
|
154
|
+
conn = Connection(
|
|
155
|
+
id=conn.id,
|
|
156
|
+
provider=conn.provider,
|
|
157
|
+
user_id=conn.user_id,
|
|
158
|
+
status="expired",
|
|
159
|
+
created_at=conn.created_at,
|
|
160
|
+
expires_at=conn.expires_at,
|
|
161
|
+
provider_data=conn.provider_data,
|
|
162
|
+
)
|
|
163
|
+
self.store.save_connection(conn)
|
|
164
|
+
return conn
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""Errors for mulasharp."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class MulaSharpError(Exception):
|
|
5
|
+
"""Base error."""
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ProviderError(MulaSharpError):
|
|
9
|
+
"""Raised when a provider API call fails."""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ConnectionExpired(MulaSharpError):
|
|
13
|
+
"""Raised when a connection/consent is expired and needs user action."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class NotFound(MulaSharpError):
|
|
17
|
+
"""Raised when a requested resource is not found."""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class ValidationError(MulaSharpError):
|
|
21
|
+
"""Raised when input data is invalid."""
|