Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/httpcore/_api.py: 58%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

12 statements  

1from contextlib import contextmanager 

2from typing import Iterator, Optional, Union 

3 

4from ._models import URL, Extensions, HeaderTypes, Response 

5from ._sync.connection_pool import ConnectionPool 

6 

7 

8def request( 

9 method: Union[bytes, str], 

10 url: Union[URL, bytes, str], 

11 *, 

12 headers: HeaderTypes = None, 

13 content: Union[bytes, Iterator[bytes], None] = None, 

14 extensions: Optional[Extensions] = None, 

15) -> Response: 

16 """ 

17 Sends an HTTP request, returning the response. 

18 

19 ``` 

20 response = httpcore.request("GET", "https://www.example.com/") 

21 ``` 

22 

23 Arguments: 

24 method: The HTTP method for the request. Typically one of `"GET"`, 

25 `"OPTIONS"`, `"HEAD"`, `"POST"`, `"PUT"`, `"PATCH"`, or `"DELETE"`. 

26 url: The URL of the HTTP request. Either as an instance of `httpcore.URL`, 

27 or as str/bytes. 

28 headers: The HTTP request headers. Either as a dictionary of str/bytes, 

29 or as a list of two-tuples of str/bytes. 

30 content: The content of the request body. Either as bytes, 

31 or as a bytes iterator. 

32 extensions: A dictionary of optional extra information included on the request. 

33 Possible keys include `"timeout"`. 

34 

35 Returns: 

36 An instance of `httpcore.Response`. 

37 """ 

38 with ConnectionPool() as pool: 

39 return pool.request( 

40 method=method, 

41 url=url, 

42 headers=headers, 

43 content=content, 

44 extensions=extensions, 

45 ) 

46 

47 

48@contextmanager 

49def stream( 

50 method: Union[bytes, str], 

51 url: Union[URL, bytes, str], 

52 *, 

53 headers: HeaderTypes = None, 

54 content: Union[bytes, Iterator[bytes], None] = None, 

55 extensions: Optional[Extensions] = None, 

56) -> Iterator[Response]: 

57 """ 

58 Sends an HTTP request, returning the response within a content manager. 

59 

60 ``` 

61 with httpcore.stream("GET", "https://www.example.com/") as response: 

62 ... 

63 ``` 

64 

65 When using the `stream()` function, the body of the response will not be 

66 automatically read. If you want to access the response body you should 

67 either use `content = response.read()`, or `for chunk in response.iter_content()`. 

68 

69 Arguments: 

70 method: The HTTP method for the request. Typically one of `"GET"`, 

71 `"OPTIONS"`, `"HEAD"`, `"POST"`, `"PUT"`, `"PATCH"`, or `"DELETE"`. 

72 url: The URL of the HTTP request. Either as an instance of `httpcore.URL`, 

73 or as str/bytes. 

74 headers: The HTTP request headers. Either as a dictionary of str/bytes, 

75 or as a list of two-tuples of str/bytes. 

76 content: The content of the request body. Either as bytes, 

77 or as a bytes iterator. 

78 extensions: A dictionary of optional extra information included on the request. 

79 Possible keys include `"timeout"`. 

80 

81 Returns: 

82 An instance of `httpcore.Response`. 

83 """ 

84 with ConnectionPool() as pool: 

85 with pool.stream( 

86 method=method, 

87 url=url, 

88 headers=headers, 

89 content=content, 

90 extensions=extensions, 

91 ) as response: 

92 yield response