-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
228 lines (184 loc) · 7.2 KB
/
Copy pathclient.py
File metadata and controls
228 lines (184 loc) · 7.2 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""Easy Enrichment API client for Python.
Enrich bank transaction descriptions with merchant name, logo, category,
subscription detection, and more. A powerful alternative to Plaid Enrich,
Ntropy, and Mastercard Ethoca.
Usage:
from easyenrichment import EasyEnrichment
client = EasyEnrichment("your-api-key")
result = client.enrich("AMZN Mktp US*RT5KN1Y24")
print(result["data"]["merchant_name"]) # "Amazon"
"""
from typing import Any, Dict, List, Optional
import requests
from .exceptions import EasyEnrichmentError
__version__ = "1.0.0"
class EasyEnrichment:
"""Client for the Easy Enrichment Transaction Enrichment API.
Args:
api_key: Your Easy Enrichment API key. Get one at https://easyenrichment.com
base_url: API base URL. Defaults to https://api.easyenrichment.com
timeout: Request timeout in seconds. Defaults to 30.
Example:
>>> client = EasyEnrichment("your-api-key")
>>> result = client.enrich("NETFLIX.COM")
>>> print(result["data"]["merchant_name"])
'Netflix'
"""
def __init__(
self,
api_key: str,
base_url: str = "https://api.easyenrichment.com",
timeout: int = 30,
):
if not api_key:
raise EasyEnrichmentError(
"API key is required. Get one at https://easyenrichment.com",
code="MISSING_API_KEY",
)
self.api_key = api_key
self.base_url = base_url.rstrip("/")
self.timeout = timeout
self._session = requests.Session()
self._session.headers.update(
{
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
"User-Agent": f"easyenrichment-python/{__version__}",
}
)
def _request(
self,
method: str,
path: str,
json: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
"""Make an HTTP request to the Easy Enrichment API.
Args:
method: HTTP method (GET, POST, etc.).
path: API endpoint path (e.g., '/enrich').
json: Optional JSON body for POST requests.
Returns:
Parsed JSON response as a dictionary.
Raises:
EasyEnrichmentError: If the API returns an error or the request fails.
"""
url = f"{self.base_url}{path}"
try:
response = self._session.request(
method=method,
url=url,
json=json,
timeout=self.timeout,
)
except requests.exceptions.Timeout:
raise EasyEnrichmentError(
f"Request timed out after {self.timeout}s",
code="TIMEOUT",
)
except requests.exceptions.ConnectionError:
raise EasyEnrichmentError(
"Failed to connect to Easy Enrichment API. Check your network connection.",
code="CONNECTION_ERROR",
)
except requests.exceptions.RequestException as e:
raise EasyEnrichmentError(
f"Request failed: {str(e)}",
code="REQUEST_ERROR",
)
# Try to parse JSON response
try:
data = response.json()
except ValueError:
raise EasyEnrichmentError(
f"Invalid JSON response from API (HTTP {response.status_code})",
code="INVALID_RESPONSE",
status=response.status_code,
)
# Handle error responses
if not response.ok:
error_message = data.get("error", data.get("message", "Unknown API error"))
error_code = data.get("code", "API_ERROR")
raise EasyEnrichmentError(
message=error_message,
code=error_code,
status=response.status_code,
)
return data
def enrich(self, description: str) -> Dict[str, Any]:
"""Enrich a single bank transaction description.
Takes a raw transaction description (e.g., 'AMZN Mktp US*RT5KN1Y24')
and returns enriched data including merchant name, logo, category,
subscription status, and more.
Args:
description: The raw bank transaction description to enrich.
Returns:
Dictionary containing the enriched transaction data.
See EnrichResponse type for full structure.
Raises:
EasyEnrichmentError: If the API returns an error.
Example:
>>> client = EasyEnrichment("your-api-key")
>>> result = client.enrich("APPLE.COM/BILL")
>>> print(result["data"]["merchant_name"])
'Apple'
>>> print(result["data"]["is_subscription"])
True
"""
return self._request("POST", "/enrich", json={"description": description})
def enrich_batch(self, transactions: List[str]) -> Dict[str, Any]:
"""Enrich multiple bank transaction descriptions in a single request.
More efficient than calling enrich() multiple times. Supports up to
100 transactions per batch request.
Args:
transactions: List of raw transaction description strings.
Returns:
Dictionary containing a list of enriched transaction results.
See BatchResponse type for full structure.
Raises:
EasyEnrichmentError: If the API returns an error.
Example:
>>> client = EasyEnrichment("your-api-key")
>>> result = client.enrich_batch([
... "NETFLIX.COM",
... "UBER *EATS",
... "AMZN Mktp US*RT5KN1Y24",
... ])
>>> for item in result["data"]:
... print(item["merchant_name"])
'Netflix'
'Uber Eats'
'Amazon'
"""
return self._request(
"POST", "/enrich/batch", json={"transactions": transactions}
)
def usage(self) -> Dict[str, Any]:
"""Get current API usage statistics.
Returns the number of API requests used and remaining in the
current billing period.
Returns:
Dictionary containing usage information.
See UsageResponse type for full structure.
Raises:
EasyEnrichmentError: If the API returns an error.
Example:
>>> client = EasyEnrichment("your-api-key")
>>> usage = client.usage()
>>> print(f"Used: {usage['requests_used']} / {usage['requests_limit']}")
"""
return self._request("GET", "/usage")
def balance(self) -> Dict[str, Any]:
"""Get current account balance.
Returns the current balance and currency for your
Easy Enrichment account.
Returns:
Dictionary containing balance information.
See BalanceResponse type for full structure.
Raises:
EasyEnrichmentError: If the API returns an error.
Example:
>>> client = EasyEnrichment("your-api-key")
>>> balance = client.balance()
>>> print(f"Balance: ${balance['balance']:.2f} {balance['currency']}")
"""
return self._request("GET", "/balance")