|
9 | 9 | """
|
10 | 10 |
|
11 | 11 | import asyncio
|
12 |
| -import logging |
13 | 12 | from typing import Optional, Dict, List, Union, Callable
|
14 | 13 |
|
15 |
| -from web_poet.requests import request_backend_var |
16 |
| -from web_poet.exceptions import RequestBackendError |
| 14 | +from web_poet.requests import request_backend_var, _perform_request |
17 | 15 | from web_poet.page_inputs.http import (
|
18 | 16 | HttpRequest,
|
19 | 17 | HttpRequestHeaders,
|
20 | 18 | HttpRequestBody,
|
21 | 19 | HttpResponse,
|
22 | 20 | )
|
23 | 21 |
|
24 |
| -logger = logging.getLogger(__name__) |
25 | 22 |
|
26 | 23 | _StrMapping = Dict[str, str]
|
27 | 24 | _Headers = Union[_StrMapping, HttpRequestHeaders]
|
28 | 25 | _Body = Union[bytes, HttpRequestBody]
|
29 | 26 |
|
30 | 27 |
|
31 |
| -async def _perform_request(request: HttpRequest) -> HttpResponse: |
32 |
| - """Given a :class:`~.Request`, execute it using the **request implementation** |
33 |
| - that was set in the ``web_poet.request_backend_var`` :mod:`contextvars` |
34 |
| - instance. |
35 |
| -
|
36 |
| - .. warning:: |
37 |
| - By convention, this function should return a :class:`~.HttpResponse`. |
38 |
| - However, the underlying downloader assigned in |
39 |
| - ``web_poet.request_backend_var`` might change that, depending on |
40 |
| - how the framework using **web-poet** implements it. |
41 |
| - """ |
42 |
| - |
43 |
| - logger.info(f"Requesting page: {request}") |
44 |
| - |
45 |
| - try: |
46 |
| - request_backend = request_backend_var.get() |
47 |
| - except LookupError: |
48 |
| - raise RequestBackendError( |
49 |
| - "Additional requests are used inside the Page Object but the " |
50 |
| - "current framework has not set any HttpRequest Backend via " |
51 |
| - "'web_poet.request_backend_var'" |
52 |
| - ) |
53 |
| - |
54 |
| - response_data: HttpResponse = await request_backend(request) |
55 |
| - return response_data |
56 |
| - |
57 |
| - |
58 | 28 | class HttpClient:
|
59 | 29 | """A convenient client to easily execute requests.
|
60 | 30 |
|
|
0 commit comments