hyb
2025-12-31 6cdcd01f77e11b72c323603e27ebdb85b15223c9
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
from __future__ import annotations
 
import base64
import gzip
import json
import ssl
import zlib
from dataclasses import dataclass
from typing import Any, Dict, Mapping, Optional, Tuple, Union
from urllib.error import HTTPError, URLError
from urllib.parse import urlencode, urljoin
from urllib.request import Request, urlopen
 
__all__ = ["HttpClient", "HttpResponse", "HttpError", "DEFAULT_TIMEOUT"]
 
from redis.backoff import ExponentialWithJitterBackoff
from redis.retry import Retry
from redis.utils import dummy_fail
 
DEFAULT_USER_AGENT = "HttpClient/1.0 (+https://example.invalid)"
DEFAULT_TIMEOUT = 30.0
RETRY_STATUS_CODES = {429, 500, 502, 503, 504}
 
 
@dataclass
class HttpResponse:
    status: int
    headers: Dict[str, str]
    url: str
    content: bytes
 
    def text(self, encoding: Optional[str] = None) -> str:
        enc = encoding or self._get_encoding()
        return self.content.decode(enc, errors="replace")
 
    def json(self) -> Any:
        return json.loads(self.text(encoding=self._get_encoding()))
 
    def _get_encoding(self) -> str:
        # Try to infer encoding from headers; default to utf-8
        ctype = self.headers.get("content-type", "")
        # Example: application/json; charset=utf-8
        for part in ctype.split(";"):
            p = part.strip()
            if p.lower().startswith("charset="):
                return p.split("=", 1)[1].strip() or "utf-8"
        return "utf-8"
 
 
class HttpError(Exception):
    def __init__(self, status: int, url: str, message: Optional[str] = None):
        self.status = status
        self.url = url
        self.message = message or f"HTTP {status} for {url}"
        super().__init__(self.message)
 
 
