Skip to content

Commit a4f1dcc

Browse files
committed
move _perform_request() back into requests.py
1 parent d5cff9d commit a4f1dcc

File tree

2 files changed

+37
-31
lines changed

2 files changed

+37
-31
lines changed

web_poet/page_inputs/client.py

+1-31
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,22 @@
99
"""
1010

1111
import asyncio
12-
import logging
1312
from typing import Optional, Dict, List, Union, Callable
1413

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
1715
from web_poet.page_inputs.http import (
1816
HttpRequest,
1917
HttpRequestHeaders,
2018
HttpRequestBody,
2119
HttpResponse,
2220
)
2321

24-
logger = logging.getLogger(__name__)
2522

2623
_StrMapping = Dict[str, str]
2724
_Headers = Union[_StrMapping, HttpRequestHeaders]
2825
_Body = Union[bytes, HttpRequestBody]
2926

3027

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-
5828
class HttpClient:
5929
"""A convenient client to easily execute requests.
6030

web_poet/requests.py

+36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
1+
import logging
12
from contextvars import ContextVar
23

4+
from web_poet.exceptions import RequestBackendError
5+
from web_poet.page_inputs.http import (
6+
HttpRequest,
7+
HttpResponse,
8+
)
9+
10+
logger = logging.getLogger(__name__)
11+
312
# Frameworks that wants to support additional requests in ``web-poet`` should
413
# set the appropriate implementation for requesting data.
514
request_backend_var: ContextVar = ContextVar("request_backend")
15+
16+
17+
async def _perform_request(request: HttpRequest) -> HttpResponse:
18+
"""Given a :class:`~.Request`, execute it using the **request implementation**
19+
that was set in the ``web_poet.request_backend_var`` :mod:`contextvars`
20+
instance.
21+
22+
.. warning::
23+
By convention, this function should return a :class:`~.HttpResponse`.
24+
However, the underlying downloader assigned in
25+
``web_poet.request_backend_var`` might change that, depending on
26+
how the framework using **web-poet** implements it.
27+
"""
28+
29+
logger.info(f"Requesting page: {request}")
30+
31+
try:
32+
request_backend = request_backend_var.get()
33+
except LookupError:
34+
raise RequestBackendError(
35+
"Additional requests are used inside the Page Object but the "
36+
"current framework has not set any HttpRequest Backend via "
37+
"'web_poet.request_backend_var'"
38+
)
39+
40+
response_data: HttpResponse = await request_backend(request)
41+
return response_data

0 commit comments

Comments
 (0)