| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import copy as _copy |
| 4 |
from typing import Any |
| 5 |
from typing_extensions import override |
| 6 |
|
| 7 |
import httpx2 |
| 8 |
|
| 9 |
from ._types import Body, Query, Headers, NotGiven, not_given |
| 10 |
from ._utils import is_given |
| 11 |
from ._compat import model_copy |
| 12 |
from ._models import FinalRequestOptions |
| 13 |
|
| 14 |
|
| 15 |
class APIRequest: |
| 16 |
"""A view over the request that the client is about to execute. |
| 17 |
|
| 18 |
Treat instances as immutable; use `copy()` to derive a modified request. |
| 19 |
""" |
| 20 |
|
| 21 |
def __init__( |
| 22 |
self, |
| 23 |
*, |
| 24 |
options: FinalRequestOptions, |
| 25 |
cast_to: Any, |
| 26 |
stream: bool = False, |
| 27 |
stream_cls: type[Any] | None = None, |
| 28 |
retries_taken: int = 0, |
| 29 |
) -> None: |
| 30 |
self.options = options |
| 31 |
self.cast_to = cast_to |
| 32 |
self.stream = stream |
| 33 |
self.stream_cls = stream_cls |
| 34 |
self.retries_taken = retries_taken |
| 35 |
"""The number of retries the SDK has already taken for this call. |
| 36 |
|
| 37 |
`0` on the first attempt; the middleware chain is invoked once per HTTP attempt. |
| 38 |
""" |
| 39 |
|
| 40 |
@property |
| 41 |
def method(self) -> str: |
| 42 |
return self.options.method |
| 43 |
|
| 44 |
@property |
| 45 |
def url(self) -> str: |
| 46 |
return self.options.url |
| 47 |
|
| 48 |
@property |
| 49 |
def headers(self) -> Headers: |
| 50 |
headers = self.options.headers |
| 51 |
return headers if is_given(headers) else {} |
| 52 |
|
| 53 |
@property |
| 54 |
def query_params(self) -> Query: |
| 55 |
return self.options.params |
| 56 |
|
| 57 |
@property |
| 58 |
def json(self) -> Body | None: |
| 59 |
return self.options.json_data |
| 60 |
|
| 61 |
@property |
| 62 |
def timeout(self) -> float | httpx2.Timeout | None | NotGiven: |
| 63 |
return self.options.timeout |
| 64 |
|
| 65 |
@property |
| 66 |
def max_retries(self) -> int | NotGiven: |
| 67 |
return self.options.max_retries |
| 68 |
|
| 69 |
def copy( |
| 70 |
self, |
| 71 |
*, |
| 72 |
method: str | NotGiven = not_given, |
| 73 |
url: str | NotGiven = not_given, |
| 74 |
headers: Headers | NotGiven = not_given, |
| 75 |
params: Query | NotGiven = not_given, |
| 76 |
body: Body | NotGiven = not_given, |
| 77 |
timeout: float | httpx2.Timeout | None | NotGiven = not_given, |
| 78 |
) -> APIRequest: |
| 79 |
# Note: we intentionally avoid `model_copy(deep=True)` here as fields like |
| 80 |
# `files` and `content` can hold open file/IO objects which cannot be deep-copied. |
| 81 |
# |
| 82 |
# Instead we shallow-copy the options and then deep-copy only the JSON-safe mutable |
| 83 |
# fields so that mutating the returned request never affects the original request. |
| 84 |
options = model_copy(self.options) |
| 85 |
options.json_data = _copy.deepcopy(options.json_data) |
| 86 |
options.extra_json = _copy.deepcopy(options.extra_json) |
| 87 |
options.params = _copy.deepcopy(options.params) |
| 88 |
if is_given(options.headers): |
| 89 |
options.headers = dict(options.headers) |
| 90 |
|
| 91 |
if is_given(method): |
| 92 |
options.method = method |
| 93 |
if is_given(url): |
| 94 |
options.url = url |
| 95 |
if is_given(headers): |
| 96 |
options.headers = headers |
| 97 |
if is_given(params): |
| 98 |
options.params = params |
| 99 |
if not isinstance(body, NotGiven): |
| 100 |
options.json_data = body |
| 101 |
if not isinstance(timeout, NotGiven): |
| 102 |
options.timeout = timeout |
| 103 |
|
| 104 |
return APIRequest( |
| 105 |
options=options, |
| 106 |
cast_to=self.cast_to, |
| 107 |
stream=self.stream, |
| 108 |
stream_cls=self.stream_cls, |
| 109 |
retries_taken=self.retries_taken, |
| 110 |
) |
| 111 |
|
| 112 |
@override |
| 113 |
def __repr__(self) -> str: |
| 114 |
return f"<APIRequest method={self.method!r} url={self.url!r} stream={self.stream!r}>" |