Skip to content

Commit

Permalink
Implement is_session_init_request()
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallaecio committed Jul 18, 2024
1 parent b927947 commit 08fca0b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/usage/session.rst
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ To define a different session config for a given URL pattern, install

.. autofunction:: scrapy_zyte_api.session_config

If in a session config implementation or in any other Scrapy component you need
to tell whether a request is a :ref:`session initialization request
<session-init>` or not, use :func:`~scrapy_zyte_api.is_session_init_request`:

.. autofunction:: scrapy_zyte_api.is_session_init_request

Classes decorated with :func:`~scrapy_zyte_api.session_config` are registered
into :data:`~scrapy_zyte_api.session_config_registry`:

Expand Down
1 change: 1 addition & 0 deletions scrapy_zyte_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from ._session import (
ScrapyZyteAPISessionDownloaderMiddleware,
SessionConfig,
is_session_init_request,
session_config,
)
from ._session import session_config_registry as _session_config_registry
Expand Down
10 changes: 10 additions & 0 deletions scrapy_zyte_api/_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
ZYTE_API_META_KEYS = ("zyte_api", "zyte_api_automap", "zyte_api_provider")


def is_session_init_request(request):
"""Return ``True`` if the request is a :ref:`session initialization request
<session-init>` or ``False`` otherwise."""
return request.meta.get(SESSION_INIT_META_KEY, False) is True

Check warning on line 43 in scrapy_zyte_api/_session.py

View check run for this annotation

Codecov / codecov/patch

scrapy_zyte_api/_session.py#L43

Added line #L43 was not covered by tests


class SessionRetryFactory(RetryFactory):
temporary_download_error_stop = stop_after_attempt(1)

Expand Down Expand Up @@ -362,6 +368,10 @@ def check(self, response: Response, request: Request) -> bool:
The default implementation checks the outcome of the ``setLocation``
action if a location was defined, as described in :ref:`session-check`.
If you need to tell whether *request* is a :ref:`session initialization
request <session-init>` or not, use
:func:`~scrapy_zyte_api.is_session_init_request`.
"""
if self._checker:
return self._checker.check(response, request)
Expand Down

0 comments on commit 08fca0b

Please sign in to comment.