class HttpClient:
    """
    A lightweight HTTP client for REST API calls.
    """
 
    def __init__(
        self,
        base_url: str = "",
        headers: Optional[Mapping[str, str]] = None,
        timeout: float = DEFAULT_TIMEOUT,
        retry: Retry = Retry(
            backoff=ExponentialWithJitterBackoff(base=1, cap=10), retries=3
        ),
        verify_tls: bool = True,
        # TLS verification (server) options
        ca_file: Optional[str] = None,
        ca_path: Optional[str] = None,
        ca_data: Optional[Union[str, bytes]] = None,
        # Mutual TLS (client cert) options
        client_cert_file: Optional[str] = None,
        client_key_file: Optional[str] = None,
        client_key_password: Optional[str] = None,
        auth_basic: Optional[Tuple[str, str]] = None,  # (username, password)
        user_agent: str = DEFAULT_USER_AGENT,
    ) -> None:
        """
        Initialize a new HTTP client instance.
 
        Args:
            base_url: Base URL for all requests. Will be prefixed to all paths.
            headers: Default headers to include in all requests.
            timeout: Default timeout in seconds for requests.
            retry: Retry configuration for failed requests.
            verify_tls: Whether to verify TLS certificates.
            ca_file: Path to CA certificate file for TLS verification.
            ca_path: Path to a directory containing CA certificates.
            ca_data: CA certificate data as string or bytes.
            client_cert_file: Path to client certificate for mutual TLS.
            client_key_file: Path to a client private key for mutual TLS.
            client_key_password: Password for an encrypted client private key.
            auth_basic: Tuple of (username, password) for HTTP basic auth.
            user_agent: User-Agent header value for requests.
 
        The client supports both regular HTTPS with server verification and mutual TLS
        authentication. For server verification, provide CA certificate information via
        ca_file, ca_path or ca_data. For mutual TLS, additionally provide a client
        certificate and key via client_cert_file and client_key_file.
        """
        self.base_url = (
            base_url.rstrip() + "/"
            if base_url and not base_url.endswith("/")
            else base_url
        )
        self._default_headers = {k.lower(): v for k, v in (headers or {}).items()}
        self.timeout = timeout
        self.retry = retry
        self.retry.update_supported_errors((HTTPError, URLError, ssl.SSLError))
        self.verify_tls = verify_tls
 
        # TLS settings
        self.ca_file = ca_file
        self.ca_path = ca_path
        self.ca_data = ca_data
        self.client_cert_file = client_cert_file
        self.client_key_file = client_key_file
        self.client_key_password = client_key_password
 
        self.auth_basic = auth_basic
        self.user_agent = user_agent
 
    # Public JSON-centric helpers
    def get(
        self,
        path: str,
        params: Optional[
            Mapping[str, Union[None, str, int, float, bool, list, tuple]]
        ] = None,
        headers: Optional[Mapping[str, str]] = None,
        timeout: Optional[float] = None,
        expect_json: bool = True,
    ) -> Union[HttpResponse, Any]:
        return self._json_call(
            "GET",
            path,
            params=params,
            headers=headers,
            timeout=timeout,
            body=None,
            expect_json=expect_json,
        )
 
    def delete(
        self,
        path: str,
        params: Optional[
            Mapping[str, Union[None, str, int, float, bool, list, tuple]]
        ] = None,
        headers: Optional[Mapping[str, str]] = None,
        timeout: Optional[float] = None,
        expect_json: bool = True,
    ) -> Union[HttpResponse, Any]:
        return self._json_call(
            "DELETE",
            path,
            params=params,
            headers=headers,
            timeout=timeout,
            body=None,
            expect_json=expect_json,
        )
 
    def post(
        self,
        path: str,
        json_body: Optional[Any] = None,
        data: Optional[Union[bytes, str]] = None,
        params: Optional[
            Mapping[str, Union[None, str, int, float, bool, list, tuple]]
        ] = None,
        headers: Optional[Mapping[str, str]] = None,
        timeout: Optional[float] = None,
        expect_json: bool = True,
    ) -> Union[HttpResponse, Any]:
        return self._json_call(
            "POST",
            path,
            params=params,
            headers=headers,
            timeout=timeout,
            body=self._prepare_body(json_body=json_body, data=data),
            expect_json=expect_json,
        )
 
    def put(
        self,
        path: str,
        json_body: Optional[Any] = None,
        data: Optional[Union[bytes, str]] = None,
        params: Optional[
            Mapping[str, Union[None, str, int, float, bool, list, tuple]]
        ] = None,
        headers: Optional[Mapping[str, str]] = None,
        timeout: Optional[float] = None,
        expect_json: bool = True,
    ) -> Union[HttpResponse, Any]:
        return self._json_call(
            "PUT",
            path,
            params=params,
            headers=headers,
            timeout=timeout,
            body=self._prepare_body(json_body=json_body, data=data),
            expect_json=expect_json,
        )
 
    def patch(
        self,
        path: str,
        json_body: Optional[Any] = None,
        data: Optional[Union[bytes, str]] = None,
        params: Optional[
            Mapping[str, Union[None, str, int, float, bool, list, tuple]]
        ] = None,
        headers: Optional[Mapping[str, str]] = None,
        timeout: Optional[float] = None,
        expect_json: bool = True,
    ) -> Union[HttpResponse, Any]:
        return self._json_call(
            "PATCH",
            path,
            params=params,
            headers=headers,
            timeout=timeout,
            body=self._prepare_body(json_body=json_body, data=data),
            expect_json=expect_json,
        )
 
    # Low-level request
    def request(
        self,
        method: str,
        path: str,
        params: Optional[
            Mapping[str, Union[None, str, int, float, bool, list, tuple]]
        ] = None,
        headers: Optional[Mapping[str, str]] = None,
        body: Optional[Union[bytes, str]] = None,
        timeout: Optional[float] = None,
    ) -> HttpResponse:
        url = self._build_url(path, params)
        all_headers = self._prepare_headers(headers, body)
        data = body.encode("utf-8") if isinstance(body, str) else body
 
        req = Request(url=url, method=method.upper(), data=data, headers=all_headers)
 
        context: Optional[ssl.SSLContext] = None
        if url.lower().startswith("https"):
            if self.verify_tls:
                # Use provided CA material if any; fall back to system defaults
                context = ssl.create_default_context(
                    cafile=self.ca_file,
                    capath=self.ca_path,
                    cadata=self.ca_data,
                )
                # Load client certificate for mTLS if configured
                if self.client_cert_file:
                    context.load_cert_chain(
                        certfile=self.client_cert_file,
                        keyfile=self.client_key_file,
                        password=self.client_key_password,
                    )
            else:
                # Verification disabled
                context = ssl.create_default_context()
                context.check_hostname = False
                context.verify_mode = ssl.CERT_NONE
 
        try:
            return self.retry.call_with_retry(
                lambda: self._make_request(req, context=context, timeout=timeout),
                lambda _: dummy_fail(),
                lambda error: self._is_retryable_http_error(error),
            )
        except HTTPError as e:
            # Read error body, build response, and decide on retry
            err_body = b""
            try:
                err_body = e.read()
            except Exception:
                pass
            headers_map = {k.lower(): v for k, v in (e.headers or {}).items()}
            err_body = self._maybe_decompress(err_body, headers_map)
            status = getattr(e, "code", 0) or 0
            response = HttpResponse(
                status=status,
                headers=headers_map,
                url=url,
                content=err_body,
            )
            return response
 
    def _make_request(
        self,
        request: Request,
        context: Optional[ssl.SSLContext] = None,
        timeout: Optional[float] = None,
    ):
        with urlopen(request, timeout=timeout or self.timeout, context=context) as resp:
            raw = resp.read()
            headers_map = {k.lower(): v for k, v in resp.headers.items()}
            raw = self._maybe_decompress(raw, headers_map)
            return HttpResponse(
                status=resp.status,
                headers=headers_map,
                url=resp.geturl(),
                content=raw,
            )
 
    def _is_retryable_http_error(self, error: Exception) -> bool:
        if isinstance(error, HTTPError):
            return self._should_retry_status(error.code)
        return False
 
    # Internal utilities
    def _json_call(
        self,
        method: str,
        path: str,
        params: Optional[
            Mapping[str, Union[None, str, int, float, bool, list, tuple]]
        ] = None,
        headers: Optional[Mapping[str, str]] = None,
        timeout: Optional[float] = None,
        body: Optional[Union[bytes, str]] = None,
        expect_json: bool = True,
    ) -> Union[HttpResponse, Any]:
        resp = self.request(
            method=method,
            path=path,
            params=params,
            headers=headers,
            body=body,
            timeout=timeout,
        )
        if not (200 <= resp.status < 400):
            raise HttpError(resp.status, resp.url, resp.text())
        if expect_json:
            return resp.json()
        return resp
 
    def _prepare_body(
        self, json_body: Optional[Any] = None, data: Optional[Union[bytes, str]] = None
    ) -> Optional[Union[bytes, str]]:
        if json_body is not None and data is not None:
            raise ValueError("Provide either json_body or data, not both.")
        if json_body is not None:
            return json.dumps(json_body, ensure_ascii=False, separators=(",", ":"))
        return data
 
    def _build_url(
        self,
        path: str,
        params: Optional[
            Mapping[str, Union[None, str, int, float, bool, list, tuple]]
        ] = None,
    ) -> str:
        url = urljoin(self.base_url or "", path)
        if params:
            # urlencode with doseq=True supports list/tuple values
            query = urlencode(
                {k: v for k, v in params.items() if v is not None}, doseq=True
            )
            separator = "&" if ("?" in url) else "?"
            url = f"{url}{separator}{query}" if query else url
        return url
 
    def _prepare_headers(
        self, headers: Optional[Mapping[str, str]], body: Optional[Union[bytes, str]]
    ) -> Dict[str, str]:
        # Start with defaults
        prepared: Dict[str, str] = {}
        prepared.update(self._default_headers)
 
        # Standard defaults for JSON REST usage
        prepared.setdefault("accept", "application/json")
        prepared.setdefault("user-agent", self.user_agent)
        # We will send gzip accept-encoding; handle decompression manually
        prepared.setdefault("accept-encoding", "gzip, deflate")
 
        # If we have a string body and content-type not specified, assume JSON
        if body is not None and isinstance(body, str):
            prepared.setdefault("content-type", "application/json; charset=utf-8")
 
        # Basic authentication if provided and not overridden
        if self.auth_basic and "authorization" not in prepared:
            user, pwd = self.auth_basic
            token = base64.b64encode(f"{user}:{pwd}".encode("utf-8")).decode("ascii")
            prepared["authorization"] = f"Basic {token}"
 
        # Merge per-call headers (case-insensitive)
        if headers:
            for k, v in headers.items():
                prepared[k.lower()] = v
 
        # urllib expects header keys in canonical capitalization sometimes; but it’s tolerant.
        # We'll return as provided; urllib will handle it.
        return prepared
 
    def _should_retry_status(self, status: int) -> bool:
        return status in RETRY_STATUS_CODES
 
    def _maybe_decompress(self, content: bytes, headers: Mapping[str, str]) -> bytes:
        if not content:
            return content
        encoding = (headers.get("content-encoding") or "").lower()
        try:
            if "gzip" in encoding:
                return gzip.decompress(content)
            if "deflate" in encoding:
                # Try raw deflate, then zlib-wrapped
                try:
                    return zlib.decompress(content, -zlib.MAX_WBITS)
                except zlib.error:
                    return zlib.decompress(content)
        except Exception:
            # If decompression fails, return original bytes
            return content
        return content