Executes a request, asynchronously returning an `HTTPResponse`. The request may be either a string URL or an `HTTPRequest` object. If it is a string, we construct an `HTTPRequest` using any additional kwargs: ``HTTPRequest(request, **kwargs)`` This method returns a
(
self,
request: Union[str, "HTTPRequest"],
raise_error: bool = True,
**kwargs: Any
)
| 247 | raise RuntimeError("inconsistent AsyncHTTPClient cache") |
| 248 | |
| 249 | def fetch( |
| 250 | self, |
| 251 | request: Union[str, "HTTPRequest"], |
| 252 | raise_error: bool = True, |
| 253 | **kwargs: Any |
| 254 | ) -> "Future[HTTPResponse]": |
| 255 | """Executes a request, asynchronously returning an `HTTPResponse`. |
| 256 | |
| 257 | The request may be either a string URL or an `HTTPRequest` object. |
| 258 | If it is a string, we construct an `HTTPRequest` using any additional |
| 259 | kwargs: ``HTTPRequest(request, **kwargs)`` |
| 260 | |
| 261 | This method returns a `.Future` whose result is an |
| 262 | `HTTPResponse`. By default, the ``Future`` will raise an |
| 263 | `HTTPError` if the request returned a non-200 response code |
| 264 | (other errors may also be raised if the server could not be |
| 265 | contacted). Instead, if ``raise_error`` is set to False, the |
| 266 | response will always be returned regardless of the response |
| 267 | code. |
| 268 | |
| 269 | If a ``callback`` is given, it will be invoked with the `HTTPResponse`. |
| 270 | In the callback interface, `HTTPError` is not automatically raised. |
| 271 | Instead, you must check the response's ``error`` attribute or |
| 272 | call its `~HTTPResponse.rethrow` method. |
| 273 | |
| 274 | .. versionchanged:: 6.0 |
| 275 | |
| 276 | The ``callback`` argument was removed. Use the returned |
| 277 | `.Future` instead. |
| 278 | |
| 279 | The ``raise_error=False`` argument only affects the |
| 280 | `HTTPError` raised when a non-200 response code is used, |
| 281 | instead of suppressing all errors. |
| 282 | """ |
| 283 | if self._closed: |
| 284 | raise RuntimeError("fetch() called on closed AsyncHTTPClient") |
| 285 | if not isinstance(request, HTTPRequest): |
| 286 | request = HTTPRequest(url=request, **kwargs) |
| 287 | else: |
| 288 | if kwargs: |
| 289 | raise ValueError( |
| 290 | "kwargs can't be used if request is an HTTPRequest object" |
| 291 | ) |
| 292 | # We may modify this (to add Host, Accept-Encoding, etc), |
| 293 | # so make sure we don't modify the caller's object. This is also |
| 294 | # where normal dicts get converted to HTTPHeaders objects. |
| 295 | request.headers = httputil.HTTPHeaders(request.headers) |
| 296 | request_proxy = _RequestProxy(request, self.defaults) |
| 297 | future = Future() # type: Future[HTTPResponse] |
| 298 | |
| 299 | def handle_response(response: "HTTPResponse") -> None: |
| 300 | if response.error: |
| 301 | if raise_error or not response._error_is_response_code: |
| 302 | future_set_exception_unless_cancelled(future, response.error) |
| 303 | return |
| 304 | future_set_result_unless_cancelled(future, response) |
| 305 | |
| 306 | self.fetch_impl(cast(HTTPRequest, request_proxy), handle_response) |
nothing calls this directly
no test coverage detected