pyqwest 0.2.0__cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyqwest/_pyqwest.pyi ADDED
@@ -0,0 +1,1132 @@
1
+ from collections.abc import (
2
+ AsyncIterator,
3
+ Awaitable,
4
+ ItemsView,
5
+ Iterable,
6
+ Iterator,
7
+ KeysView,
8
+ Mapping,
9
+ Sequence,
10
+ ValuesView,
11
+ )
12
+ from contextlib import AbstractContextManager
13
+ from types import TracebackType
14
+ from typing import Protocol, TypeAlias, TypeVar, overload, runtime_checkable
15
+
16
+ _T = TypeVar("_T")
17
+ JSON: TypeAlias = Mapping[str, JSON] | Sequence[JSON] | str | int | float | bool | None
18
+
19
+ Buffer: TypeAlias = bytes | memoryview | bytearray
20
+
21
+ class Headers:
22
+ """Container of HTTP headers.
23
+
24
+ This class behaves like a dictionary with case-insensitive keys and
25
+ string values. Standard dictionary access will act as if keys can only
26
+ have a single value. The add method can be used to It additionally can be used to store
27
+ multiple values for the same key by using the add method. Iterating over
28
+ values or items will return all values, including duplicates.
29
+ """
30
+
31
+ def __init__(
32
+ self, items: Mapping[str, str] | Iterable[tuple[str, str]] | None = None
33
+ ) -> None:
34
+ """Creates a new Headers object.
35
+
36
+ Args:
37
+ items: Initial headers to add.
38
+ """
39
+
40
+ def __getitem__(self, key: str) -> str:
41
+ """Return the header value for the key.
42
+
43
+ If multiple values are present for the key, returns the first value.
44
+
45
+ Args:
46
+ key: The header name.
47
+
48
+ Raises:
49
+ KeyError: If the key is not present.
50
+ """
51
+
52
+ def __setitem__(self, key: str, value: str) -> None:
53
+ """Sets the header value for the key, replacing any existing values.
54
+
55
+ Args:
56
+ key: The header name.
57
+ value: The header value.
58
+ """
59
+
60
+ def __delitem__(self, key: str) -> None:
61
+ """Deletes all values for the key.
62
+
63
+ Args:
64
+ key: The header name.
65
+
66
+ Raises:
67
+ KeyError: If the key is not present.
68
+ """
69
+
70
+ def __iter__(self) -> Iterator[str]:
71
+ """Returns an iterator over the header names."""
72
+
73
+ def __len__(self) -> int:
74
+ """Returns the number of unique header names."""
75
+
76
+ def __eq__(self, other: object) -> bool:
77
+ """Compares the headers for equality with another Headers object,
78
+ mapping, or iterable of key-value pairs.
79
+
80
+ Args:
81
+ other: The object to compare against.
82
+ """
83
+
84
+ def get(self, key: str, default: str | None = None) -> str | None:
85
+ """Returns the header value for the key, or default if not present.
86
+
87
+ Args:
88
+ key: The header name.
89
+ default: The default value to return if the key is not present.
90
+ """
91
+
92
+ @overload
93
+ def pop(self, key: str) -> str:
94
+ """Removes and returns the header value for the key.
95
+
96
+ Args:
97
+ key: The header name.
98
+
99
+ Raises:
100
+ KeyError: If the key is not present.
101
+ """
102
+
103
+ @overload
104
+ def pop(self, key: str, default: _T) -> str | _T:
105
+ """Removes and returns the header value for the key, or default if not present.
106
+
107
+ Args:
108
+ key: The header name.
109
+ default: The default value to return if the key is not present.
110
+ """
111
+
112
+ def popitem(self) -> tuple[str, str]:
113
+ """Removes and returns an arbitrary (name, value) pair. Will return the same
114
+ name multiple times if it has multiple values.
115
+
116
+ Raises:
117
+ KeyError: If the headers are empty.
118
+ """
119
+
120
+ def setdefault(self, key: str, default: str | None = None) -> str:
121
+ """If the key is not present, sets it to the default value.
122
+ Returns the value for the key.
123
+
124
+ Args:
125
+ key: The header name.
126
+ default: The default value to set and return if the key is not present.
127
+ """
128
+
129
+ def add(self, key: str, value: str) -> None:
130
+ """Adds a header value for the key. Existing values are preserved.
131
+
132
+ Args:
133
+ key: The header name.
134
+ value: The header value.
135
+ """
136
+
137
+ @overload
138
+ def update(self, **kwargs: str) -> None:
139
+ """Updates headers from keyword arguments. Existing values are replaced.
140
+
141
+ Args:
142
+ **kwargs: Header names and values to set.
143
+ """
144
+ @overload
145
+ def update(
146
+ self, items: Mapping[str, str] | Iterable[tuple[str, str]], /, **kwargs: str
147
+ ) -> None:
148
+ """Updates headers with the provided items. Existing values are replaced.
149
+
150
+ Args:
151
+ items: Header names and values to set.
152
+ **kwargs: Additional header names and values to set after items. May overwrite items.
153
+ """
154
+
155
+ def clear(self) -> None:
156
+ """Removes all headers."""
157
+
158
+ def getall(self, key: str) -> Sequence[str]:
159
+ """Returns all header values for the key.
160
+
161
+ Args:
162
+ key: The header name.
163
+ """
164
+
165
+ def items(self) -> ItemsView[str, str]:
166
+ """Returns a new view of all header name-value pairs, including duplicates."""
167
+
168
+ def keys(self) -> KeysView[str]:
169
+ """Returns a new view of all unique header names."""
170
+
171
+ def values(self) -> ValuesView[str]:
172
+ """Returns a new view of all header values, including duplicates."""
173
+
174
+ def __contains__(self, key: object) -> bool:
175
+ """Returns True if the header name is present.
176
+
177
+ Args:
178
+ key: The header name.
179
+ """
180
+
181
+ class HTTPVersion:
182
+ """An enumeration of HTTP versions."""
183
+
184
+ HTTP1: HTTPVersion
185
+ """HTTP/1.1"""
186
+
187
+ HTTP2: HTTPVersion
188
+ """HTTP/2"""
189
+
190
+ HTTP3: HTTPVersion
191
+ """HTTP/3"""
192
+
193
+ class Client:
194
+ def __init__(self, transport: Transport | None = None) -> None:
195
+ """Creates a new asynchronous HTTP client.
196
+
197
+ Args:
198
+ transport: The transport to use for requests. If None, the shared default
199
+ transport will be used.
200
+ """
201
+
202
+ def get(
203
+ self,
204
+ url: str,
205
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
206
+ timeout: float | None = None,
207
+ ) -> Awaitable[FullResponse]:
208
+ """Executes a GET HTTP request.
209
+
210
+ Args:
211
+ url: The unencoded request URL.
212
+ headers: The request headers.
213
+ timeout: The timeout for the request in seconds.
214
+
215
+ Raises:
216
+ ConnectionError: If the connection fails.
217
+ TimeoutError: If the request times out.
218
+ ReadError: If an error occurs reading the response.
219
+ WriteError: If an error occurs writing the request.
220
+ """
221
+
222
+ def post(
223
+ self,
224
+ url: str,
225
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
226
+ content: bytes | AsyncIterator[bytes] | None = None,
227
+ timeout: float | None = None,
228
+ ) -> Awaitable[FullResponse]:
229
+ """Executes a POST HTTP request.
230
+
231
+ Args:
232
+ url: The unencoded request URL.
233
+ headers: The request headers.
234
+ content: The request content.
235
+ timeout: The timeout for the request in seconds.
236
+
237
+ Raises:
238
+ ConnectionError: If the connection fails.
239
+ TimeoutError: If the request times out.
240
+ ReadError: If an error occurs reading the response.
241
+ WriteError: If an error occurs writing the request.
242
+ """
243
+
244
+ def delete(
245
+ self,
246
+ url: str,
247
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
248
+ timeout: float | None = None,
249
+ ) -> Awaitable[FullResponse]:
250
+ """Executes a DELETE HTTP request.
251
+
252
+ Args:
253
+ url: The unencoded request URL.
254
+ headers: The request headers.
255
+ timeout: The timeout for the request in seconds.
256
+
257
+ Raises:
258
+ ConnectionError: If the connection fails.
259
+ TimeoutError: If the request times out.
260
+ ReadError: If an error occurs reading the response.
261
+ WriteError: If an error occurs writing the request.
262
+ """
263
+
264
+ def head(
265
+ self,
266
+ url: str,
267
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
268
+ timeout: float | None = None,
269
+ ) -> Awaitable[FullResponse]:
270
+ """Executes a HEAD HTTP request.
271
+
272
+ Args:
273
+ url: The unencoded request URL.
274
+ headers: The request headers.
275
+ timeout: The timeout for the request in seconds.
276
+
277
+ Raises:
278
+ ConnectionError: If the connection fails.
279
+ TimeoutError: If the request times out.
280
+ ReadError: If an error occurs reading the response.
281
+ WriteError: If an error occurs writing the request.
282
+ """
283
+
284
+ def options(
285
+ self,
286
+ url: str,
287
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
288
+ timeout: float | None = None,
289
+ ) -> Awaitable[FullResponse]:
290
+ """Executes a OPTIONS HTTP request.
291
+
292
+ Args:
293
+ url: The unencoded request URL.
294
+ headers: The request headers.
295
+ timeout: The timeout for the request in seconds.
296
+
297
+ Raises:
298
+ ConnectionError: If the connection fails.
299
+ TimeoutError: If the request times out.
300
+ ReadError: If an error occurs reading the response.
301
+ WriteError: If an error occurs writing the request.
302
+ """
303
+
304
+ def patch(
305
+ self,
306
+ url: str,
307
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
308
+ content: bytes | AsyncIterator[bytes] | None = None,
309
+ timeout: float | None = None,
310
+ ) -> Awaitable[FullResponse]:
311
+ """Executes a PATCH HTTP request.
312
+
313
+ Args:
314
+ url: The unencoded request URL.
315
+ headers: The request headers.
316
+ content: The request content.
317
+ timeout: The timeout for the request in seconds.
318
+
319
+ Raises:
320
+ ConnectionError: If the connection fails.
321
+ TimeoutError: If the request times out.
322
+ ReadError: If an error occurs reading the response.
323
+ WriteError: If an error occurs writing the request.
324
+ """
325
+
326
+ def put(
327
+ self,
328
+ url: str,
329
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
330
+ content: bytes | AsyncIterator[bytes] | None = None,
331
+ timeout: float | None = None,
332
+ ) -> Awaitable[FullResponse]:
333
+ """Executes a PUT HTTP request.
334
+
335
+ Args:
336
+ url: The unencoded request URL.
337
+ headers: The request headers.
338
+ content: The request content.
339
+ timeout: The timeout for the request in seconds.
340
+
341
+ Raises:
342
+ ConnectionError: If the connection fails.
343
+ TimeoutError: If the request times out.
344
+ ReadError: If an error occurs reading the response.
345
+ WriteError: If an error occurs writing the request.
346
+ """
347
+
348
+ def execute(
349
+ self,
350
+ method: str,
351
+ url: str,
352
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
353
+ content: bytes | AsyncIterator[bytes] | None = None,
354
+ timeout: float | None = None,
355
+ ) -> Awaitable[FullResponse]:
356
+ """Executes an HTTP request, returning the full buffered response.
357
+
358
+ Args:
359
+ method: The HTTP method.
360
+ url: The unencoded request URL.
361
+ headers: The request headers.
362
+ content: The request content.
363
+ timeout: The timeout for the request in seconds.
364
+
365
+ Raises:
366
+ ConnectionError: If the connection fails.
367
+ TimeoutError: If the request times out.
368
+ ReadError: If an error occurs reading the response.
369
+ WriteError: If an error occurs writing the request.
370
+ """
371
+
372
+ def stream(
373
+ self,
374
+ method: str,
375
+ url: str,
376
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
377
+ content: bytes | AsyncIterator[bytes] | None = None,
378
+ timeout: float | None = None,
379
+ ) -> Awaitable[Response]:
380
+ """Executes an HTTP request, allowing the response content to be streamed.
381
+
382
+ Args:
383
+ method: The HTTP method.
384
+ url: The unencoded request URL.
385
+ headers: The request headers.
386
+ content: The request content.
387
+ timeout: The timeout for the request in seconds.
388
+
389
+ Raises:
390
+ ConnectionError: If the connection fails.
391
+ TimeoutError: If the request times out.
392
+ ReadError: If an error occurs reading the response.
393
+ WriteError: If an error occurs writing the request.
394
+ """
395
+
396
+ @runtime_checkable
397
+ class Transport(Protocol):
398
+ """Protocol for asynchronous HTTP transport implementations.
399
+
400
+ The default implementation of Transport is HTTPTransport which issues requests.
401
+ Custom implementations may be useful to:
402
+
403
+ - Mock requests for testing.
404
+ - Add middleware wrapping transports
405
+ """
406
+
407
+ def execute(self, request: Request) -> Awaitable[Response]:
408
+ """Executes a request."""
409
+
410
+ class HTTPTransport:
411
+ """An HTTP transport implementation using reqwest."""
412
+
413
+ def __init__(
414
+ self,
415
+ *,
416
+ tls_ca_cert: bytes | None = None,
417
+ tls_key: bytes | None = None,
418
+ tls_cert: bytes | None = None,
419
+ http_version: HTTPVersion | None = None,
420
+ timeout: float | None = None,
421
+ connect_timeout: float | None = None,
422
+ read_timeout: float | None = None,
423
+ pool_idle_timeout: float | None = None,
424
+ pool_max_idle_per_host: int | None = None,
425
+ tcp_keepalive_interval: float | None = None,
426
+ enable_gzip: bool = False,
427
+ enable_brotli: bool = False,
428
+ enable_zstd: bool = False,
429
+ use_system_dns: bool = False,
430
+ ) -> None:
431
+ """Creates a new HTTPTransport object.
432
+
433
+ Without any arguments, the transport behaves like a raw, low-level HTTP transport,
434
+ with no timeouts or other higher level behavior. When creating a transport, take care
435
+ to set options to meet your needs. Also consider using get_default_transport instead
436
+ which is preconfigured with reasonable defaults, though does not support custom TLS
437
+ certificates.
438
+
439
+ Args:
440
+ tls_ca_cert: The CA certificate to use to verify the server for TLS connections.
441
+ tls_key: The client private key to identify the client for mTLS connections.
442
+ tls_cert must also be set.
443
+ tls_cert: The client certificate to identify the client for mTLS connections.
444
+ tls_key must also be set.
445
+ http_version: The HTTP version to use for requests. If unset, HTTP/1 is used for
446
+ plaintext and ALPN negotiates the version for TLS connections
447
+ which typically means HTTP/2 if the server supports it.
448
+ timeout: Default timeout for requests in seconds. This is the timeout from
449
+ the start of the request to the end of the response.
450
+ connect_timeout: Timeout for connection establishment in seconds.
451
+ read_timeout: Timeout for each read operation of a request in seconds.
452
+ pool_idle_timeout: Timeout for idle connections in the connection pool in seconds.
453
+ pool_max_idle_per_host: Maximum number of idle connections to keep in the pool per host.
454
+ Defaults to 2.
455
+ tcp_keepalive_interval: Interval for TCP keepalive probes in seconds.
456
+ enable_gzip: Whether to enable gzip decompression for responses.
457
+ enable_brotli: Whether to enable brotli decompression for responses.
458
+ enable_zstd: Whether to enable zstd decompression for responses.
459
+ use_system_dns: Whether to use the system DNS resolver. By default, pyqwest uses an
460
+ asynchronous DNS resolver implemented in Rust, but it can have different
461
+ behavior from system DNS in certain environments. Try enabling this option if
462
+ you have any DNS resolution issues.
463
+ """
464
+
465
+ def __aenter__(self) -> Awaitable[HTTPTransport]:
466
+ """Enters the context manager for the transport to automatically close it when
467
+ leaving.
468
+ """
469
+
470
+ def __aexit__(
471
+ self,
472
+ _exc_type: type[BaseException] | None,
473
+ _exc_value: BaseException | None,
474
+ _traceback: TracebackType | None,
475
+ ) -> Awaitable[None]:
476
+ """Exits the context manager for the transport, closing it."""
477
+
478
+ def execute(self, request: Request) -> Awaitable[Response]:
479
+ """Executes the given request, returning the response.
480
+
481
+ Args:
482
+ request: The request to execute.
483
+
484
+ Raises:
485
+ ConnectionError: If the connection fails.
486
+ TimeoutError: If the request times out.
487
+ ReadError: If an error occurs reading the response.
488
+ WriteError: If an error occurs writing the request.
489
+ """
490
+
491
+ def aclose(self) -> Awaitable[None]:
492
+ """Closes the transport, releasing any underlying resources."""
493
+
494
+ def get_default_transport() -> HTTPTransport:
495
+ """Returns the singleton default HTTP transport instance used by clients that do not
496
+ specify a transport.
497
+
498
+ The default transport is constructed as follows:
499
+ ```
500
+ HTTPTransport(
501
+ connect_timeout=30.0,
502
+ pool_idle_timeout=90.0,
503
+ pool_max_idle_per_host=2,
504
+ tcp_keepalive_interval=30.0,
505
+ enable_gzip: bool = True,
506
+ enable_brotli: bool = True,
507
+ enable_zstd: bool = True,
508
+ )
509
+ ```
510
+ """
511
+
512
+ class Request:
513
+ """An HTTP request."""
514
+
515
+ def __init__(
516
+ self,
517
+ method: str,
518
+ url: str,
519
+ headers: Headers | None = None,
520
+ content: bytes | AsyncIterator[bytes] | None = None,
521
+ ) -> None:
522
+ """Creates a new Request object.
523
+
524
+ Args:
525
+ method: The HTTP method.
526
+ url: The unencoded request URL.
527
+ headers: The request headers.
528
+ content: The request content.
529
+ """
530
+
531
+ @property
532
+ def method(self) -> str:
533
+ """Returns the HTTP method of the request."""
534
+
535
+ @property
536
+ def url(self) -> str:
537
+ """Returns the unencoded request URL."""
538
+
539
+ @property
540
+ def headers(self) -> Headers:
541
+ """Returns the request headers."""
542
+
543
+ @property
544
+ def content(self) -> AsyncIterator[bytes]:
545
+ """Returns an async iterator over the request content."""
546
+
547
+ class Response:
548
+ """An HTTP response."""
549
+
550
+ def __init__(
551
+ self,
552
+ *,
553
+ status: int,
554
+ http_version: HTTPVersion | None = None,
555
+ headers: Headers | None = None,
556
+ content: bytes | AsyncIterator[Buffer] | None = None,
557
+ trailers: Headers | None = None,
558
+ ) -> None:
559
+ """Creates a new Response object.
560
+
561
+ Care must be taken if your service uses trailers and you override content.
562
+ Trailers will not be received without fully consuming the original response content.
563
+ Patterns that wrap the original response content should not have any issue but if
564
+ you replace it completely and need trailers, make sure to still read and discard
565
+ the original content.
566
+
567
+ Args:
568
+ status: The HTTP status code of the response.
569
+ http_version: The HTTP version of the response.
570
+ headers: The response headers.
571
+ content: The response content.
572
+ trailers: The response trailers.
573
+ """
574
+
575
+ def __aenter__(self) -> Awaitable[Response]:
576
+ """Enters the context manager for the response to automatically close it when
577
+ leaving.
578
+
579
+ Note that if your code is guaranteed to fully consume the response content,
580
+ it is not necessary to explicitly close the response.
581
+ """
582
+
583
+ def __aexit__(
584
+ self,
585
+ _exc_type: type[BaseException] | None,
586
+ _exc_value: BaseException | None,
587
+ _traceback: TracebackType | None,
588
+ ) -> Awaitable[None]:
589
+ """Exits the context manager for the response, closing it."""
590
+
591
+ @property
592
+ def status(self) -> int:
593
+ """Returns the HTTP status code of the response."""
594
+
595
+ @property
596
+ def http_version(self) -> HTTPVersion:
597
+ """Returns the HTTP version of the response."""
598
+
599
+ @property
600
+ def headers(self) -> Headers:
601
+ """Returns the response headers."""
602
+
603
+ @property
604
+ def content(self) -> AsyncIterator[Buffer]:
605
+ """Returns an asynchronous iterator over the response content."""
606
+
607
+ @property
608
+ def trailers(self) -> Headers:
609
+ """Returns the response trailers.
610
+
611
+ Because trailers complete the response, this will only be filled after fully
612
+ consuming the content iterator.
613
+ """
614
+
615
+ def aclose(self) -> Awaitable[None]:
616
+ """Closes the response, releasing any underlying resources.
617
+
618
+ Note that if your code is guaranteed to fully consume the response content,
619
+ it is not necessary to explicitly close the response.
620
+ """
621
+
622
+ class SyncClient:
623
+ """A synchronous HTTP client.
624
+
625
+ A client is a lightweight wrapper around a SyncTransport, providing convenience methods
626
+ for common HTTP operations with buffering.
627
+ """
628
+
629
+ def __init__(self, transport: SyncTransport | None = None) -> None:
630
+ """Creates a new synchronous HTTP client.
631
+
632
+ Args:
633
+ transport: The transport to use for requests. If None, the shared default
634
+ transport will be used.
635
+ """
636
+
637
+ def get(
638
+ self,
639
+ url: str,
640
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
641
+ timeout: float | None = None,
642
+ ) -> FullResponse:
643
+ """Executes a GET HTTP request.
644
+
645
+ Args:
646
+ url: The unencoded request URL.
647
+ headers: The request headers.
648
+ timeout: The timeout for the request in seconds.
649
+
650
+ Raises:
651
+ ConnectionError: If the connection fails.
652
+ TimeoutError: If the request times out.
653
+ ReadError: If an error occurs reading the response.
654
+ WriteError: If an error occurs writing the request.
655
+ """
656
+
657
+ def post(
658
+ self,
659
+ url: str,
660
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
661
+ content: bytes | Iterable[bytes] | None = None,
662
+ timeout: float | None = None,
663
+ ) -> FullResponse:
664
+ """Executes a POST HTTP request.
665
+
666
+ Args:
667
+ url: The unencoded request URL.
668
+ headers: The request headers.
669
+ content: The request content.
670
+ timeout: The timeout for the request in seconds.
671
+
672
+ Raises:
673
+ ConnectionError: If the connection fails.
674
+ TimeoutError: If the request times out.
675
+ ReadError: If an error occurs reading the response.
676
+ WriteError: If an error occurs writing the request.
677
+ """
678
+
679
+ def delete(
680
+ self,
681
+ url: str,
682
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
683
+ timeout: float | None = None,
684
+ ) -> FullResponse:
685
+ """Executes a DELETE HTTP request.
686
+
687
+ Args:
688
+ url: The unencoded request URL.
689
+ headers: The request headers.
690
+ timeout: The timeout for the request in seconds.
691
+
692
+ Raises:
693
+ ConnectionError: If the connection fails.
694
+ TimeoutError: If the request times out.
695
+ ReadError: If an error occurs reading the response.
696
+ WriteError: If an error occurs writing the request.
697
+ """
698
+
699
+ def head(
700
+ self,
701
+ url: str,
702
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
703
+ timeout: float | None = None,
704
+ ) -> FullResponse:
705
+ """Executes a HEAD HTTP request.
706
+
707
+ Args:
708
+ url: The unencoded request URL.
709
+ headers: The request headers.
710
+ timeout: The timeout for the request in seconds.
711
+
712
+ Raises:
713
+ ConnectionError: If the connection fails.
714
+ TimeoutError: If the request times out.
715
+ ReadError: If an error occurs reading the response.
716
+ WriteError: If an error occurs writing the request.
717
+ """
718
+
719
+ def options(
720
+ self,
721
+ url: str,
722
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
723
+ timeout: float | None = None,
724
+ ) -> FullResponse:
725
+ """Executes a OPTIONS HTTP request.
726
+
727
+ Args:
728
+ url: The unencoded request URL.
729
+ headers: The request headers.
730
+ timeout: The timeout for the request in seconds.
731
+
732
+ Raises:
733
+ ConnectionError: If the connection fails.
734
+ TimeoutError: If the request times out.
735
+ ReadError: If an error occurs reading the response.
736
+ WriteError: If an error occurs writing the request.
737
+ """
738
+
739
+ def patch(
740
+ self,
741
+ url: str,
742
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
743
+ content: bytes | Iterable[bytes] | None = None,
744
+ timeout: float | None = None,
745
+ ) -> FullResponse:
746
+ """Executes a PATCH HTTP request.
747
+
748
+ Args:
749
+ url: The unencoded request URL.
750
+ headers: The request headers.
751
+ content: The request content.
752
+ timeout: The timeout for the request in seconds.
753
+
754
+ Raises:
755
+ ConnectionError: If the connection fails.
756
+ TimeoutError: If the request times out.
757
+ ReadError: If an error occurs reading the response.
758
+ WriteError: If an error occurs writing the request.
759
+ """
760
+
761
+ def put(
762
+ self,
763
+ url: str,
764
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
765
+ content: bytes | Iterable[bytes] | None = None,
766
+ timeout: float | None = None,
767
+ ) -> FullResponse:
768
+ """Executes a PUT HTTP request.
769
+
770
+ Args:
771
+ url: The unencoded request URL.
772
+ headers: The request headers.
773
+ content: The request content.
774
+ timeout: The timeout for the request in seconds.
775
+
776
+ Raises:
777
+ ConnectionError: If the connection fails.
778
+ TimeoutError: If the request times out.
779
+ ReadError: If an error occurs reading the response.
780
+ WriteError: If an error occurs writing the request.
781
+ """
782
+
783
+ def execute(
784
+ self,
785
+ method: str,
786
+ url: str,
787
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
788
+ content: bytes | Iterable[bytes] | None = None,
789
+ timeout: float | None = None,
790
+ ) -> FullResponse:
791
+ """Executes an HTTP request, returning the full buffered response.
792
+
793
+ Args:
794
+ method: The HTTP method.
795
+ url: The unencoded request URL.
796
+ headers: The request headers.
797
+ content: The request content.
798
+ timeout: The timeout for the request in seconds.
799
+
800
+ Raises:
801
+ ConnectionError: If the connection fails.
802
+ TimeoutError: If the request times out.
803
+ ReadError: If an error occurs reading the response.
804
+ WriteError: If an error occurs writing the request.
805
+ """
806
+
807
+ def stream(
808
+ self,
809
+ method: str,
810
+ url: str,
811
+ headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
812
+ content: bytes | Iterable[bytes] | None = None,
813
+ timeout: float | None = None,
814
+ ) -> AbstractContextManager[SyncResponse]:
815
+ """Executes an HTTP request, allowing the response content to be streamed.
816
+
817
+ Args:
818
+ method: The HTTP method.
819
+ url: The unencoded request URL.
820
+ headers: The request headers.
821
+ content: The request content.
822
+ timeout: The timeout for the request in seconds.
823
+
824
+ Raises:
825
+ ConnectionError: If the connection fails.
826
+ TimeoutError: If the request times out.
827
+ ReadError: If an error occurs reading the response.
828
+ WriteError: If an error occurs writing the request.
829
+ """
830
+
831
+ @runtime_checkable
832
+ class SyncTransport(Protocol):
833
+ """Protocol for synchronous HTTP transport implementations.
834
+
835
+ The default implementation of SyncTransport is SyncHTTPTransport which issues requests.
836
+ Custom implementations may be useful to:
837
+
838
+ - Mock requests for testing.
839
+ - Add middleware wrapping transports
840
+ """
841
+
842
+ def execute_sync(self, request: SyncRequest) -> SyncResponse:
843
+ """Executes a request."""
844
+
845
+ class SyncHTTPTransport:
846
+ """An HTTP transport implementation using reqwest."""
847
+
848
+ def __init__(
849
+ self,
850
+ *,
851
+ tls_ca_cert: bytes | None = None,
852
+ tls_key: bytes | None = None,
853
+ tls_cert: bytes | None = None,
854
+ http_version: HTTPVersion | None = None,
855
+ timeout: float | None = None,
856
+ connect_timeout: float | None = None,
857
+ read_timeout: float | None = None,
858
+ pool_idle_timeout: float | None = None,
859
+ pool_max_idle_per_host: int | None = None,
860
+ tcp_keepalive_interval: float | None = None,
861
+ enable_gzip: bool = False,
862
+ enable_brotli: bool = False,
863
+ enable_zstd: bool = False,
864
+ use_system_dns: bool = False,
865
+ ) -> None:
866
+ """Creates a new SyncHTTPTransport object.
867
+
868
+ Without any arguments, the transport behaves like a raw, low-level HTTP transport,
869
+ with no timeouts or other higher level behavior. When creating a transport, take care
870
+ to set options to meet your needs. Also consider using get_default_transport instead
871
+ which is preconfigured with reasonable defaults, though does not support custom TLS
872
+ certificates.
873
+
874
+ Args:
875
+ tls_ca_cert: The CA certificate to use to verify the server for TLS connections.
876
+ tls_key: The client private key to identify the client for mTLS connections.
877
+ tls_cert must also be set.
878
+ tls_cert: The client certificate to identify the client for mTLS connections.
879
+ tls_key must also be set.
880
+ http_version: The HTTP version to use for requests. If unset, HTTP/1 is used for
881
+ plaintext and ALPN negotiates the version for TLS connections
882
+ which typically means HTTP/2 if the server supports it.
883
+ timeout: Default timeout for requests in seconds. This is the timeout from
884
+ the start of the request to the end of the response.
885
+ connect_timeout: Timeout for connection establishment in seconds.
886
+ read_timeout: Timeout for each read operation of a request in seconds.
887
+ pool_idle_timeout: Timeout for idle connections in the connection pool in seconds.
888
+ pool_max_idle_per_host: Maximum number of idle connections to keep in the pool per host.
889
+ Defaults to 2.
890
+ tcp_keepalive_interval: Interval for TCP keepalive probes in seconds.
891
+ enable_gzip: Whether to enable gzip decompression for responses.
892
+ enable_brotli: Whether to enable brotli decompression for responses.
893
+ enable_zstd: Whether to enable zstd decompression for responses.
894
+ use_system_dns: Whether to use the system DNS resolver. By default, pyqwest uses an
895
+ asynchronous DNS resolver implemented in Rust, but it can have different
896
+ behavior from system DNS in certain environments. Try enabling this option if
897
+ you have any DNS resolution issues.
898
+ """
899
+
900
+ def __enter__(self) -> SyncHTTPTransport:
901
+ """Enters the context manager for the transport to automatically
902
+ close it when leaving.
903
+ """
904
+
905
+ def __exit__(
906
+ self,
907
+ _exc_type: type[BaseException] | None,
908
+ _exc_value: BaseException | None,
909
+ _traceback: TracebackType | None,
910
+ ) -> None:
911
+ """Exits the context manager for the transport, closing it."""
912
+
913
+ def execute_sync(self, request: SyncRequest) -> SyncResponse:
914
+ """Executes the given request, returning the response.
915
+
916
+ Args:
917
+ request: The request to execute.
918
+ """
919
+
920
+ def close(self) -> None:
921
+ """Closes the transport, releasing any underlying resources."""
922
+
923
+ def get_default_sync_transport() -> SyncHTTPTransport:
924
+ """Returns the singleton default HTTP transport instance used by synchronous clients that do not
925
+ specify a transport.ult HTTP transport instance used by clients that do not
926
+ specify a transport.
927
+
928
+ The default transport is constructed as follows:
929
+ ```
930
+ SyncHTTPTransport(
931
+ connect_timeout=30.0,
932
+ pool_idle_timeout=90.0,
933
+ pool_max_idle_per_host=2,
934
+ tcp_keepalive_interval=30.0,
935
+ enable_gzip: bool = True,
936
+ enable_brotli: bool = True,
937
+ enable_zstd: bool = True,
938
+ )
939
+ ```
940
+ """
941
+
942
+ class SyncRequest:
943
+ """An HTTP request."""
944
+
945
+ def __init__(
946
+ self,
947
+ method: str,
948
+ url: str,
949
+ headers: Headers | None = None,
950
+ content: bytes | Iterable[bytes] | None = None,
951
+ ) -> None:
952
+ """Creates a new SyncRequest object.
953
+
954
+ Args:
955
+ method: The HTTP method.
956
+ url: The unencoded request URL.
957
+ headers: The request headers.
958
+ content: The request content.
959
+ timeout: The timeout for the request in seconds.
960
+ """
961
+
962
+ @property
963
+ def method(self) -> str:
964
+ """Returns the HTTP method of the request."""
965
+
966
+ @property
967
+ def url(self) -> str:
968
+ """Returns the unencoded request URL."""
969
+
970
+ @property
971
+ def headers(self) -> Headers:
972
+ """Returns the request headers."""
973
+
974
+ @property
975
+ def content(self) -> Iterator[bytes]:
976
+ """Returns an iterator over the request content."""
977
+
978
+ class SyncResponse:
979
+ """An HTTP response."""
980
+
981
+ def __init__(
982
+ self,
983
+ *,
984
+ status: int,
985
+ http_version: HTTPVersion | None = None,
986
+ headers: Headers | None = None,
987
+ content: bytes | Iterable[Buffer] | None = None,
988
+ trailers: Headers | None = None,
989
+ ) -> None:
990
+ """Creates a new SyncResponse object.
991
+
992
+ Care must be taken if your service uses trailers and you override content.
993
+ Trailers will not be received without fully consuming the original response content.
994
+ Patterns that wrap the original response content should not have any issue but if
995
+ you replace it completely and need trailers, make sure to still read and discard
996
+ the original content.
997
+
998
+ Args:
999
+ status: The HTTP status code of the response.
1000
+ http_version: The HTTP version of the response.
1001
+ headers: The response headers.
1002
+ content: The response content.
1003
+ trailers: The response trailers.
1004
+ """
1005
+
1006
+ def __enter__(self) -> SyncResponse:
1007
+ """Enters the context manager for the response to automatically
1008
+ close it when leaving.
1009
+
1010
+ Note that if your code is guaranteed to fully consume the response content,
1011
+ it is not necessary to explicitly close the response.
1012
+ """
1013
+
1014
+ def __exit__(
1015
+ self,
1016
+ _exc_type: type[BaseException] | None,
1017
+ _exc_value: BaseException | None,
1018
+ _traceback: TracebackType | None,
1019
+ ) -> None:
1020
+ """Exits the context manager for the response, closing it."""
1021
+
1022
+ @property
1023
+ def status(self) -> int:
1024
+ """Returns the HTTP status code of the response."""
1025
+
1026
+ @property
1027
+ def http_version(self) -> HTTPVersion:
1028
+ """Returns the HTTP version of the response."""
1029
+
1030
+ @property
1031
+ def headers(self) -> Headers:
1032
+ """Returns the response headers."""
1033
+
1034
+ @property
1035
+ def content(self) -> Iterator[Buffer]:
1036
+ """Returns an iterator over the response content."""
1037
+
1038
+ @property
1039
+ def trailers(self) -> Headers:
1040
+ """Returns the response trailers.
1041
+
1042
+ Because trailers complete the response, this will only be filled after fully
1043
+ consuming the content iterator.
1044
+ """
1045
+
1046
+ def close(self) -> None:
1047
+ """Closes the response, releasing any underlying resources.
1048
+
1049
+ Note that if your code is guaranteed to fully consume the response content,
1050
+ it is not necessary to explicitly close the response.
1051
+ """
1052
+
1053
+ class FullResponse:
1054
+ """A fully buffered HTTP response."""
1055
+
1056
+ def __init__(
1057
+ self, status: int, headers: Headers, content: bytes, trailers: Headers
1058
+ ) -> None:
1059
+ """Creates a new FullResponse object.
1060
+
1061
+ Args:
1062
+ status: The HTTP status code of the response.
1063
+ headers: The response headers.
1064
+ content: The response content.
1065
+ trailers: The response trailers.
1066
+ """
1067
+
1068
+ @property
1069
+ def status(self) -> int:
1070
+ """Returns the HTTP status code of the response."""
1071
+
1072
+ @property
1073
+ def headers(self) -> Headers:
1074
+ """Returns the response headers."""
1075
+
1076
+ @property
1077
+ def content(self) -> bytes:
1078
+ """Returns the response content."""
1079
+
1080
+ @property
1081
+ def trailers(self) -> Headers:
1082
+ """Returns the response trailers."""
1083
+
1084
+ def text(self) -> str:
1085
+ """Returns the response content decoded as text.
1086
+
1087
+ The encoding for decoding is determined from the content-type header if present,
1088
+ defaulting to UTF-8 otherwise.
1089
+ """
1090
+
1091
+ def json(self) -> JSON:
1092
+ """Parses and returns the response content as JSON.
1093
+
1094
+ The content-type header is not checked when using this method.
1095
+ """
1096
+
1097
+ class StreamErrorCode:
1098
+ NO_ERROR: StreamErrorCode
1099
+ PROTOCOL_ERROR: StreamErrorCode
1100
+ INTERNAL_ERROR: StreamErrorCode
1101
+ FLOW_CONTROL_ERROR: StreamErrorCode
1102
+ SETTINGS_TIMEOUT: StreamErrorCode
1103
+ STREAM_CLOSED: StreamErrorCode
1104
+ FRAME_SIZE_ERROR: StreamErrorCode
1105
+ REFUSED_STREAM: StreamErrorCode
1106
+ CANCEL: StreamErrorCode
1107
+ COMPRESSION_ERROR: StreamErrorCode
1108
+ CONNECT_ERROR: StreamErrorCode
1109
+ ENHANCE_YOUR_CALM: StreamErrorCode
1110
+ INADEQUATE_SECURITY: StreamErrorCode
1111
+ HTTP_1_1_REQUIRED: StreamErrorCode
1112
+
1113
+ class StreamError(Exception):
1114
+ """An error representing an HTTP/2+ stream error."""
1115
+
1116
+ def __init__(self, message: str, code: StreamErrorCode) -> None:
1117
+ """Creates a new StreamError.
1118
+
1119
+ Args:
1120
+ message: The error message.
1121
+ code: The stream error code.
1122
+ """
1123
+
1124
+ @property
1125
+ def code(self) -> StreamErrorCode:
1126
+ """The stream error code."""
1127
+
1128
+ class ReadError(Exception):
1129
+ """An error representing a read error during response reading."""
1130
+
1131
+ class WriteError(Exception):
1132
+ """An error representing a write error during request sending."""