feat(trade-monitor): NAS trade-alert 클라이언트 (monitor-set/report)
This commit is contained in:
48
services/trade-monitor/nas_client.py
Normal file
48
services/trade-monitor/nas_client.py
Normal file
@@ -0,0 +1,48 @@
|
||||
"""NAS stock 백엔드 trade-alert 계약 — X-WebAI-Key + retry."""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
import httpx
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_MAX_ATTEMPTS = 3
|
||||
_RETRY_STATUSES = {429, 500, 502, 503, 504}
|
||||
|
||||
|
||||
class NASClient:
|
||||
def __init__(self, base_url: str, api_key: str, timeout: float = 10.0):
|
||||
self._base_url = base_url.rstrip("/")
|
||||
self._api_key = api_key
|
||||
self._client = httpx.AsyncClient(timeout=timeout)
|
||||
|
||||
async def close(self) -> None:
|
||||
await self._client.aclose()
|
||||
|
||||
async def get_monitor_set(self) -> dict:
|
||||
return await self._request("GET", "/api/webai/trade-alert/monitor-set")
|
||||
|
||||
async def post_report(self, as_of: str, firing: list[dict]) -> dict:
|
||||
return await self._request(
|
||||
"POST", "/api/webai/trade-alert/report",
|
||||
json={"as_of": as_of, "firing": firing})
|
||||
|
||||
async def _request(self, method: str, path: str, **kwargs) -> dict:
|
||||
url = f"{self._base_url}{path}"
|
||||
headers = {"X-WebAI-Key": self._api_key}
|
||||
for attempt in range(_MAX_ATTEMPTS):
|
||||
try:
|
||||
resp = await self._client.request(method, url, headers=headers, **kwargs)
|
||||
if resp.status_code in _RETRY_STATUSES and attempt < _MAX_ATTEMPTS - 1:
|
||||
await asyncio.sleep(2 ** attempt)
|
||||
continue
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
except httpx.TimeoutException:
|
||||
if attempt < _MAX_ATTEMPTS - 1:
|
||||
await asyncio.sleep(2 ** attempt)
|
||||
continue
|
||||
raise
|
||||
raise RuntimeError("retry exhausted")
|
||||
39
services/trade-monitor/tests/test_nas_client.py
Normal file
39
services/trade-monitor/tests/test_nas_client.py
Normal file
@@ -0,0 +1,39 @@
|
||||
"""NASClient — monitor-set/report + X-WebAI-Key (respx)."""
|
||||
import json as _json
|
||||
|
||||
import httpx
|
||||
import respx
|
||||
|
||||
from nas_client import NASClient
|
||||
|
||||
BASE = "http://nas.test"
|
||||
|
||||
|
||||
@respx.mock
|
||||
async def test_get_monitor_set_sends_key():
|
||||
route = respx.get(f"{BASE}/api/webai/trade-alert/monitor-set").mock(
|
||||
return_value=httpx.Response(200, json={"session": "regular", "buy_targets": []}))
|
||||
c = NASClient(BASE, "KEY")
|
||||
ms = await c.get_monitor_set()
|
||||
assert ms["session"] == "regular"
|
||||
assert route.calls.last.request.headers["X-WebAI-Key"] == "KEY"
|
||||
await c.close()
|
||||
|
||||
|
||||
@respx.mock
|
||||
async def test_post_report_payload():
|
||||
captured = {}
|
||||
|
||||
def _resp(request):
|
||||
captured.update(_json.loads(request.content))
|
||||
return httpx.Response(200, json={"new_alerts": 1, "cleared": 0})
|
||||
|
||||
respx.post(f"{BASE}/api/webai/trade-alert/report").mock(side_effect=_resp)
|
||||
c = NASClient(BASE, "KEY")
|
||||
firing = [{"ticker": "005930", "kind": "buy", "condition": "buy_breakout",
|
||||
"price": 71500, "detail": {}}]
|
||||
out = await c.post_report("2026-07-02T09:01:00+09:00", firing)
|
||||
assert out["new_alerts"] == 1
|
||||
assert captured["as_of"] == "2026-07-02T09:01:00+09:00"
|
||||
assert captured["firing"] == firing
|
||||
await c.close()
|
||||
Reference in New Issue
Block a user