_pyqwest.pyi 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  1. import datetime
  2. from collections.abc import (
  3. AsyncIterator,
  4. Awaitable,
  5. ItemsView,
  6. Iterable,
  7. Iterator,
  8. KeysView,
  9. Mapping,
  10. Sequence,
  11. ValuesView,
  12. )
  13. from contextlib import AbstractContextManager
  14. from types import TracebackType
  15. from typing import (
  16. Literal,
  17. Protocol,
  18. TypeAlias,
  19. TypeVar,
  20. final,
  21. overload,
  22. runtime_checkable,
  23. )
  24. from opentelemetry.metrics import MeterProvider
  25. from opentelemetry.trace import TracerProvider
  26. from ._multipart import Multipart, SyncMultipart
  27. _T = TypeVar("_T")
  28. _JSON: TypeAlias = (
  29. Mapping[str, _JSON] | Sequence[_JSON] | str | int | float | bool | None
  30. )
  31. _RequestContent: TypeAlias = (
  32. bytes | AsyncIterator[bytes] | Mapping[str, _JSON] | Multipart
  33. )
  34. _SyncRequestContent: TypeAlias = (
  35. bytes | Iterable[bytes] | Mapping[str, _JSON] | SyncMultipart
  36. )
  37. _Buffer: TypeAlias = bytes | memoryview | bytearray
  38. _QueryParams: TypeAlias = dict[str, str | None] | Iterable[tuple[str, str | None]]
  39. @final
  40. class Headers:
  41. """Container of HTTP headers.
  42. This class behaves like a dictionary with case-insensitive keys and
  43. string values. Standard dictionary access will act as if keys can only
  44. have a single value. The add method can be used to It additionally can be used to store
  45. multiple values for the same key by using the add method. Iterating over
  46. values or items will return all values, including duplicates.
  47. """
  48. def __init__(
  49. self,
  50. items: Mapping[str | HTTPHeaderName, str]
  51. | Iterable[tuple[str | HTTPHeaderName, str]]
  52. | None = None,
  53. ) -> None:
  54. """Creates a new Headers object.
  55. Args:
  56. items: Initial headers to add.
  57. """
  58. def __getitem__(self, key: str | HTTPHeaderName) -> str:
  59. """Return the header value for the key.
  60. If multiple values are present for the key, returns the first value.
  61. Args:
  62. key: The header name.
  63. Raises:
  64. KeyError: If the key is not present.
  65. """
  66. def __setitem__(self, key: str | HTTPHeaderName, value: str) -> None:
  67. """Sets the header value for the key, replacing any existing values.
  68. Args:
  69. key: The header name.
  70. value: The header value.
  71. """
  72. def __delitem__(self, key: str | HTTPHeaderName) -> None:
  73. """Deletes all values for the key.
  74. Args:
  75. key: The header name.
  76. Raises:
  77. KeyError: If the key is not present.
  78. """
  79. def __iter__(self) -> Iterator[str]:
  80. """Returns an iterator over the header names."""
  81. def __len__(self) -> int:
  82. """Returns the number of unique header names."""
  83. def __eq__(self, other: object) -> bool:
  84. """Compares the headers for equality with another Headers object,
  85. mapping, or iterable of key-value pairs.
  86. Args:
  87. other: The object to compare against.
  88. """
  89. def get(self, key: str | HTTPHeaderName, default: _T | None = None) -> str | _T:
  90. """Returns the header value for the key, or default if not present.
  91. Args:
  92. key: The header name.
  93. default: The default value to return if the key is not present.
  94. """
  95. @overload
  96. def pop(self, key: str | HTTPHeaderName) -> str:
  97. """Removes and returns the header value for the key.
  98. Args:
  99. key: The header name.
  100. Raises:
  101. KeyError: If the key is not present.
  102. """
  103. @overload
  104. def pop(self, key: str | HTTPHeaderName, default: _T) -> str | _T:
  105. """Removes and returns the header value for the key, or default if not present.
  106. Args:
  107. key: The header name.
  108. default: The default value to return if the key is not present.
  109. """
  110. def popitem(self) -> tuple[str, str]:
  111. """Removes and returns an arbitrary (name, value) pair. Will return the same
  112. name multiple times if it has multiple values.
  113. Raises:
  114. KeyError: If the headers are empty.
  115. """
  116. def setdefault(self, key: str | HTTPHeaderName, default: str | None = None) -> str:
  117. """If the key is not present, sets it to the default value.
  118. Returns the value for the key.
  119. Args:
  120. key: The header name.
  121. default: The default value to set and return if the key is not present.
  122. """
  123. def add(self, key: str | HTTPHeaderName, value: str) -> None:
  124. """Adds a header value for the key. Existing values are preserved.
  125. Args:
  126. key: The header name.
  127. value: The header value.
  128. """
  129. @overload
  130. def update(self, **kwargs: str) -> None:
  131. """Updates headers from keyword arguments. Existing values are replaced.
  132. Args:
  133. **kwargs: Header names and values to set.
  134. """
  135. @overload
  136. def update(
  137. self,
  138. items: Mapping[str | HTTPHeaderName, str]
  139. | Iterable[tuple[str | HTTPHeaderName, str]],
  140. /,
  141. **kwargs: str,
  142. ) -> None:
  143. """Updates headers with the provided items. Existing values are replaced.
  144. Args:
  145. items: Header names and values to set.
  146. **kwargs: Additional header names and values to set after items. May overwrite items.
  147. """
  148. def clear(self) -> None:
  149. """Removes all headers."""
  150. def getall(self, key: str | HTTPHeaderName) -> Sequence[str]:
  151. """Returns all header values for the key.
  152. Args:
  153. key: The header name.
  154. """
  155. def items(self) -> ItemsView[str, str]:
  156. """Returns a new view of all header name-value pairs, including duplicates."""
  157. def keys(self) -> KeysView[str]:
  158. """Returns a new view of all unique header names."""
  159. def values(self) -> ValuesView[str]:
  160. """Returns a new view of all header values, including duplicates."""
  161. def __contains__(self, key: object) -> bool:
  162. """Returns True if the header name is present.
  163. Args:
  164. key: The header name.
  165. """
  166. @final
  167. class HTTPVersion:
  168. """An enumeration of HTTP versions."""
  169. HTTP1: HTTPVersion
  170. """HTTP/1.1"""
  171. HTTP2: HTTPVersion
  172. """HTTP/2"""
  173. HTTP3: HTTPVersion
  174. """HTTP/3"""
  175. def __eq__(self, other: object) -> bool: ...
  176. def __ne__(self, other: object) -> bool: ...
  177. def __lt__(self, other: object) -> bool: ...
  178. def __le__(self, other: object) -> bool: ...
  179. def __gt__(self, other: object) -> bool: ...
  180. def __ge__(self, other: object) -> bool: ...
  181. @final
  182. class Client:
  183. def __init__(self, transport: Transport | None = None) -> None:
  184. """Creates a new asynchronous HTTP client.
  185. The asynchronous client does not expose per-request timeouts on its methods.
  186. Use `asyncio.wait_for` or similar to enforce timeouts on requests.
  187. Args:
  188. transport: The transport to use for requests. If None, the shared default
  189. transport will be used.
  190. """
  191. def get(
  192. self,
  193. url: str,
  194. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  195. *,
  196. params: _QueryParams | None = None,
  197. ) -> Awaitable[FullResponse]:
  198. """Executes a GET HTTP request.
  199. Args:
  200. url: The unencoded request URL.
  201. headers: The request headers.
  202. params: Query parameters to append to the URL. None values will be treated as key-only.
  203. Raises:
  204. ConnectionError: If the connection fails.
  205. TimeoutError: If the request times out.
  206. RemoteProtocolError: If the peer violates the HTTP protocol.
  207. ReadError: If an error occurs reading the response.
  208. WriteError: If an error occurs writing the request.
  209. """
  210. def post(
  211. self,
  212. url: str,
  213. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  214. content: _RequestContent | None = None,
  215. *,
  216. params: _QueryParams | None = None,
  217. ) -> Awaitable[FullResponse]:
  218. """Executes a POST HTTP request.
  219. Args:
  220. url: The unencoded request URL.
  221. headers: The request headers.
  222. content: The request content. A Python dictionary will be converted
  223. to JSON and a Multipart will be sent as a multipart form.
  224. params: Query parameters to append to the URL. None values will be treated as key-only.
  225. Raises:
  226. ConnectionError: If the connection fails.
  227. TimeoutError: If the request times out.
  228. RemoteProtocolError: If the peer violates the HTTP protocol.
  229. ReadError: If an error occurs reading the response.
  230. WriteError: If an error occurs writing the request.
  231. """
  232. def delete(
  233. self,
  234. url: str,
  235. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  236. *,
  237. params: _QueryParams | None = None,
  238. ) -> Awaitable[FullResponse]:
  239. """Executes a DELETE HTTP request.
  240. Args:
  241. url: The unencoded request URL.
  242. headers: The request headers.
  243. params: Query parameters to append to the URL. None values will be treated as key-only.
  244. Raises:
  245. ConnectionError: If the connection fails.
  246. TimeoutError: If the request times out.
  247. RemoteProtocolError: If the peer violates the HTTP protocol.
  248. ReadError: If an error occurs reading the response.
  249. WriteError: If an error occurs writing the request.
  250. """
  251. def head(
  252. self,
  253. url: str,
  254. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  255. *,
  256. params: _QueryParams | None = None,
  257. ) -> Awaitable[FullResponse]:
  258. """Executes a HEAD HTTP request.
  259. Args:
  260. url: The unencoded request URL.
  261. headers: The request headers.
  262. params: Query parameters to append to the URL. None values will be treated as key-only.
  263. Raises:
  264. ConnectionError: If the connection fails.
  265. TimeoutError: If the request times out.
  266. RemoteProtocolError: If the peer violates the HTTP protocol.
  267. ReadError: If an error occurs reading the response.
  268. WriteError: If an error occurs writing the request.
  269. """
  270. def options(
  271. self,
  272. url: str,
  273. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  274. *,
  275. params: _QueryParams | None = None,
  276. ) -> Awaitable[FullResponse]:
  277. """Executes a OPTIONS HTTP request.
  278. Args:
  279. url: The unencoded request URL.
  280. headers: The request headers.
  281. params: Query parameters to append to the URL. None values will be treated as key-only.
  282. Raises:
  283. ConnectionError: If the connection fails.
  284. TimeoutError: If the request times out.
  285. RemoteProtocolError: If the peer violates the HTTP protocol.
  286. ReadError: If an error occurs reading the response.
  287. WriteError: If an error occurs writing the request.
  288. """
  289. def patch(
  290. self,
  291. url: str,
  292. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  293. content: _RequestContent | None = None,
  294. *,
  295. params: _QueryParams | None = None,
  296. ) -> Awaitable[FullResponse]:
  297. """Executes a PATCH HTTP request.
  298. Args:
  299. url: The unencoded request URL.
  300. headers: The request headers.
  301. content: The request content. A Python dictionary will be converted
  302. to JSON and a Multipart will be sent as a multipart form.
  303. params: Query parameters to append to the URL. None values will be treated as key-only.
  304. Raises:
  305. ConnectionError: If the connection fails.
  306. TimeoutError: If the request times out.
  307. RemoteProtocolError: If the peer violates the HTTP protocol.
  308. ReadError: If an error occurs reading the response.
  309. WriteError: If an error occurs writing the request.
  310. """
  311. def put(
  312. self,
  313. url: str,
  314. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  315. content: _RequestContent | None = None,
  316. *,
  317. params: _QueryParams | None = None,
  318. ) -> Awaitable[FullResponse]:
  319. """Executes a PUT HTTP request.
  320. Args:
  321. url: The unencoded request URL.
  322. headers: The request headers.
  323. content: The request content. A Python dictionary will be converted
  324. to JSON and a Multipart will be sent as a multipart form.
  325. params: Query parameters to append to the URL. None values will be treated as key-only.
  326. Raises:
  327. ConnectionError: If the connection fails.
  328. TimeoutError: If the request times out.
  329. RemoteProtocolError: If the peer violates the HTTP protocol.
  330. ReadError: If an error occurs reading the response.
  331. WriteError: If an error occurs writing the request.
  332. """
  333. def execute(
  334. self,
  335. method: str,
  336. url: str,
  337. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  338. content: _RequestContent | None = None,
  339. *,
  340. params: _QueryParams | None = None,
  341. ) -> Awaitable[FullResponse]:
  342. """Executes an HTTP request, returning the full buffered response.
  343. Args:
  344. method: The HTTP method.
  345. url: The unencoded request URL.
  346. headers: The request headers.
  347. content: The request content. A Python dictionary will be converted
  348. to JSON and a Multipart will be sent as a multipart form.
  349. params: Query parameters to append to the URL. None values will be treated as key-only.
  350. Raises:
  351. ConnectionError: If the connection fails.
  352. TimeoutError: If the request times out.
  353. RemoteProtocolError: If the peer violates the HTTP protocol.
  354. ReadError: If an error occurs reading the response.
  355. WriteError: If an error occurs writing the request.
  356. """
  357. def stream(
  358. self,
  359. method: str,
  360. url: str,
  361. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  362. content: _RequestContent | None = None,
  363. *,
  364. params: _QueryParams | None = None,
  365. ) -> Awaitable[Response]:
  366. """Executes an HTTP request, allowing the response content to be streamed.
  367. Args:
  368. method: The HTTP method.
  369. url: The unencoded request URL.
  370. headers: The request headers.
  371. content: The request content. A Python dictionary will be converted
  372. to JSON and a Multipart will be sent as a multipart form.
  373. params: Query parameters to append to the URL. None values will be treated as key-only.
  374. Raises:
  375. ConnectionError: If the connection fails.
  376. TimeoutError: If the request times out.
  377. RemoteProtocolError: If the peer violates the HTTP protocol.
  378. ReadError: If an error occurs reading the response.
  379. WriteError: If an error occurs writing the request.
  380. """
  381. class Proxy:
  382. """A proxy for transports to route requests through.
  383. In addition to authentication and extra headers to send to the proxy,
  384. it allows restricting the requests routed through the proxy by URL
  385. scheme or exclusion list.
  386. """
  387. def __init__(
  388. self,
  389. url: str,
  390. *,
  391. auth: tuple[str, str] | None = None,
  392. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  393. no_proxy: str | None = None,
  394. scheme: Literal["http", "https"] | None = None,
  395. ) -> None:
  396. """Creates a new Proxy object.
  397. Args:
  398. url: The URL of the proxy, for example "http://localhost:8030".
  399. The URL scheme may be http, https, socks5, or socks5h.
  400. Credentials in the URL, for example
  401. "http://user:pass@localhost:8030", will be used for proxy
  402. authentication.
  403. auth: A (username, password) tuple to use for basic proxy
  404. authentication, as an alternative to credentials in the URL.
  405. headers: Extra headers to send to the proxy.
  406. no_proxy: A comma-separated list of hosts that should not be proxied.
  407. Entries may be IP addresses, optionally with a subnet mask
  408. such as "192.168.1.0/24", or domain names which also match
  409. all subdomains. The entry "*" matches all hosts.
  410. scheme: Which request URL scheme to route through the proxy. By default,
  411. both http and https requests are proxied.
  412. """
  413. @runtime_checkable
  414. class Transport(Protocol):
  415. """Protocol for asynchronous HTTP transport implementations.
  416. The default implementation of Transport is HTTPTransport which issues requests.
  417. Custom implementations may be useful to:
  418. - Mock requests for testing.
  419. - Add middleware wrapping transports
  420. """
  421. def execute(self, request: Request) -> Awaitable[Response]:
  422. """Executes a request."""
  423. @final
  424. class HTTPTransport:
  425. """An HTTP transport implementation using reqwest."""
  426. def __init__(
  427. self,
  428. *,
  429. tls_ca_cert: bytes | None = None,
  430. tls_include_system_certs: bool = False,
  431. tls_key: bytes | None = None,
  432. tls_cert: bytes | None = None,
  433. http_version: HTTPVersion | None = None,
  434. proxy: str | Proxy | Sequence[str | Proxy] | None = None,
  435. timeout: float | None = None,
  436. connect_timeout: float | None = 30.0,
  437. read_timeout: float | None = None,
  438. pool_idle_timeout: float | None = 90.0,
  439. pool_max_idle_per_host: int | None = None,
  440. tcp_keepalive_interval: float | None = 30.0,
  441. enable_gzip: bool = True,
  442. enable_brotli: bool = True,
  443. enable_zstd: bool = True,
  444. use_system_dns: bool = False,
  445. enable_cookie_store: bool = False,
  446. follow_redirects: bool = True,
  447. max_redirects: int = 10,
  448. enable_otel: bool = True,
  449. meter_provider: MeterProvider | None = None,
  450. tracer_provider: TracerProvider | None = None,
  451. ) -> None:
  452. """Creates a new HTTPTransport object.
  453. Without any arguments, the transport behaves like the default transport without trusted TLS certificates.
  454. When creating a transport, take care to set options to meet your needs.
  455. Args:
  456. tls_ca_cert: The CA certificate to use to verify the server for TLS connections.
  457. tls_include_system_certs: Whether to include the system CA certificates to verify TLS connections.
  458. If this is unset and tls_ca_cert is not provided, TLS will not function.
  459. tls_key: The client private key to identify the client for mTLS connections.
  460. tls_cert must also be set.
  461. tls_cert: The client certificate to identify the client for mTLS connections.
  462. tls_key must also be set.
  463. http_version: The HTTP version to use for requests. If unset, HTTP/1 is used for
  464. plaintext and ALPN negotiates the version for TLS connections
  465. which typically means HTTP/2 if the server supports it.
  466. proxy: A proxy to send requests through. A URL string such as
  467. "http://localhost:8030" proxies all requests, equivalent to
  468. Proxy(url). Pass a Proxy object to configure authentication,
  469. extra headers, or routing rules, or a sequence of them to
  470. apply multiple proxy rules, where the first matching proxy
  471. is used for each request. An empty sequence, like None,
  472. configures no explicit proxy, in which case proxy
  473. environment variables such as HTTP_PROXY still apply.
  474. timeout: Default timeout for requests in seconds. This is the timeout from
  475. the start of the request to the end of the response.
  476. connect_timeout: Timeout for connection establishment in seconds.
  477. read_timeout: Timeout for each read operation of a request in seconds.
  478. pool_idle_timeout: Timeout for idle connections in the connection pool in seconds.
  479. pool_max_idle_per_host: Maximum number of idle connections to keep in the pool per host.
  480. Defaults to 2.
  481. tcp_keepalive_interval: Interval for TCP keepalive probes in seconds.
  482. enable_gzip: Whether to enable gzip decompression for responses.
  483. enable_brotli: Whether to enable brotli decompression for responses.
  484. enable_zstd: Whether to enable zstd decompression for responses.
  485. use_system_dns: Whether to use the system DNS resolver. By default, pyqwest uses an
  486. asynchronous DNS resolver implemented in Rust, but it can have different
  487. behavior from system DNS in certain environments. Try enabling this option if
  488. you have any DNS resolution issues.
  489. enable_cookie_store: Whether to enable automatic cookie storage and sending. When enabled,
  490. the transport will automatically store cookies from responses and send
  491. them with subsequent requests.
  492. follow_redirects: Whether to automatically follow redirect responses. When disabled,
  493. which is the default, redirect responses are returned as-is.
  494. Leave this disabled when the transport is used through
  495. pyqwest.httpx, because httpx clients apply their own
  496. follow_redirects setting and track redirects in response.history.
  497. max_redirects: Maximum number of redirects to follow when follow_redirects is enabled.
  498. A request exceeding it fails with TooManyRedirects.
  499. """
  500. def __aenter__(self) -> Awaitable[HTTPTransport]:
  501. """Enters the context manager for the transport to automatically close it when
  502. leaving.
  503. """
  504. def __aexit__(
  505. self,
  506. _exc_type: type[BaseException] | None,
  507. _exc_value: BaseException | None,
  508. _traceback: TracebackType | None,
  509. ) -> Awaitable[None]:
  510. """Exits the context manager for the transport, closing it."""
  511. def execute(self, request: Request) -> Awaitable[Response]:
  512. """Executes the given request, returning the response.
  513. Args:
  514. request: The request to execute.
  515. Raises:
  516. ConnectionError: If the connection fails.
  517. TimeoutError: If the request times out.
  518. RemoteProtocolError: If the peer violates the HTTP protocol.
  519. ReadError: If an error occurs reading the response.
  520. WriteError: If an error occurs writing the request.
  521. """
  522. def aclose(self) -> Awaitable[None]:
  523. """Closes the transport, releasing any underlying resources."""
  524. def get_default_transport() -> HTTPTransport:
  525. """Returns the singleton default HTTP transport instance used by clients that do not
  526. specify a transport.
  527. The default transport is constructed as follows:
  528. ```
  529. HTTPTransport(
  530. connect_timeout=30.0,
  531. pool_idle_timeout=90.0,
  532. tcp_keepalive_interval=30.0,
  533. enable_gzip: bool = True,
  534. enable_brotli: bool = True,
  535. enable_zstd: bool = True,
  536. )
  537. ```
  538. """
  539. @final
  540. class Request:
  541. """An HTTP request."""
  542. def __init__(
  543. self,
  544. method: str,
  545. url: str,
  546. headers: Headers | None = None,
  547. content: _RequestContent | None = None,
  548. *,
  549. params: _QueryParams | None = None,
  550. ) -> None:
  551. """Creates a new Request object.
  552. Args:
  553. method: The HTTP method.
  554. url: The unencoded request URL.
  555. headers: The request headers.
  556. content: The request content. A Python dictionary will be converted
  557. to JSON and a Multipart will be sent as a multipart form.
  558. params: Query parameters to append to the URL. None values will be treated as key-only.
  559. """
  560. @property
  561. def method(self) -> str:
  562. """Returns the HTTP method of the request."""
  563. @property
  564. def url(self) -> str:
  565. """Returns the unencoded request URL."""
  566. @property
  567. def headers(self) -> Headers:
  568. """Returns the request headers."""
  569. @property
  570. def content(self) -> bytes | AsyncIterator[bytes]:
  571. """Returns an async iterator over the request content."""
  572. @property
  573. def _json(self) -> bool: ...
  574. @final
  575. class Response:
  576. """An HTTP response."""
  577. def __init__(
  578. self,
  579. *,
  580. status: int,
  581. http_version: HTTPVersion | None = None,
  582. headers: Headers | None = None,
  583. content: bytes | AsyncIterator[_Buffer] | None = None,
  584. trailers: Headers | None = None,
  585. ) -> None:
  586. """Creates a new Response object.
  587. Care must be taken if your service uses trailers and you override content.
  588. Trailers will not be received without fully consuming the original response content.
  589. Patterns that wrap the original response content should not have any issue but if
  590. you replace it completely and need trailers, make sure to still read and discard
  591. the original content.
  592. Args:
  593. status: The HTTP status code of the response.
  594. http_version: The HTTP version of the response.
  595. headers: The response headers.
  596. content: The response content.
  597. trailers: The response trailers.
  598. Raises:
  599. RemoteProtocolError: If the status is not a valid HTTP status code.
  600. """
  601. def __aenter__(self) -> Awaitable[Response]:
  602. """Enters the context manager for the response to automatically close it when
  603. leaving.
  604. Note that if your code is guaranteed to fully consume the response content,
  605. it is not necessary to explicitly close the response.
  606. """
  607. def __aexit__(
  608. self,
  609. _exc_type: type[BaseException] | None,
  610. _exc_value: BaseException | None,
  611. _traceback: TracebackType | None,
  612. ) -> Awaitable[None]:
  613. """Exits the context manager for the response, closing it."""
  614. @property
  615. def status(self) -> int:
  616. """Returns the HTTP status code of the response."""
  617. @property
  618. def http_version(self) -> HTTPVersion:
  619. """Returns the HTTP version of the response."""
  620. @property
  621. def headers(self) -> Headers:
  622. """Returns the response headers."""
  623. @property
  624. def content(self) -> AsyncIterator[_Buffer]:
  625. """Returns an asynchronous iterator over the response content."""
  626. @property
  627. def trailers(self) -> Headers:
  628. """Returns the response trailers.
  629. Because trailers complete the response, this will only be filled after fully
  630. consuming the content iterator.
  631. """
  632. def aclose(self) -> Awaitable[None]:
  633. """Closes the response, releasing any underlying resources.
  634. Note that if your code is guaranteed to fully consume the response content,
  635. it is not necessary to explicitly close the response.
  636. """
  637. @final
  638. class SyncClient:
  639. """A synchronous HTTP client.
  640. A client is a lightweight wrapper around a SyncTransport, providing convenience methods
  641. for common HTTP operations with buffering.
  642. """
  643. def __init__(self, transport: SyncTransport | None = None) -> None:
  644. """Creates a new synchronous HTTP client.
  645. Args:
  646. transport: The transport to use for requests. If None, the shared default
  647. transport will be used.
  648. """
  649. def get(
  650. self,
  651. url: str,
  652. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  653. *,
  654. timeout: float | None = None,
  655. params: _QueryParams | None = None,
  656. ) -> FullResponse:
  657. """Executes a GET HTTP request.
  658. Args:
  659. url: The unencoded request URL.
  660. headers: The request headers.
  661. timeout: The timeout for the request in seconds.
  662. params: Query parameters to append to the URL. None values will be treated as key-only.
  663. Raises:
  664. ConnectionError: If the connection fails.
  665. TimeoutError: If the request times out.
  666. RemoteProtocolError: If the peer violates the HTTP protocol.
  667. ReadError: If an error occurs reading the response.
  668. WriteError: If an error occurs writing the request.
  669. """
  670. def post(
  671. self,
  672. url: str,
  673. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  674. content: _SyncRequestContent | None = None,
  675. *,
  676. timeout: float | None = None,
  677. params: _QueryParams | None = None,
  678. ) -> FullResponse:
  679. """Executes a POST HTTP request.
  680. Args:
  681. url: The unencoded request URL.
  682. headers: The request headers.
  683. content: The request content. A Python dictionary will be converted
  684. to JSON and a SyncMultipart will be sent as a multipart form.
  685. timeout: The timeout for the request in seconds.
  686. params: Query parameters to append to the URL. None values will be treated as key-only.
  687. Raises:
  688. ConnectionError: If the connection fails.
  689. TimeoutError: If the request times out.
  690. RemoteProtocolError: If the peer violates the HTTP protocol.
  691. ReadError: If an error occurs reading the response.
  692. WriteError: If an error occurs writing the request.
  693. """
  694. def delete(
  695. self,
  696. url: str,
  697. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  698. *,
  699. timeout: float | None = None,
  700. params: _QueryParams | None = None,
  701. ) -> FullResponse:
  702. """Executes a DELETE HTTP request.
  703. Args:
  704. url: The unencoded request URL.
  705. headers: The request headers.
  706. timeout: The timeout for the request in seconds.
  707. params: Query parameters to append to the URL. None values will be treated as key-only.
  708. Raises:
  709. ConnectionError: If the connection fails.
  710. TimeoutError: If the request times out.
  711. RemoteProtocolError: If the peer violates the HTTP protocol.
  712. ReadError: If an error occurs reading the response.
  713. WriteError: If an error occurs writing the request.
  714. """
  715. def head(
  716. self,
  717. url: str,
  718. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  719. *,
  720. timeout: float | None = None,
  721. params: _QueryParams | None = None,
  722. ) -> FullResponse:
  723. """Executes a HEAD HTTP request.
  724. Args:
  725. url: The unencoded request URL.
  726. headers: The request headers.
  727. timeout: The timeout for the request in seconds.
  728. params: Query parameters to append to the URL. None values will be treated as key-only.
  729. Raises:
  730. ConnectionError: If the connection fails.
  731. TimeoutError: If the request times out.
  732. RemoteProtocolError: If the peer violates the HTTP protocol.
  733. ReadError: If an error occurs reading the response.
  734. WriteError: If an error occurs writing the request.
  735. """
  736. def options(
  737. self,
  738. url: str,
  739. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  740. *,
  741. timeout: float | None = None,
  742. params: _QueryParams | None = None,
  743. ) -> FullResponse:
  744. """Executes a OPTIONS HTTP request.
  745. Args:
  746. url: The unencoded request URL.
  747. headers: The request headers.
  748. timeout: The timeout for the request in seconds.
  749. params: Query parameters to append to the URL. None values will be treated as key-only.
  750. Raises:
  751. ConnectionError: If the connection fails.
  752. TimeoutError: If the request times out.
  753. RemoteProtocolError: If the peer violates the HTTP protocol.
  754. ReadError: If an error occurs reading the response.
  755. WriteError: If an error occurs writing the request.
  756. """
  757. def patch(
  758. self,
  759. url: str,
  760. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  761. content: _SyncRequestContent | None = None,
  762. *,
  763. timeout: float | None = None,
  764. params: _QueryParams | None = None,
  765. ) -> FullResponse:
  766. """Executes a PATCH HTTP request.
  767. Args:
  768. url: The unencoded request URL.
  769. headers: The request headers.
  770. content: The request content. A Python dictionary will be converted
  771. to JSON and a SyncMultipart will be sent as a multipart form.
  772. timeout: The timeout for the request in seconds.
  773. params: Query parameters to append to the URL. None values will be treated as key-only.
  774. Raises:
  775. ConnectionError: If the connection fails.
  776. TimeoutError: If the request times out.
  777. RemoteProtocolError: If the peer violates the HTTP protocol.
  778. ReadError: If an error occurs reading the response.
  779. WriteError: If an error occurs writing the request.
  780. """
  781. def put(
  782. self,
  783. url: str,
  784. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  785. content: _SyncRequestContent | None = None,
  786. *,
  787. timeout: float | None = None,
  788. params: _QueryParams | None = None,
  789. ) -> FullResponse:
  790. """Executes a PUT HTTP request.
  791. Args:
  792. url: The unencoded request URL.
  793. headers: The request headers.
  794. content: The request content. A Python dictionary will be converted
  795. to JSON and a SyncMultipart will be sent as a multipart form.
  796. timeout: The timeout for the request in seconds.
  797. params: Query parameters to append to the URL. None values will be treated as key-only.
  798. Raises:
  799. ConnectionError: If the connection fails.
  800. TimeoutError: If the request times out.
  801. RemoteProtocolError: If the peer violates the HTTP protocol.
  802. ReadError: If an error occurs reading the response.
  803. WriteError: If an error occurs writing the request.
  804. """
  805. def execute(
  806. self,
  807. method: str,
  808. url: str,
  809. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  810. content: _SyncRequestContent | None = None,
  811. *,
  812. timeout: float | None = None,
  813. params: _QueryParams | None = None,
  814. ) -> FullResponse:
  815. """Executes an HTTP request, returning the full buffered response.
  816. Args:
  817. method: The HTTP method.
  818. url: The unencoded request URL.
  819. headers: The request headers.
  820. content: The request content. A Python dictionary will be converted
  821. to JSON and a SyncMultipart will be sent as a multipart form.
  822. timeout: The timeout for the request in seconds.
  823. params: Query parameters to append to the URL. None values will be treated as key-only.
  824. Raises:
  825. ConnectionError: If the connection fails.
  826. TimeoutError: If the request times out.
  827. RemoteProtocolError: If the peer violates the HTTP protocol.
  828. ReadError: If an error occurs reading the response.
  829. WriteError: If an error occurs writing the request.
  830. """
  831. def stream(
  832. self,
  833. method: str,
  834. url: str,
  835. headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None = None,
  836. content: _SyncRequestContent | None = None,
  837. *,
  838. timeout: float | None = None,
  839. params: _QueryParams | None = None,
  840. ) -> AbstractContextManager[SyncResponse]:
  841. """Executes an HTTP request, allowing the response content to be streamed.
  842. Args:
  843. method: The HTTP method.
  844. url: The unencoded request URL.
  845. headers: The request headers.
  846. content: The request content. A Python dictionary will be converted
  847. to JSON and a SyncMultipart will be sent as a multipart form.
  848. timeout: The timeout for the request in seconds.
  849. params: Query parameters to append to the URL. None values will be treated as key-only.
  850. Raises:
  851. ConnectionError: If the connection fails.
  852. TimeoutError: If the request times out.
  853. RemoteProtocolError: If the peer violates the HTTP protocol.
  854. ReadError: If an error occurs reading the response.
  855. WriteError: If an error occurs writing the request.
  856. """
  857. @runtime_checkable
  858. class SyncTransport(Protocol):
  859. """Protocol for synchronous HTTP transport implementations.
  860. The default implementation of SyncTransport is SyncHTTPTransport which issues requests.
  861. Custom implementations may be useful to:
  862. - Mock requests for testing.
  863. - Add middleware wrapping transports
  864. """
  865. def execute_sync(self, request: SyncRequest) -> SyncResponse:
  866. """Executes a request."""
  867. @final
  868. class SyncHTTPTransport:
  869. """An HTTP transport implementation using reqwest."""
  870. def __init__(
  871. self,
  872. *,
  873. tls_ca_cert: bytes | None = None,
  874. tls_include_system_certs: bool = False,
  875. tls_key: bytes | None = None,
  876. tls_cert: bytes | None = None,
  877. http_version: HTTPVersion | None = None,
  878. proxy: str | Proxy | Sequence[str | Proxy] | None = None,
  879. timeout: float | None = None,
  880. connect_timeout: float | None = 30.0,
  881. read_timeout: float | None = None,
  882. pool_idle_timeout: float | None = 90.0,
  883. pool_max_idle_per_host: int | None = None,
  884. tcp_keepalive_interval: float | None = 30.0,
  885. enable_gzip: bool = True,
  886. enable_brotli: bool = True,
  887. enable_zstd: bool = True,
  888. use_system_dns: bool = False,
  889. enable_cookie_store: bool = False,
  890. follow_redirects: bool = True,
  891. max_redirects: int = 10,
  892. enable_otel: bool = True,
  893. meter_provider: MeterProvider | None = None,
  894. tracer_provider: TracerProvider | None = None,
  895. ) -> None:
  896. """Creates a new SyncHTTPTransport object.
  897. Without any arguments, the transport behaves like the default transport without trusted TLS certificates.
  898. When creating a transport, take care to set options to meet your needs.
  899. Args:
  900. tls_ca_cert: The CA certificate to use to verify the server for TLS connections.
  901. tls_include_system_certs: Whether to include the system CA certificates to verify TLS connections.
  902. If this is unset and tls_ca_cert is not provided, TLS will not function.
  903. tls_key: The client private key to identify the client for mTLS connections.
  904. tls_cert must also be set.
  905. tls_cert: The client certificate to identify the client for mTLS connections.
  906. tls_key must also be set.
  907. http_version: The HTTP version to use for requests. If unset, HTTP/1 is used for
  908. plaintext and ALPN negotiates the version for TLS connections
  909. which typically means HTTP/2 if the server supports it.
  910. proxy: A proxy to send requests through. A URL string such as
  911. "http://localhost:8030" proxies all requests, equivalent to
  912. Proxy(url). Pass a Proxy object to configure authentication,
  913. extra headers, or routing rules, or a sequence of them to
  914. apply multiple proxy rules, where the first matching proxy
  915. is used for each request. An empty sequence, like None,
  916. configures no explicit proxy, in which case proxy
  917. environment variables such as HTTP_PROXY still apply.
  918. timeout: Default timeout for requests in seconds. This is the timeout from
  919. the start of the request to the end of the response.
  920. connect_timeout: Timeout for connection establishment in seconds.
  921. read_timeout: Timeout for each read operation of a request in seconds.
  922. pool_idle_timeout: Timeout for idle connections in the connection pool in seconds.
  923. pool_max_idle_per_host: Maximum number of idle connections to keep in the pool per host.
  924. Defaults to 2.
  925. tcp_keepalive_interval: Interval for TCP keepalive probes in seconds.
  926. enable_gzip: Whether to enable gzip decompression for responses.
  927. enable_brotli: Whether to enable brotli decompression for responses.
  928. enable_zstd: Whether to enable zstd decompression for responses.
  929. use_system_dns: Whether to use the system DNS resolver. By default, pyqwest uses an
  930. asynchronous DNS resolver implemented in Rust, but it can have different
  931. behavior from system DNS in certain environments. Try enabling this option if
  932. you have any DNS resolution issues.
  933. enable_cookie_store: Whether to enable automatic cookie storage and sending. When enabled,
  934. the transport will automatically store cookies from responses and send
  935. them with subsequent requests.
  936. follow_redirects: Whether to automatically follow redirect responses. When disabled,
  937. which is the default, redirect responses are returned as-is.
  938. Leave this disabled when the transport is used through
  939. pyqwest.httpx, because httpx clients apply their own
  940. follow_redirects setting and track redirects in response.history.
  941. max_redirects: Maximum number of redirects to follow when follow_redirects is enabled.
  942. A request exceeding it fails with TooManyRedirects.
  943. """
  944. def __enter__(self) -> SyncHTTPTransport:
  945. """Enters the context manager for the transport to automatically
  946. close it when leaving.
  947. """
  948. def __exit__(
  949. self,
  950. _exc_type: type[BaseException] | None,
  951. _exc_value: BaseException | None,
  952. _traceback: TracebackType | None,
  953. ) -> None:
  954. """Exits the context manager for the transport, closing it."""
  955. def execute_sync(self, request: SyncRequest) -> SyncResponse:
  956. """Executes the given request, returning the response.
  957. Args:
  958. request: The request to execute.
  959. """
  960. def close(self) -> None:
  961. """Closes the transport, releasing any underlying resources."""
  962. def get_default_sync_transport() -> SyncHTTPTransport:
  963. """Returns the singleton default HTTP transport instance used by synchronous clients that do not
  964. specify a transport.ult HTTP transport instance used by clients that do not
  965. specify a transport.
  966. The default transport is constructed as follows:
  967. ```
  968. SyncHTTPTransport(
  969. connect_timeout=30.0,
  970. pool_idle_timeout=90.0,
  971. tcp_keepalive_interval=30.0,
  972. enable_gzip: bool = True,
  973. enable_brotli: bool = True,
  974. enable_zstd: bool = True,
  975. )
  976. ```
  977. """
  978. @final
  979. class SyncRequest:
  980. """An HTTP request."""
  981. def __init__(
  982. self,
  983. method: str,
  984. url: str,
  985. headers: Headers | None = None,
  986. content: _SyncRequestContent | None = None,
  987. *,
  988. params: _QueryParams | None = None,
  989. ) -> None:
  990. """Creates a new SyncRequest object.
  991. Args:
  992. method: The HTTP method.
  993. url: The unencoded request URL.
  994. headers: The request headers.
  995. content: The request content. A Python dictionary will be converted
  996. to JSON and a SyncMultipart will be sent as a multipart form.
  997. params: Query parameters to append to the URL. None values will be treated as key-only.
  998. """
  999. @property
  1000. def method(self) -> str:
  1001. """Returns the HTTP method of the request."""
  1002. @property
  1003. def url(self) -> str:
  1004. """Returns the unencoded request URL."""
  1005. @property
  1006. def headers(self) -> Headers:
  1007. """Returns the request headers."""
  1008. @property
  1009. def content(self) -> bytes | Iterator[bytes]:
  1010. """Returns an iterator over the request content."""
  1011. @property
  1012. def _json(self) -> bool: ...
  1013. @final
  1014. class SyncResponse:
  1015. """An HTTP response."""
  1016. def __init__(
  1017. self,
  1018. *,
  1019. status: int,
  1020. http_version: HTTPVersion | None = None,
  1021. headers: Headers | None = None,
  1022. content: bytes | Iterable[_Buffer] | None = None,
  1023. trailers: Headers | None = None,
  1024. ) -> None:
  1025. """Creates a new SyncResponse object.
  1026. Care must be taken if your service uses trailers and you override content.
  1027. Trailers will not be received without fully consuming the original response content.
  1028. Patterns that wrap the original response content should not have any issue but if
  1029. you replace it completely and need trailers, make sure to still read and discard
  1030. the original content.
  1031. Args:
  1032. status: The HTTP status code of the response.
  1033. http_version: The HTTP version of the response.
  1034. headers: The response headers.
  1035. content: The response content.
  1036. trailers: The response trailers.
  1037. Raises:
  1038. RemoteProtocolError: If the status is not a valid HTTP status code.
  1039. """
  1040. def __enter__(self) -> SyncResponse:
  1041. """Enters the context manager for the response to automatically
  1042. close it when leaving.
  1043. Note that if your code is guaranteed to fully consume the response content,
  1044. it is not necessary to explicitly close the response.
  1045. """
  1046. def __exit__(
  1047. self,
  1048. _exc_type: type[BaseException] | None,
  1049. _exc_value: BaseException | None,
  1050. _traceback: TracebackType | None,
  1051. ) -> None:
  1052. """Exits the context manager for the response, closing it."""
  1053. @property
  1054. def status(self) -> int:
  1055. """Returns the HTTP status code of the response."""
  1056. @property
  1057. def http_version(self) -> HTTPVersion:
  1058. """Returns the HTTP version of the response."""
  1059. @property
  1060. def headers(self) -> Headers:
  1061. """Returns the response headers."""
  1062. @property
  1063. def content(self) -> Iterator[_Buffer]:
  1064. """Returns an iterator over the response content."""
  1065. @property
  1066. def trailers(self) -> Headers:
  1067. """Returns the response trailers.
  1068. Because trailers complete the response, this will only be filled after fully
  1069. consuming the content iterator.
  1070. """
  1071. def close(self) -> None:
  1072. """Closes the response, releasing any underlying resources.
  1073. Note that if your code is guaranteed to fully consume the response content,
  1074. it is not necessary to explicitly close the response.
  1075. """
  1076. @final
  1077. class FullResponse:
  1078. """A fully buffered HTTP response."""
  1079. def __init__(
  1080. self, status: int, headers: Headers, content: bytes, trailers: Headers
  1081. ) -> None:
  1082. """Creates a new FullResponse object.
  1083. Args:
  1084. status: The HTTP status code of the response.
  1085. headers: The response headers.
  1086. content: The response content.
  1087. trailers: The response trailers.
  1088. """
  1089. @property
  1090. def status(self) -> int:
  1091. """Returns the HTTP status code of the response."""
  1092. @property
  1093. def headers(self) -> Headers:
  1094. """Returns the response headers."""
  1095. @property
  1096. def content(self) -> bytes:
  1097. """Returns the response content."""
  1098. @property
  1099. def trailers(self) -> Headers:
  1100. """Returns the response trailers."""
  1101. def text(self) -> str:
  1102. """Returns the response content decoded as text.
  1103. The encoding for decoding is determined from the content-type header if present,
  1104. defaulting to UTF-8 otherwise.
  1105. """
  1106. def json(self) -> _JSON:
  1107. """Parses and returns the response content as JSON.
  1108. The content-type header is not checked when using this method.
  1109. """
  1110. @final
  1111. class ReadError(Exception):
  1112. """An error representing a read error during response reading."""
  1113. @final
  1114. class WriteError(Exception):
  1115. """An error representing a write error during request sending."""
  1116. @final
  1117. class TooManyRedirects(Exception):
  1118. """An error raised when a request exceeded the transport's max_redirects."""
  1119. @final
  1120. class HTTPHeaderName:
  1121. """An enum type corresponding to HTTP header names."""
  1122. def __init__(self, name: str) -> None:
  1123. """Creates a new HTTPHeaderName. When available, prefer one of the
  1124. class attributes.
  1125. Args:
  1126. name: The header name.
  1127. """
  1128. ACCEPT: HTTPHeaderName
  1129. """The "accept" header."""
  1130. ACCEPT_CHARSET: HTTPHeaderName
  1131. """The "accept-charset" header."""
  1132. ACCEPT_ENCODING: HTTPHeaderName
  1133. """The "accept-encoding" header."""
  1134. ACCEPT_LANGUAGE: HTTPHeaderName
  1135. """The "accept-language" header."""
  1136. ACCEPT_RANGES: HTTPHeaderName
  1137. """The "accept-ranges" header."""
  1138. ACCESS_CONTROL_ALLOW_CREDENTIALS: HTTPHeaderName
  1139. """The "access-control-allow-credentials" header."""
  1140. ACCESS_CONTROL_ALLOW_HEADERS: HTTPHeaderName
  1141. """The "access-control-allow-headers" header."""
  1142. ACCESS_CONTROL_ALLOW_METHODS: HTTPHeaderName
  1143. """The "access-control-allow-methods" header."""
  1144. ACCESS_CONTROL_ALLOW_ORIGIN: HTTPHeaderName
  1145. """The "access-control-allow-origin" header."""
  1146. ACCESS_CONTROL_EXPOSE_HEADERS: HTTPHeaderName
  1147. """The "access-control-expose-headers" header."""
  1148. ACCESS_CONTROL_MAX_AGE: HTTPHeaderName
  1149. """The "access-control-max-age" header."""
  1150. ACCESS_CONTROL_REQUEST_HEADERS: HTTPHeaderName
  1151. """The "access-control-request-headers" header."""
  1152. ACCESS_CONTROL_REQUEST_METHOD: HTTPHeaderName
  1153. """The "access-control-request-method" header."""
  1154. AGE: HTTPHeaderName
  1155. """The "age" header."""
  1156. ALLOW: HTTPHeaderName
  1157. """The "allow" header."""
  1158. ALT_SVC: HTTPHeaderName
  1159. """The "alt-svc" header."""
  1160. AUTHORIZATION: HTTPHeaderName
  1161. """The "authorization" header."""
  1162. CACHE_CONTROL: HTTPHeaderName
  1163. """The "cache-control" header."""
  1164. CACHE_STATUS: HTTPHeaderName
  1165. """The "cache-status" header."""
  1166. CDN_CACHE_CONTROL: HTTPHeaderName
  1167. """The "cdn-cache-control" header."""
  1168. CONNECTION: HTTPHeaderName
  1169. """The "connection" header."""
  1170. CONTENT_DISPOSITION: HTTPHeaderName
  1171. """The "content-disposition" header."""
  1172. CONTENT_ENCODING: HTTPHeaderName
  1173. """The "content-encoding" header."""
  1174. CONTENT_LANGUAGE: HTTPHeaderName
  1175. """The "content-language" header."""
  1176. CONTENT_LENGTH: HTTPHeaderName
  1177. """The "content-length" header."""
  1178. CONTENT_LOCATION: HTTPHeaderName
  1179. """The "content-location" header."""
  1180. CONTENT_RANGE: HTTPHeaderName
  1181. """The "content-range" header."""
  1182. CONTENT_SECURITY_POLICY: HTTPHeaderName
  1183. """The "content-security-policy" header."""
  1184. CONTENT_SECURITY_POLICY_REPORT_ONLY: HTTPHeaderName
  1185. """The "content-security-policy-report-only" header."""
  1186. CONTENT_TYPE: HTTPHeaderName
  1187. """The "content-type" header."""
  1188. COOKIE: HTTPHeaderName
  1189. """The "cookie" header."""
  1190. DNT: HTTPHeaderName
  1191. """The "dnt" header."""
  1192. DATE: HTTPHeaderName
  1193. """The "date" header."""
  1194. ETAG: HTTPHeaderName
  1195. """The "etag" header."""
  1196. EXPECT: HTTPHeaderName
  1197. """The "expect" header."""
  1198. EXPIRES: HTTPHeaderName
  1199. """The "expires" header."""
  1200. FORWARDED: HTTPHeaderName
  1201. """The "forwarded" header."""
  1202. FROM: HTTPHeaderName
  1203. """The "from" header."""
  1204. HOST: HTTPHeaderName
  1205. """The "host" header."""
  1206. IF_MATCH: HTTPHeaderName
  1207. """The "if-match" header."""
  1208. IF_MODIFIED_SINCE: HTTPHeaderName
  1209. """The "if-modified-since" header."""
  1210. IF_NONE_MATCH: HTTPHeaderName
  1211. """The "if-none-match" header."""
  1212. IF_RANGE: HTTPHeaderName
  1213. """The "if-range" header."""
  1214. IF_UNMODIFIED_SINCE: HTTPHeaderName
  1215. """The "if-unmodified-since" header."""
  1216. LAST_MODIFIED: HTTPHeaderName
  1217. """The "last-modified" header."""
  1218. LINK: HTTPHeaderName
  1219. """The "link" header."""
  1220. LOCATION: HTTPHeaderName
  1221. """The "location" header."""
  1222. MAX_FORWARDS: HTTPHeaderName
  1223. """The "max-forwards" header."""
  1224. ORIGIN: HTTPHeaderName
  1225. """The "origin" header."""
  1226. PRAGMA: HTTPHeaderName
  1227. """The "pragma" header."""
  1228. PROXY_AUTHENTICATE: HTTPHeaderName
  1229. """The "proxy-authenticate" header."""
  1230. PROXY_AUTHORIZATION: HTTPHeaderName
  1231. """The "proxy-authorization" header."""
  1232. PUBLIC_KEY_PINS: HTTPHeaderName
  1233. """The "public-key-pins" header."""
  1234. PUBLIC_KEY_PINS_REPORT_ONLY: HTTPHeaderName
  1235. """The "public-key-pins-report-only" header."""
  1236. RANGE: HTTPHeaderName
  1237. """The "range" header."""
  1238. REFERER: HTTPHeaderName
  1239. """The "referer" header."""
  1240. REFERRER_POLICY: HTTPHeaderName
  1241. """The "referrer-policy" header."""
  1242. REFRESH: HTTPHeaderName
  1243. """The "refresh" header."""
  1244. RETRY_AFTER: HTTPHeaderName
  1245. """The "retry-after" header."""
  1246. SEC_WEBSOCKET_ACCEPT: HTTPHeaderName
  1247. """The "sec-websocket-accept" header."""
  1248. SEC_WEBSOCKET_EXTENSIONS: HTTPHeaderName
  1249. """The "sec-websocket-extensions" header."""
  1250. SEC_WEBSOCKET_KEY: HTTPHeaderName
  1251. """The "sec-websocket-key" header."""
  1252. SEC_WEBSOCKET_PROTOCOL: HTTPHeaderName
  1253. """The "sec-websocket-protocol" header."""
  1254. SEC_WEBSOCKET_VERSION: HTTPHeaderName
  1255. """The "sec-websocket-version" header."""
  1256. SERVER: HTTPHeaderName
  1257. """The "server" header."""
  1258. SET_COOKIE: HTTPHeaderName
  1259. """The "set-cookie" header."""
  1260. STRICT_TRANSPORT_SECURITY: HTTPHeaderName
  1261. """The "strict-transport-security" header."""
  1262. TE: HTTPHeaderName
  1263. """The "te" header."""
  1264. TRAILER: HTTPHeaderName
  1265. """The "trailer" header."""
  1266. TRANSFER_ENCODING: HTTPHeaderName
  1267. """The "transfer-encoding" header."""
  1268. USER_AGENT: HTTPHeaderName
  1269. """The "user-agent" header."""
  1270. UPGRADE: HTTPHeaderName
  1271. """The "upgrade" header."""
  1272. UPGRADE_INSECURE_REQUESTS: HTTPHeaderName
  1273. """The "upgrade-insecure-requests" header."""
  1274. VARY: HTTPHeaderName
  1275. """The "vary" header."""
  1276. VIA: HTTPHeaderName
  1277. """The "via" header."""
  1278. WARNING: HTTPHeaderName
  1279. """The "warning" header."""
  1280. WWW_AUTHENTICATE: HTTPHeaderName
  1281. """The "www-authenticate" header."""
  1282. X_CONTENT_TYPE_OPTIONS: HTTPHeaderName
  1283. """The "x-content-type-options" header."""
  1284. X_DNS_PREFETCH_CONTROL: HTTPHeaderName
  1285. """The "x-dns-prefetch-control" header."""
  1286. X_FRAME_OPTIONS: HTTPHeaderName
  1287. """The "x-frame-options" header."""
  1288. X_XSS_PROTECTION: HTTPHeaderName
  1289. """The "x-xss-protection" header."""
  1290. def set_sync_timeout(timeout: float) -> AbstractContextManager[None]: ...
  1291. def get_sync_timeout() -> datetime.timedelta | None: ...
  1292. class _BrotliDecompressor:
  1293. def feed(self, data: bytes, *, end: bool) -> bytes: ...
  1294. class _ZstdDecompressor:
  1295. def feed(self, data: bytes, *, end: bool) -> bytes: ...
  1296. class _Backoff:
  1297. def __init__(
  1298. self,
  1299. initial_interval: float,
  1300. randomization_factor: float,
  1301. multiplier: float,
  1302. max_interval: float,
  1303. ) -> None: ...
  1304. def next_backoff(self) -> float | None: ...