-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython_client.py
More file actions
426 lines (317 loc) · 11.9 KB
/
Copy pathpython_client.py
File metadata and controls
426 lines (317 loc) · 11.9 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
"""
IntegrityX Python Client Library
A complete Python client for the IntegrityX API with authentication,
error handling, and retry logic.
Usage:
from integrityx_client import IntegrityXClient
client = IntegrityXClient('http://localhost:8000', 'your_jwt_token')
result = client.upload_document({'loan_id': 'LOAN-123', 'amount': 250000})
print(f"Sealed with ETID: {result['etid']}")
"""
import requests
from typing import Dict, List, Optional, Any
import time
from datetime import datetime
class IntegrityXError(Exception):
"""Base exception for IntegrityX client errors."""
pass
class AuthenticationError(IntegrityXError):
"""Raised when authentication fails."""
pass
class ValidationError(IntegrityXError):
"""Raised when request validation fails."""
pass
class IntegrityXClient:
"""
Python client for IntegrityX API.
Args:
base_url: Base URL of the IntegrityX API (e.g., 'http://localhost:8000')
token: Clerk JWT token for authentication
timeout: Request timeout in seconds (default: 30)
max_retries: Maximum number of retries for failed requests (default: 3)
Example:
>>> client = IntegrityXClient('http://localhost:8000', 'your_jwt_token')
>>> result = client.upload_document({'loan_id': 'LOAN-123'})
>>> print(result['etid'])
"""
def __init__(
self,
base_url: str,
token: str,
timeout: int = 30,
max_retries: int = 3
):
self.base_url = base_url.rstrip('/')
self.token = token
self.timeout = timeout
self.max_retries = max_retries
self.session = requests.Session()
self.session.headers.update({
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
})
def _request(
self,
method: str,
endpoint: str,
json: Optional[Dict] = None,
params: Optional[Dict] = None,
requires_auth: bool = True
) -> Dict:
"""Make HTTP request with retry logic."""
url = f"{self.base_url}{endpoint}"
headers = self.session.headers if requires_auth else {'Content-Type': 'application/json'}
for attempt in range(self.max_retries):
try:
response = requests.request(
method=method,
url=url,
json=json,
params=params,
headers=headers,
timeout=self.timeout
)
if response.status_code == 401:
raise AuthenticationError("Invalid or expired token")
if response.status_code == 400:
error_data = response.json()
raise ValidationError(f"Validation error: {error_data.get('detail', 'Unknown error')}")
response.raise_for_status()
return response.json()
except requests.exceptions.Timeout:
if attempt == self.max_retries - 1:
raise IntegrityXError(f"Request timeout after {self.max_retries} attempts")
time.sleep(2 ** attempt) # Exponential backoff
except requests.exceptions.RequestException as e:
if attempt == self.max_retries - 1:
raise IntegrityXError(f"Request failed: {str(e)}")
time.sleep(2 ** attempt)
# ===========================
# Document Operations
# ===========================
def upload_document(self, document: Dict, metadata: Optional[Dict] = None) -> Dict:
"""
Upload a single JSON document.
Args:
document: Document data as dictionary
metadata: Optional metadata
Returns:
Response with ETID, hash, and blockchain transaction ID
Example:
>>> result = client.upload_document({
... 'loan_id': 'LOAN-12345',
... 'amount': 250000,
... 'borrower_name': 'John Doe'
... })
>>> print(f"ETID: {result['etid']}")
"""
payload = {'document': document}
if metadata:
payload['metadata'] = metadata
return self._request('POST', '/ingest-json', json=payload)
def get_document(self, etid: str) -> Dict:
"""
Retrieve a document by ETID.
Args:
etid: Document ETID
Returns:
Document data with metadata
"""
return self._request('GET', f'/document/{etid}')
def delete_document(self, etid: str) -> Dict:
"""
Delete a document (admin only).
Args:
etid: Document ETID
Returns:
Deletion confirmation
"""
return self._request('DELETE', f'/document/{etid}')
# ===========================
# Verification
# ===========================
def verify_document(self, etid: str) -> Dict:
"""
Verify a document (no auth required).
Args:
etid: Document ETID
Returns:
Verification result with blockchain proof
Example:
>>> result = client.verify_document('ETID-20241028123456-ABC123')
>>> print(f"Verified: {result['verified']}")
"""
return self._request('GET', f'/public/verify/{etid}', requires_auth=False)
def batch_verify(self, etids: List[str]) -> Dict:
"""
Verify multiple documents at once.
Args:
etids: List of document ETIDs
Returns:
Verification results for all documents
"""
return self._request('POST', '/verify/batch', json={'etids': etids})
# ===========================
# Attestations
# ===========================
def create_attestation(
self,
etid: str,
role: str,
status: str,
comments: Optional[str] = None,
attested_by: Optional[str] = None
) -> Dict:
"""
Create an attestation for a document.
Args:
etid: Document ETID
role: Attestor role (e.g., 'underwriter', 'compliance_officer')
status: Attestation status (e.g., 'approved', 'rejected')
comments: Optional comments
attested_by: Email of attestor
Returns:
Attestation confirmation
"""
payload = {
'etid': etid,
'role': role,
'status': status
}
if comments:
payload['comments'] = comments
if attested_by:
payload['attested_by'] = attested_by
return self._request('POST', '/attestations', json=payload)
def get_attestations(self, etid: str) -> Dict:
"""
Get all attestations for a document.
Args:
etid: Document ETID
Returns:
List of attestations
"""
return self._request('GET', f'/attestations/{etid}')
# ===========================
# Provenance
# ===========================
def get_provenance(self, etid: str) -> Dict:
"""
Get complete provenance chain for a document.
Args:
etid: Document ETID
Returns:
Complete chain of custody
"""
return self._request('GET', f'/provenance/{etid}')
# ===========================
# Analytics
# ===========================
def get_stats(self) -> Dict:
"""
Get document statistics.
Returns:
Statistics and insights
"""
return self._request('GET', '/analytics/stats')
def predict(self, metric: str, timeframe: str) -> Dict:
"""
Get predictive analytics.
Args:
metric: Metric to predict (e.g., 'document_volume')
timeframe: Timeframe (e.g., 'next_30_days')
Returns:
Prediction results
"""
payload = {'metric': metric, 'timeframe': timeframe}
return self._request('POST', '/analytics/predictive', json=payload)
# ===========================
# AI Features
# ===========================
def detect_anomalies(self, etid: str) -> Dict:
"""
Detect anomalies in a document using AI.
Args:
etid: Document ETID
Returns:
Anomaly detection results
"""
return self._request('POST', '/ai/detect-anomalies', json={'etid': etid})
def analyze_document(self, etid: str) -> Dict:
"""
Analyze document using NLP and entity extraction.
Args:
etid: Document ETID
Returns:
Document intelligence results
"""
return self._request('POST', '/intelligence/analyze', json={'etid': etid})
# ===========================
# Usage Examples
# ===========================
def example_upload_and_verify():
"""Example: Upload and verify a document."""
client = IntegrityXClient('http://localhost:8000', 'your_jwt_token')
# Upload document
document = {
'loan_id': 'LOAN-12345',
'borrower_name': 'John Doe',
'amount': 250000,
'interest_rate': 4.5,
'term_months': 360
}
result = client.upload_document(document)
print(f"✅ Document sealed with ETID: {result['etid']}")
print(f" Blockchain TX: {result['walacor_txid']}")
# Verify document (no auth needed)
verification = client.verify_document(result['etid'])
print(f"✅ Document verified: {verification['verified']}")
print(f" Blockchain verified: {verification['blockchain_verified']}")
def example_attestations():
"""Example: Create and retrieve attestations."""
client = IntegrityXClient('http://localhost:8000', 'your_jwt_token')
etid = 'ETID-20241028123456-ABC123'
# Create attestation
attestation = client.create_attestation(
etid=etid,
role='underwriter',
status='approved',
comments='Loan application approved after thorough review',
attested_by='john.doe@company.com'
)
print(f"✅ Attestation created: {attestation['attestation_id']}")
# Get all attestations
attestations = client.get_attestations(etid)
print(f"✅ Found {len(attestations['attestations'])} attestations")
def example_analytics():
"""Example: Get analytics and predictions."""
client = IntegrityXClient('http://localhost:8000', 'your_jwt_token')
# Get statistics
stats = client.get_stats()
print(f"📊 Total documents: {stats['total_documents']}")
print(f" Sealed today: {stats['sealed_today']}")
print(f" Average health score: {stats['average_health_score']}")
# Get predictions
prediction = client.predict('document_volume', 'next_30_days')
print(f"📈 Predicted volume trend: {prediction['forecast'][0]['predicted_value']}")
def example_error_handling():
"""Example: Proper error handling."""
client = IntegrityXClient('http://localhost:8000', 'your_jwt_token')
try:
result = client.upload_document({'loan_id': 'LOAN-123'})
print(f"✅ Success: {result['etid']}")
except AuthenticationError as e:
print(f"❌ Authentication error: {e}")
# Handle token refresh
except ValidationError as e:
print(f"❌ Validation error: {e}")
# Handle invalid data
except IntegrityXError as e:
print(f"❌ API error: {e}")
# Handle other errors
if __name__ == '__main__':
# Run examples (update token first!)
example_upload_and_verify()
example_attestations()
example_analytics()
example_error_handling()