forked from ritik4ever/stellar-portfolio-rebalancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_api_client.py
More file actions
407 lines (317 loc) · 14.2 KB
/
Copy pathpython_api_client.py
File metadata and controls
407 lines (317 loc) · 14.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
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
#!/usr/bin/env python3
"""
Stellar Portfolio Rebalancer - Python API Client.
A comprehensive Python client for interacting with the Stellar Portfolio
Rebalancer API. Covers portfolio management, prices, rebalancing, history,
auto-rebalancer status, notifications, and system health.
Setup:
python3 -m venv venv && source venv/bin/activate
pip install requests
Usage:
python python_api_client.py --help
python python_api_client.py prices
python python_api_client.py create-portfolio --address GABC... --assets XLM:40,USDC:60
python python_api_client.py rebalance --portfolio-id abc-123
python python_api_client.py history
Environment variables:
API_URL - Base API URL (default: http://localhost:3001/api/v1)
API_KEY - Optional Bearer token for authenticated requests
"""
import os
import sys
import json
import argparse
from typing import Dict, Any, Optional, List
class RebalancerClient:
"""
HTTP client for the Stellar Portfolio Rebalancer REST API.
Args:
base_url: Root URL of the API. Defaults to http://localhost:3001/api/v1.
api_key: Optional Bearer token attached to every request.
"""
def __init__(
self,
base_url: str = "http://localhost:3001/api/v1",
api_key: Optional[str] = None,
):
import requests
self.base_url = base_url.rstrip("/")
self.session = requests.Session()
self.session.headers.update({"Accept": "application/json"})
if api_key:
self.session.headers.update({"Authorization": f"Bearer {api_key}"})
def _get(self, path: str, params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
import requests
response = self.session.get(f"{self.base_url}{path}", params=params)
response.raise_for_status()
return response.json()
def _post(
self, path: str, payload: Optional[Dict[str, Any]] = None
) -> Dict[str, Any]:
import requests
response = self.session.post(
f"{self.base_url}{path}", json=payload or {}
)
response.raise_for_status()
return response.json()
# --- Health & System ---------------------------------------------------
def health(self) -> Dict[str, Any]:
"""Check whether the API server is reachable and healthy."""
return self._get("/health")
def system_status(self) -> Dict[str, Any]:
"""Fetch detailed system status including dependencies."""
return self._get("/system/status")
def readiness(self) -> Dict[str, Any]:
"""Fetch readiness report (database, queue, workers, etc.)."""
return self._get("/system/readiness")
# --- Prices & Markets --------------------------------------------------
def get_prices(self) -> Dict[str, Any]:
"""Fetch current price data for all supported assets."""
return self._get("/prices")
def get_enhanced_prices(self) -> Dict[str, Any]:
"""Fetch enhanced price data with risk metadata."""
return self._get("/prices/enhanced")
def get_market_details(self, asset: str) -> Dict[str, Any]:
"""Fetch market details (depth, volume, spread) for an asset."""
return self._get(f"/market/{asset}/details")
def get_price_chart(self, asset: str) -> Dict[str, Any]:
"""Fetch historical price chart data for an asset."""
return self._get(f"/market/{asset}/chart")
# --- Portfolios --------------------------------------------------------
def create_portfolio(
self, address: str, allocations: List[Dict[str, Any]], name: Optional[str] = None
) -> Dict[str, Any]:
"""
Create a new portfolio.
Args:
address: Stellar public key of the portfolio owner.
allocations: List of dicts, each with ``asset`` and ``percentage`` keys.
name: Optional display name for the portfolio.
"""
payload: Dict[str, Any] = {
"ownerAddress": address,
"allocations": allocations,
}
if name:
payload["name"] = name
return self._post("/portfolio", payload)
def get_portfolio(self, portfolio_id: str) -> Dict[str, Any]:
"""Fetch a single portfolio by ID."""
return self._get(f"/portfolio/{portfolio_id}")
def update_portfolio(
self, portfolio_id: str, allocations: List[Dict[str, Any]], version: int
) -> Dict[str, Any]:
"""
Update an existing portfolio with optimistic concurrency.
Args:
portfolio_id: ID of the portfolio to update.
allocations: New allocation list.
version: Current version (etag) of the portfolio for conflict detection.
"""
payload = {"allocations": allocations, "version": version}
return self.session.put(
f"{self.base_url}/portfolio/{portfolio_id}",
json=payload,
).json()
def list_user_portfolios(self, address: str) -> Dict[str, Any]:
"""List all portfolios owned by a Stellar address."""
return self._get(f"/user/{address}/portfolios")
def search_portfolios(
self, params: Optional[Dict[str, Any]] = None
) -> Dict[str, Any]:
"""Search across all portfolios with optional filters."""
return self._get("/portfolios", params=params)
def get_rebalance_plan(self, portfolio_id: str) -> Dict[str, Any]:
"""Fetch the current drift analysis and planned trades."""
return self._get(f"/portfolio/{portfolio_id}/rebalance-plan")
def get_rebalance_estimate(self, portfolio_id: str) -> Dict[str, Any]:
"""Fetch estimated gas fees and trade count for a rebalance."""
return self._get(f"/portfolio/{portfolio_id}/rebalance-estimate")
def execute_rebalance(self, portfolio_id: str) -> Dict[str, Any]:
"""Trigger a manual on-chain rebalance for a portfolio."""
return self._post(f"/portfolio/{portfolio_id}/rebalance")
def get_rebalance_summary(self, portfolio_id: str) -> Dict[str, Any]:
"""Fetch the rebalance readiness summary for a portfolio."""
return self._get(f"/rebalance/summary/{portfolio_id}")
def export_portfolio(
self, portfolio_id: str, format: str = "json"
) -> Dict[str, Any]:
"""Export portfolio data in json, csv, or pdf format."""
return self._get(f"/portfolio/{portfolio_id}/export", params={"format": format})
def share_portfolio(self, portfolio_id: str) -> Dict[str, Any]:
"""Create a public share link for a portfolio."""
return self._post(f"/portfolio/{portfolio_id}/share")
def revoke_share(self, portfolio_id: str) -> Dict[str, Any]:
"""Revoke a portfolio's public share link."""
return self.session.delete(
f"{self.base_url}/portfolio/{portfolio_id}/share"
).json()
# --- Rebalance History -------------------------------------------------
def get_rebalance_history(
self, params: Optional[Dict[str, Any]] = None
) -> Dict[str, Any]:
"""
Fetch rebalance history with optional filters.
Args:
params: Optional dict with keys like ``portfolioId``, ``limit``, ``offset``.
"""
return self._get("/rebalance/history", params=params)
# --- Auto-Rebalancer ---------------------------------------------------
def auto_rebalancer_status(self) -> Dict[str, Any]:
"""Check whether the auto-rebalancer is running."""
return self._get("/auto-rebalancer/status")
# --- Assets Registry ---------------------------------------------------
def get_assets(self) -> Dict[str, Any]:
"""Fetch the full asset registry (supported assets)."""
return self._get("/assets")
# --- Strategies --------------------------------------------------------
def get_strategies(self) -> Dict[str, Any]:
"""Fetch available rebalancing strategies."""
return self._get("/strategies")
# --- Risk --------------------------------------------------------------
def get_risk_metrics(self, portfolio_id: str) -> Dict[str, Any]:
"""Fetch risk metrics (VaR, volatility, etc.) for a portfolio."""
return self._get(f"/risk/metrics/{portfolio_id}")
def check_risk(self, portfolio_id: str) -> Dict[str, Any]:
"""Run a risk check (circuit breakers, volatility) on a portfolio."""
return self._get(f"/risk/check/{portfolio_id}")
# --- Notifications -----------------------------------------------------
def subscribe_notifications(
self, user_address: str, email: str
) -> Dict[str, Any]:
"""
Subscribe to rebalance and circuit-breaker notifications.
Args:
user_address: Stellar public key.
email: Email address for delivery.
"""
payload = {
"userId": user_address,
"email": email,
"events": ["rebalance", "circuit_breaker"],
}
return self._post("/notifications/subscribe", payload)
def get_notification_preferences(self, user_address: str) -> Dict[str, Any]:
"""Fetch notification preferences for a user."""
return self._get("/notifications/preferences", params={"userId": user_address})
def _format_json(data: Any) -> str:
return json.dumps(data, indent=2, sort_keys=True)
def _require_client(base_url: str, api_key: Optional[str]) -> RebalancerClient:
import requests
try:
return RebalancerClient(base_url=base_url, api_key=api_key)
except Exception as exc:
print(f"Failed to create API client: {exc}", file=sys.stderr)
sys.exit(1)
def cmd_prices(args: argparse.Namespace) -> None:
client = _require_client(args.api_url, args.api_key)
try:
prices = client.get_prices()
print(_format_json(prices))
except Exception as exc:
print(f"Failed to fetch prices: {exc}", file=sys.stderr)
sys.exit(1)
def cmd_create_portfolio(args: argparse.Namespace) -> None:
client = _require_client(args.api_url, args.api_key)
allocations = []
for item in args.assets.split(","):
asset, pct = item.strip().split(":")
allocations.append({"asset": asset.strip(), "percentage": float(pct)})
try:
result = client.create_portfolio(args.address, allocations, name=args.name)
print(_format_json(result))
except Exception as exc:
print(f"Failed to create portfolio: {exc}", file=sys.stderr)
sys.exit(1)
def cmd_rebalance(args: argparse.Namespace) -> None:
client = _require_client(args.api_url, args.api_key)
try:
result = client.execute_rebalance(args.portfolio_id)
print(_format_json(result))
except Exception as exc:
print(f"Failed to execute rebalance: {exc}", file=sys.stderr)
sys.exit(1)
def cmd_history(args: argparse.Namespace) -> None:
client = _require_client(args.api_url, args.api_key)
params: Dict[str, Any] = {}
if args.portfolio_id:
params["portfolioId"] = args.portfolio_id
if args.limit:
params["limit"] = args.limit
try:
result = client.get_rebalance_history(params=params or None)
print(_format_json(result))
except Exception as exc:
print(f"Failed to fetch history: {exc}", file=sys.stderr)
sys.exit(1)
def cmd_portfolio(args: argparse.Namespace) -> None:
client = _require_client(args.api_url, args.api_key)
try:
result = client.get_portfolio(args.portfolio_id)
print(_format_json(result))
except Exception as exc:
print(f"Failed to fetch portfolio: {exc}", file=sys.stderr)
sys.exit(1)
def cmd_health(args: argparse.Namespace) -> None:
client = _require_client(args.api_url, args.api_key)
try:
result = client.health()
print(_format_json(result))
except Exception as exc:
print(f"Health check failed: {exc}", file=sys.stderr)
sys.exit(1)
def cmd_plan(args: argparse.Namespace) -> None:
client = _require_client(args.api_url, args.api_key)
try:
result = client.get_rebalance_plan(args.portfolio_id)
print(_format_json(result))
except Exception as exc:
print(f"Failed to fetch rebalance plan: {exc}", file=sys.stderr)
sys.exit(1)
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="python_api_client.py",
description="Stellar Portfolio Rebalancer API client.",
)
parser.add_argument(
"--api-url",
default=os.getenv("API_URL", "http://localhost:3001/api/v1"),
help="Base URL of the API (env: API_URL)",
)
parser.add_argument(
"--api-key",
default=os.getenv("API_KEY"),
help="Bearer token for authenticated requests (env: API_KEY)",
)
sub = parser.add_subparsers(dest="command", required=True)
p = sub.add_parser("health", help="Check API health")
p.set_defaults(func=cmd_health)
p = sub.add_parser("prices", help="Fetch current prices")
p.set_defaults(func=cmd_prices)
p = sub.add_parser("create-portfolio", help="Create a new portfolio")
p.add_argument("--address", required=True, help="Stellar public key")
p.add_argument(
"--assets", required=True, help="Allocations as ASSET:PCT, comma-separated (e.g. XLM:40,USDC:60)"
)
p.add_argument("--name", help="Optional portfolio name")
p.set_defaults(func=cmd_create_portfolio)
p = sub.add_parser("rebalance", help="Execute a rebalance")
p.add_argument("--portfolio-id", required=True, help="Portfolio ID")
p.set_defaults(func=cmd_rebalance)
p = sub.add_parser("history", help="Fetch rebalance history")
p.add_argument("--portfolio-id", help="Filter by portfolio ID")
p.add_argument("--limit", type=int, help="Maximum entries to return")
p.set_defaults(func=cmd_history)
p = sub.add_parser("portfolio", help="Get a portfolio by ID")
p.add_argument("--portfolio-id", required=True, help="Portfolio ID")
p.set_defaults(func=cmd_portfolio)
p = sub.add_parser("plan", help="Get rebalance plan for a portfolio")
p.add_argument("--portfolio-id", required=True, help="Portfolio ID")
p.set_defaults(func=cmd_plan)
return parser
def main() -> None:
parser = build_parser()
args = parser.parse_args()
args.func(args)
if __name__ == "__main__":
main()