|
| 1 | +"""HTTP transport implementations for VWS clients.""" |
| 2 | + |
| 3 | +from typing import Protocol, runtime_checkable |
| 4 | + |
| 5 | +import httpx |
| 6 | +import requests |
| 7 | +from beartype import BeartypeConf, beartype |
| 8 | + |
| 9 | +from vws.response import Response |
| 10 | + |
| 11 | + |
| 12 | +@runtime_checkable |
| 13 | +class Transport(Protocol): |
| 14 | + """Protocol for HTTP transports used by VWS clients. |
| 15 | +
|
| 16 | + A transport is a callable that makes an HTTP request and |
| 17 | + returns a ``Response``. |
| 18 | + """ |
| 19 | + |
| 20 | + def __call__( |
| 21 | + self, |
| 22 | + *, |
| 23 | + method: str, |
| 24 | + url: str, |
| 25 | + headers: dict[str, str], |
| 26 | + data: bytes, |
| 27 | + timeout: float | tuple[float, float], |
| 28 | + ) -> Response: |
| 29 | + """Make an HTTP request. |
| 30 | +
|
| 31 | + Args: |
| 32 | + method: The HTTP method (e.g. "GET", "POST"). |
| 33 | + url: The full URL to request. |
| 34 | + headers: Headers to send with the request. |
| 35 | + data: The request body as bytes. |
| 36 | + timeout: The timeout for the request. A float |
| 37 | + sets both the connect and read timeouts. A |
| 38 | + (connect, read) tuple sets them individually. |
| 39 | +
|
| 40 | + Returns: |
| 41 | + A Response populated from the HTTP response. |
| 42 | + """ |
| 43 | + ... # pylint: disable=unnecessary-ellipsis |
| 44 | + |
| 45 | + |
| 46 | +@beartype(conf=BeartypeConf(is_pep484_tower=True)) |
| 47 | +class RequestsTransport: |
| 48 | + """HTTP transport using the ``requests`` library. |
| 49 | +
|
| 50 | + This is the default transport. |
| 51 | + """ |
| 52 | + |
| 53 | + def __call__( |
| 54 | + self, |
| 55 | + *, |
| 56 | + method: str, |
| 57 | + url: str, |
| 58 | + headers: dict[str, str], |
| 59 | + data: bytes, |
| 60 | + timeout: float | tuple[float, float], |
| 61 | + ) -> Response: |
| 62 | + """Make an HTTP request using ``requests``. |
| 63 | +
|
| 64 | + Args: |
| 65 | + method: The HTTP method. |
| 66 | + url: The full URL. |
| 67 | + headers: Request headers. |
| 68 | + data: The request body. |
| 69 | + timeout: The request timeout. |
| 70 | +
|
| 71 | + Returns: |
| 72 | + A Response populated from the requests response. |
| 73 | + """ |
| 74 | + requests_response = requests.request( |
| 75 | + method=method, |
| 76 | + url=url, |
| 77 | + headers=headers, |
| 78 | + data=data, |
| 79 | + timeout=timeout, |
| 80 | + ) |
| 81 | + |
| 82 | + return Response( |
| 83 | + text=requests_response.text, |
| 84 | + url=requests_response.url, |
| 85 | + status_code=requests_response.status_code, |
| 86 | + headers=dict(requests_response.headers), |
| 87 | + request_body=requests_response.request.body, |
| 88 | + tell_position=requests_response.raw.tell(), |
| 89 | + content=bytes(requests_response.content), |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +@beartype(conf=BeartypeConf(is_pep484_tower=True)) |
| 94 | +class HTTPXTransport: |
| 95 | + """HTTP transport using the ``httpx`` library. |
| 96 | +
|
| 97 | + Use this transport for environments where ``httpx`` is |
| 98 | + preferred over ``requests``. |
| 99 | + """ |
| 100 | + |
| 101 | + def __call__( |
| 102 | + self, |
| 103 | + *, |
| 104 | + method: str, |
| 105 | + url: str, |
| 106 | + headers: dict[str, str], |
| 107 | + data: bytes, |
| 108 | + timeout: float | tuple[float, float], |
| 109 | + ) -> Response: |
| 110 | + """Make an HTTP request using ``httpx``. |
| 111 | +
|
| 112 | + Args: |
| 113 | + method: The HTTP method. |
| 114 | + url: The full URL. |
| 115 | + headers: Request headers. |
| 116 | + data: The request body. |
| 117 | + timeout: The request timeout. |
| 118 | +
|
| 119 | + Returns: |
| 120 | + A Response populated from the httpx response. |
| 121 | + """ |
| 122 | + if isinstance(timeout, tuple): |
| 123 | + connect_timeout, read_timeout = timeout |
| 124 | + httpx_timeout = httpx.Timeout( |
| 125 | + connect=connect_timeout, |
| 126 | + read=read_timeout, |
| 127 | + write=None, |
| 128 | + pool=None, |
| 129 | + ) |
| 130 | + else: |
| 131 | + httpx_timeout = httpx.Timeout( |
| 132 | + connect=timeout, |
| 133 | + read=timeout, |
| 134 | + write=None, |
| 135 | + pool=None, |
| 136 | + ) |
| 137 | + |
| 138 | + httpx_response = httpx.request( |
| 139 | + method=method, |
| 140 | + url=url, |
| 141 | + headers=headers, |
| 142 | + content=data, |
| 143 | + timeout=httpx_timeout, |
| 144 | + follow_redirects=True, |
| 145 | + ) |
| 146 | + |
| 147 | + content = bytes(httpx_response.content) |
| 148 | + request_content = httpx_response.request.content |
| 149 | + |
| 150 | + return Response( |
| 151 | + text=httpx_response.text, |
| 152 | + url=str(object=httpx_response.url), |
| 153 | + status_code=httpx_response.status_code, |
| 154 | + headers=dict(httpx_response.headers), |
| 155 | + request_body=bytes(request_content) or None, |
| 156 | + tell_position=len(content), |
| 157 | + content=content, |
| 158 | + ) |
0 commit comments