_api.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from __future__ import annotations
  2. import contextlib
  3. import typing
  4. from ._models import URL, Extensions, HeaderTypes, Response
  5. from ._sync.connection_pool import ConnectionPool
  6. def request(
  7. method: bytes | str,
  8. url: URL | bytes | str,
  9. *,
  10. headers: HeaderTypes = None,
  11. content: bytes | typing.Iterator[bytes] | None = None,
  12. extensions: Extensions | None = None,
  13. ) -> Response:
  14. """
  15. Sends an HTTP request, returning the response.
  16. ```
  17. response = httpcore.request("GET", "https://www.example.com/")
  18. ```
  19. Arguments:
  20. method: The HTTP method for the request. Typically one of `"GET"`,
  21. `"OPTIONS"`, `"HEAD"`, `"POST"`, `"PUT"`, `"PATCH"`, or `"DELETE"`.
  22. url: The URL of the HTTP request. Either as an instance of `httpcore.URL`,
  23. or as str/bytes.
  24. headers: The HTTP request headers. Either as a dictionary of str/bytes,
  25. or as a list of two-tuples of str/bytes.
  26. content: The content of the request body. Either as bytes,
  27. or as a bytes iterator.
  28. extensions: A dictionary of optional extra information included on the request.
  29. Possible keys include `"timeout"`.
  30. Returns:
  31. An instance of `httpcore.Response`.
  32. """
  33. with ConnectionPool() as pool:
  34. return pool.request(
  35. method=method,
  36. url=url,
  37. headers=headers,
  38. content=content,
  39. extensions=extensions,
  40. )
  41. @contextlib.contextmanager
  42. def stream(
  43. method: bytes | str,
  44. url: URL | bytes | str,
  45. *,
  46. headers: HeaderTypes = None,
  47. content: bytes | typing.Iterator[bytes] | None = None,
  48. extensions: Extensions | None = None,
  49. ) -> typing.Iterator[Response]:
  50. """
  51. Sends an HTTP request, returning the response within a content manager.
  52. ```
  53. with httpcore.stream("GET", "https://www.example.com/") as response:
  54. ...
  55. ```
  56. When using the `stream()` function, the body of the response will not be
  57. automatically read. If you want to access the response body you should
  58. either use `content = response.read()`, or `for chunk in response.iter_content()`.
  59. Arguments:
  60. method: The HTTP method for the request. Typically one of `"GET"`,
  61. `"OPTIONS"`, `"HEAD"`, `"POST"`, `"PUT"`, `"PATCH"`, or `"DELETE"`.
  62. url: The URL of the HTTP request. Either as an instance of `httpcore.URL`,
  63. or as str/bytes.
  64. headers: The HTTP request headers. Either as a dictionary of str/bytes,
  65. or as a list of two-tuples of str/bytes.
  66. content: The content of the request body. Either as bytes,
  67. or as a bytes iterator.
  68. extensions: A dictionary of optional extra information included on the request.
  69. Possible keys include `"timeout"`.
  70. Returns:
  71. An instance of `httpcore.Response`.
  72. """
  73. with ConnectionPool() as pool:
  74. with pool.stream(
  75. method=method,
  76. url=url,
  77. headers=headers,
  78. content=content,
  79. extensions=extensions,
  80. ) as response:
  81. yield response