File size: 8,662 Bytes
72fc0ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
QuantLib Pro SDK — HTTP Session Wrapper

Handles:
- httpx session with connection pooling
- Automatic retry with exponential backoff (3 attempts)
- JWT Bearer token injection
- Timeout configuration (connect: 5s, read: 30s)
- Verbose mode for debugging (masks auth headers)
- Async support via httpx.AsyncClient
"""

import logging
import time
from typing import Any

import httpx

from quantlib_api.exceptions import (
    QuantLibAPIError,
    QuantLibAuthError,
    QuantLibNetworkError,
    QuantLibNotFoundError,
    QuantLibRateLimitError,
)

logger = logging.getLogger(__name__)

# Default timeouts
CONNECT_TIMEOUT = 5.0   # seconds
READ_TIMEOUT = 30.0     # seconds
MAX_RETRIES = 3
BACKOFF_BASE = 0.5      # seconds


class HTTPSession:
    """
    Synchronous HTTP session wrapping httpx.Client with retry logic.

    Automatically injects Authorization header and handles
    4xx/5xx responses by raising typed exceptions.
    """

    def __init__(
        self,
        base_url: str,
        *,
        timeout: float = READ_TIMEOUT,
        max_retries: int = MAX_RETRIES,
        verbose: bool = False,
    ):
        self.base_url = base_url.rstrip("/")
        self.timeout = timeout
        self.max_retries = max_retries
        self.verbose = verbose
        self._token: str | None = None
        self._api_key: str | None = None
        self._client = httpx.Client(
            base_url=self.base_url,
            timeout=httpx.Timeout(connect=CONNECT_TIMEOUT, read=self.timeout, write=10.0, pool=5.0),
            follow_redirects=True,
        )

    def set_token(self, token: str):
        """Update the Bearer token used for all requests."""
        self._token = token

    def set_api_key(self, api_key: str):
        """Update the API key used for all requests."""
        self._api_key = api_key

    def _headers(self) -> dict:
        headers = {"Content-Type": "application/json", "Accept": "application/json"}
        if self._token:
            headers["Authorization"] = f"Bearer {self._token}"
        elif self._api_key:
            headers["X-API-Key"] = self._api_key
        return headers

    def _log_request(self, method: str, url: str, body: Any = None):
        if self.verbose:
            safe_headers = self._headers()
            if "Authorization" in safe_headers:
                safe_headers["Authorization"] = "Bearer ***"
            logger.debug(f"→ {method} {url}  headers={safe_headers}  body={body}")

    def _handle_response(self, response: httpx.Response) -> dict:
        status = response.status_code
        if self.verbose:
            logger.debug(f"← {status} ({len(response.content)} bytes)")

        if status == 200 or status == 201:
            try:
                return response.json()
            except Exception:
                return {"raw": response.text}

        # Error handling
        try:
            detail = response.json().get("detail", response.text)
        except Exception:
            detail = response.text

        if status in (401, 403):
            raise QuantLibAuthError(f"HTTP {status}: {detail}")
        if status == 404:
            raise QuantLibNotFoundError(detail)
        if status == 429:
            retry_after = int(response.headers.get("Retry-After", 60))
            raise QuantLibRateLimitError(retry_after)
        raise QuantLibAPIError(f"HTTP {status}: {detail}", status_code=status, response={"detail": detail})

    def _request_with_retry(self, method: str, path: str, **kwargs) -> dict:
        url = path if path.startswith("http") else self.base_url + path
        self._log_request(method, url, kwargs.get("json"))

        last_exc = None
        for attempt in range(self.max_retries):
            try:
                response = self._client.request(method, url, headers=self._headers(), **kwargs)
                return self._handle_response(response)
            except (QuantLibAuthError, QuantLibNotFoundError):
                raise  # Don't retry auth/404 errors
            except QuantLibRateLimitError as e:
                wait = e.retry_after
                logger.warning(f"Rate limited. Waiting {wait}s before retry {attempt + 1}/{self.max_retries}")
                time.sleep(min(wait, 60))
                last_exc = e
            except QuantLibAPIError as e:
                if e.status_code < 500:
                    raise  # Don't retry 4xx
                wait = BACKOFF_BASE * (2 ** attempt)
                logger.warning(f"Server error {e.status_code}. Retrying in {wait:.1f}s (attempt {attempt + 1}/{self.max_retries})")
                time.sleep(wait)
                last_exc = e
            except httpx.TimeoutException as e:
                wait = BACKOFF_BASE * (2 ** attempt)
                logger.warning(f"Timeout. Retrying in {wait:.1f}s (attempt {attempt + 1}/{self.max_retries})")
                time.sleep(wait)
                last_exc = QuantLibNetworkError(f"Request timed out after {self.timeout}s")
            except httpx.ConnectError as e:
                raise QuantLibNetworkError(f"Cannot connect to {self.base_url}. Is the server running?") from e
            except httpx.RequestError as e:
                raise QuantLibNetworkError(str(e)) from e

        raise last_exc or QuantLibNetworkError("Max retries exceeded")

    def get(self, path: str, params: dict = None) -> dict:
        return self._request_with_retry("GET", path, params=params)

    def post(self, path: str, json: dict = None) -> dict:
        return self._request_with_retry("POST", path, json=json or {})

    def put(self, path: str, json: dict = None) -> dict:
        return self._request_with_retry("PUT", path, json=json or {})

    def delete(self, path: str) -> dict:
        return self._request_with_retry("DELETE", path)

    def close(self):
        self._client.close()

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self.close()


class AsyncHTTPSession:
    """
    Async HTTP session (httpx.AsyncClient) for use with asyncio.

    Usage::

        async with AsyncHTTPSession("http://localhost:8000") as session:
            session.set_token(token)
            result = await session.get("/api/v1/portfolio/performance")
    """

    def __init__(self, base_url: str, *, timeout: float = READ_TIMEOUT, verbose: bool = False):
        self.base_url = base_url.rstrip("/")
        self.timeout = timeout
        self.verbose = verbose
        self._token: str | None = None
        self._client = httpx.AsyncClient(
            base_url=self.base_url,
            timeout=httpx.Timeout(connect=CONNECT_TIMEOUT, read=timeout, write=10.0, pool=5.0),
            follow_redirects=True,
        )

    def set_token(self, token: str):
        self._token = token

    def _headers(self) -> dict:
        h = {"Content-Type": "application/json", "Accept": "application/json"}
        if self._token:
            h["Authorization"] = f"Bearer {self._token}"
        return h

    async def _handle_response_async(self, response: httpx.Response) -> dict:
        status = response.status_code
        if status in (200, 201):
            try:
                return response.json()
            except Exception:
                return {"raw": response.text}
        try:
            detail = response.json().get("detail", response.text)
        except Exception:
            detail = response.text
        if status in (401, 403):
            raise QuantLibAuthError(f"HTTP {status}: {detail}")
        if status == 404:
            raise QuantLibNotFoundError(detail)
        if status == 429:
            raise QuantLibRateLimitError(int(response.headers.get("Retry-After", 60)))
        raise QuantLibAPIError(f"HTTP {status}: {detail}", status_code=status)

    async def get(self, path: str, params: dict = None) -> dict:
        url = self.base_url + path
        try:
            r = await self._client.get(url, headers=self._headers(), params=params)
            return await self._handle_response_async(r)
        except httpx.ConnectError as e:
            raise QuantLibNetworkError(str(e)) from e

    async def post(self, path: str, json: dict = None) -> dict:
        url = self.base_url + path
        try:
            r = await self._client.post(url, headers=self._headers(), json=json or {})
            return await self._handle_response_async(r)
        except httpx.ConnectError as e:
            raise QuantLibNetworkError(str(e)) from e

    async def close(self):
        await self._client.aclose()

    async def __aenter__(self):
        return self

    async def __aexit__(self, *args):
        await self.close()