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