eggfetch-python 0.1.4

Python sync and asyncio bindings for the eggfetch HTTP engine (Rust core via PyO3; Python users install from PyPI)
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
"""HTTPX-compatible Request class for eggfetch."""

from __future__ import annotations

import json as _json
import typing
from urllib.parse import urlencode, urlparse, urlunparse, parse_qs, quote

from eggfetch.compat.httpx._urls import URL, QueryParams
from eggfetch.compat.httpx._headers import Headers
from eggfetch.compat.httpx._cookies import Cookies
from eggfetch.compat.httpx._exceptions import RequestNotRead, StreamConsumed
from eggfetch.compat.httpx._stream import ByteStream

if typing.TYPE_CHECKING:
    from typing import AsyncIterator, Iterator


class Request:
    """HTTPX-compatible Request object."""

    __slots__ = (
        "_method",
        "_url",
        "_headers",
        "_cookies",
        "_params",
        "_content",
        "_stream",
        "_extensions",
        "_http_version",
        "_is_stream_consumed",
        "_stream_consumed",
        "_files",
        "_multipart_data",
        "_explicit_cookie_header",
    )

    def __init__(
        self,
        method: str,
        url: URL | str,
        *,
        params=None,
        headers=None,
        cookies=None,
        content: bytes | None = None,
        data=None,
        files=None,
        json=None,
        stream=None,
        extensions: dict | None = None,
    ) -> None:
        # ── Body-source mutual exclusion (HTTPX rules) ──────────────
        # content is exclusive with data, files, json
        # json is exclusive with content, data, files
        # data + files is VALID (multipart)
        # stream is exclusive with everything
        has_content = content is not None
        has_json = json is not None
        has_data = data is not None
        has_files = files is not None
        has_stream = stream is not None

        if has_stream:
            if has_content or has_json or has_data or has_files:
                raise ValueError(
                    "stream= is mutually exclusive with content, data, files, and json."
                )
        elif has_content and has_json:
            raise ValueError(
                "content and json are mutually exclusive."
            )
        elif has_content and has_data:
            raise ValueError(
                "content and data are mutually exclusive."
            )
        elif has_content and has_files:
            raise ValueError(
                "content and files are mutually exclusive."
            )
        elif has_json and has_data:
            raise ValueError(
                "json and data are mutually exclusive."
            )
        elif has_json and has_files:
            raise ValueError(
                "json and files are mutually exclusive."
            )
        # data + files is allowed (multipart)

        self._method = method.upper()
        self._http_version = "HTTP/1.1"
        self._is_stream_consumed = False
        self._stream_consumed = False
        self._stream = stream

        # Track whether the Cookie header was explicitly provided by the user.
        # This is used during redirects to decide whether to carry the header
        # or regenerate from the client jar.
        self._explicit_cookie_header: str | None = None

        # Build headers from user-provided value
        if isinstance(headers, Headers):
            self._headers = headers
        else:
            self._headers = Headers(headers)

        # Capture explicit Cookie header before jar merge happens.
        # Used during redirects to decide whether to carry or regenerate.
        self._explicit_cookie_header = self._headers.get("cookie")

        # Build params
        if isinstance(params, QueryParams):
            self._params = params
        else:
            self._params = QueryParams(params)

        # Build cookies
        if isinstance(cookies, Cookies):
            self._cookies = cookies
        else:
            self._cookies = Cookies(cookies)

        # ── Handle body content ──────────────────────────────────────
        self._content: bytes | None = None
        self._files = None
        self._multipart_data = None

        if has_content:
            if hasattr(content, "read") and not isinstance(content, (bytes, bytearray)):
                def _file_reader():
                    while True:
                        chunk = content.read(8192)
                        if not chunk:
                            break
                        if isinstance(chunk, str):
                            chunk = chunk.encode("utf-8")
                        yield chunk

                self._stream = _file_reader()
            elif hasattr(content, "__iter__") and not isinstance(content, (bytes, str)):
                self._stream = content
            else:
                self._content = content if isinstance(content, bytes) else content.encode("utf-8")
        elif has_json:
            self._content = _json.dumps(
                json, separators=(",", ":"), ensure_ascii=False
            ).encode("utf-8")
            if "content-type" not in self._headers:
                self._headers["content-type"] = "application/json"
        elif has_data and has_files:
            # data + files → multipart: store both for later encoding
            self._files = files
            self._multipart_data = data
            self._content = None
        elif has_data:
            if isinstance(data, dict):
                self._content = urlencode(data).encode("utf-8")
                if "content-type" not in self._headers:
                    self._headers["content-type"] = "application/x-www-form-urlencoded"
            elif isinstance(data, (list, tuple)):
                self._content = urlencode(data).encode("utf-8")
                if "content-type" not in self._headers:
                    self._headers["content-type"] = "application/x-www-form-urlencoded"
            elif isinstance(data, str):
                self._content = data.encode("utf-8")
            elif isinstance(data, bytes):
                self._content = data
            else:
                self._content = str(data).encode("utf-8")
        elif has_files:
            self._files = files

        # ── Apply params to URL ─────────────────────────────────────
        self._url = self._apply_params_to_url(
            URL(url) if not isinstance(url, URL) else url,
            self._params,
        )

        # ── Auto-headers ────────────────────────────────────────────
        host = self._url.host
        if host is not None and not has_stream:
            port = self._url.port
            scheme = self._url.scheme
            if port and (
                (scheme == "http" and port != 80) or (scheme == "https" and port != 443)
            ):
                host = f"{host}:{port}"
            if "host" not in self._headers:
                self._headers["host"] = host

        # Only add Transfer-Encoding: chunked for stream= if it wasn't
        # explicitly provided.  HTTPX does NOT auto-add it for low-level
        # stream= — only for encoded content paths.
        # (Removed auto Transfer-Encoding injection for stream=)

        # Content-Length for encoded content
        if self._content is not None:
            if "content-length" not in self._headers:
                self._headers["content-length"] = str(len(self._content))

        elif self._stream is None and self._method in {"POST", "PUT", "PATCH"}:
            if "content-length" not in self._headers and "transfer-encoding" not in self._headers:
                self._headers["content-length"] = "0"

        # Extensions
        self._extensions: dict = extensions if extensions is not None else {}

    @staticmethod
    def _apply_params_to_url(url: URL, params: QueryParams) -> URL:
        """Merge query params into the URL, matching HTTPX behavior.

        HTTPX replaces existing query parameters with those provided via
        the ``params`` argument for direct Request construction.
        """
        if not params or not params.multi_items():
            return url

        raw = str(url)
        parsed = urlparse(raw)

        # Merge: params replace existing query
        new_query = urlencode(params.multi_items(), doseq=True)
        rebuilt = urlunparse((
            parsed.scheme,
            parsed.netloc,
            parsed.path,
            parsed.params,
            new_query,
            "",  # fragment
        ))
        return URL(rebuilt)

    @staticmethod
    def _encode_files(files, data=None) -> bytes:
        """Encode files (and optional data fields) as multipart/form-data.

        Supports HTTPX file tuple forms:
        - file object or bytes
        - (filename, fileobj)
        - (filename, fileobj, content_type)
        - (filename, fileobj, content_type, headers)
        """
        boundary = "----eggfetchboundary"
        parts: list[bytes] = []

        # Encode data fields first (if any)
        if data is not None:
            if isinstance(data, dict):
                items = list(data.items())
            elif isinstance(data, (list, tuple)):
                items = data
            else:
                items = []
            for key, value in items:
                if isinstance(value, (list, tuple)):
                    for v in value:
                        parts.append(
                            (
                                f"--{boundary}\r\n"
                                f'Content-Disposition: form-data; name="{key}"\r\n\r\n'
                            ).encode("utf-8")
                            + (
                                v.encode("utf-8") if isinstance(v, str) else str(v).encode("utf-8")
                            )
                            + b"\r\n"
                        )
                else:
                    parts.append(
                        (
                            f"--{boundary}\r\n"
                            f'Content-Disposition: form-data; name="{key}"\r\n\r\n'
                        ).encode("utf-8")
                        + (
                            value.encode("utf-8")
                            if isinstance(value, str)
                            else str(value).encode("utf-8")
                        )
                        + b"\r\n"
                    )

        # Encode files
        if isinstance(files, dict):
            file_items = list(files.items())
        elif isinstance(files, (list, tuple)):
            file_items = list(files)
        else:
            file_items = [("file", files)]

        for field_name, file_spec in file_items:
            file_headers: dict[str, str] = {}

            if isinstance(file_spec, tuple):
                if len(file_spec) >= 4:
                    filename, fileobj, content_type, file_h = (
                        file_spec[0],
                        file_spec[1],
                        file_spec[2],
                        file_spec[3],
                    )
                    if isinstance(file_h, dict):
                        file_headers = file_h
                elif len(file_spec) == 3:
                    filename, fileobj, content_type = file_spec
                elif len(file_spec) == 2:
                    filename, fileobj = file_spec
                    content_type = "application/octet-stream"
                else:
                    filename = file_spec[0] if file_spec else "file"
                    fileobj = file_spec[1] if len(file_spec) > 1 else b""
                    content_type = "application/octet-stream"
            else:
                filename = str(field_name)
                fileobj = file_spec
                content_type = "application/octet-stream"

            if isinstance(fileobj, (bytes, bytearray)):
                body = bytes(fileobj)
            elif isinstance(fileobj, str):
                body = fileobj.encode("utf-8")
            elif hasattr(fileobj, "read"):
                body = fileobj.read()
                if isinstance(body, str):
                    body = body.encode("utf-8")
            else:
                body = str(fileobj).encode("utf-8")

            # Build part header
            header_lines = [
                f"--{boundary}\r\n",
                f'Content-Disposition: form-data; name="{field_name}"',
            ]
            if filename:
                header_lines[1] += f'; filename="{filename}"'
            header_lines[1] += "\r\n"
            header_lines.append(f"Content-Type: {content_type}\r\n")
            for hk, hv in file_headers.items():
                header_lines.append(f"{hk}: {hv}\r\n")
            header_lines.append("\r\n")

            part = "".join(header_lines).encode("utf-8") + body + b"\r\n"
            parts.append(part)

        parts.append(f"--{boundary}--\r\n".encode("utf-8"))
        return b"".join(parts)

    @property
    def method(self) -> str:
        return self._method

    @property
    def url(self) -> URL:
        return self._url

    @url.setter
    def url(self, value: URL | str) -> None:
        self._url = URL(value) if not isinstance(value, URL) else value

    @property
    def headers(self) -> Headers:
        return self._headers

    @property
    def cookies(self) -> Cookies:
        return self._cookies

    @property
    def params(self) -> QueryParams:
        return self._params

    @property
    def content(self) -> bytes:
        if self._content is None and self._stream is not None:
            raise RequestNotRead()
        return self._content or b""

    @property
    def stream(self):
        return self._stream

    @property
    def files(self):
        return self._files

    @property
    def extensions(self) -> dict:
        return self._extensions

    @property
    def is_stream_consumed(self) -> bool:
        return self._is_stream_consumed

    @property
    def http_version(self) -> str:
        return self._http_version

    @http_version.setter
    def http_version(self, value: str) -> None:
        self._http_version = value

    def read(self) -> bytes:
        if self._stream is not None and self._content is None:
            if self._stream_consumed:
                raise StreamConsumed()
            chunks: list[bytes] = []
            for chunk in self._stream:
                if isinstance(chunk, bytes):
                    chunks.append(chunk)
                elif isinstance(chunk, str):
                    chunks.append(chunk.encode("utf-8"))
                else:
                    chunks.append(str(chunk).encode("utf-8"))
            self._content = b"".join(chunks)
            self._is_stream_consumed = True
            self._stream_consumed = True
            self._stream = ByteStream(self._content)
        if self._content is None:
            return b""
        return self._content

    async def aread(self) -> bytes:
        if self._stream is not None and self._content is None:
            if self._stream_consumed:
                raise StreamConsumed()
            chunks: list[bytes] = []
            async for chunk in self._stream:  # type: ignore[union-attr]
                if isinstance(chunk, bytes):
                    chunks.append(chunk)
                elif isinstance(chunk, str):
                    chunks.append(chunk.encode("utf-8"))
                else:
                    chunks.append(str(chunk).encode("utf-8"))
            self._content = b"".join(chunks)
            self._is_stream_consumed = True
            self._stream_consumed = True
            self._stream = ByteStream(self._content)
        if self._content is None:
            return b""
        return self._content

    def __repr__(self) -> str:
        return f"<Request({self._method!r}, {str(self._url)!r})>"