Skip to content

Commit

Permalink
Change names of extension points. Refactore HttpClient:prepare_reques…
Browse files Browse the repository at this point in the history
…t method.
  • Loading branch information
Some User committed Dec 26, 2022
1 parent fc17ef5 commit 91d9657
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
8 changes: 4 additions & 4 deletions grab/base.py
Expand Up @@ -45,9 +45,9 @@ class BaseResponse:

class BaseExtension(Generic[RequestT, ResponseT], metaclass=ABCMeta):
ext_handlers: Mapping[
Literal["prepare_request_post"]
Literal["request:pre"]
| Literal["request_cookies"]
| Literal["response_post"]
| Literal["response:post"]
| Literal["init-retry"]
| Literal["retry"],
Callable[..., Any],
Expand Down Expand Up @@ -85,9 +85,9 @@ class BaseClient(Generic[RequestT, ResponseT], metaclass=ABCMeta):

extensions: MutableMapping[str, MutableMapping[str, Any]] = {}
ext_handlers: Mapping[str, list[Callable[..., Any]]] = {
"prepare_request_post": [],
"request:pre": [],
"request_cookies": [],
"response_post": [],
"response:post": [],
"init-retry": [],
"retry": [],
}
Expand Down
12 changes: 5 additions & 7 deletions grab/client.py
Expand Up @@ -49,18 +49,14 @@ def prepare_request(self, request_config: MutableMapping[str, Any]) -> HttpReque
This method is called before doing real request via transport extension.
"""
self.transport.reset()
cfg = copy(request_config)
if cfg.get("url") is None:
raise ValueError("Request could not be instantiated with no URL")
if not cfg.get("method"):
cfg["method"] = "GET"
if cfg.get("follow_location") is None:
cfg["follow_location"] = True
req = HttpRequest.create_from_mapping(cfg)
for func in self.ext_handlers["prepare_request_post"]:
func(req)
return req
return HttpRequest.create_from_mapping(cfg)

def get_request_cookies(self, req: HttpRequest) -> CookieJar:
jar = CookieJar()
Expand All @@ -76,10 +72,12 @@ def request(
assert isinstance(req, str)
request_kwargs["url"] = req
req = self.prepare_request(request_kwargs)
# redir_count = 0
retry = Retry()
all(x(retry) for x in self.ext_handlers["init-retry"])
while True:
for func in self.ext_handlers["request:pre"]:
func(req)
self.transport.reset()
self.transport.request(req, self.get_request_cookies(req))
with self.transport.wrap_transport_error():
doc = self.process_request_result(req)
Expand All @@ -97,7 +95,7 @@ def request(
def process_request_result(self, req: HttpRequest) -> Document:
"""Process result of real request performed via transport extension."""
doc = self.transport.prepare_response(req, document_class=self.document_class)
for func in self.ext_handlers["response_post"]:
for func in self.ext_handlers["response:post"]:
func(req, doc)
return doc

Expand Down
4 changes: 2 additions & 2 deletions grab/extensions.py
Expand Up @@ -52,9 +52,9 @@ class CookiesExtension(BaseExtension[HttpRequest, Document]):
def __init__(self, cookiejar: None | CookieJar = None) -> None:
self.cookiejar = cookiejar if cookiejar else CookieJar()
self.ext_handlers = {
"prepare_request_post": self.process_prepare_request_post,
"request:pre": self.process_prepare_request_post,
"request_cookies": self.process_request_cookies,
"response_post": self.process_response_post,
"response:post": self.process_response_post,
}

def set_cookie(self, cookie: Cookie) -> None:
Expand Down

0 comments on commit 91d9657

Please sign in to comment.