Skip to content

Commit a9d5bff

Browse files
authored
Improve the pool API and default behavior, and change param precedence (#207)
1 parent 7266e87 commit a9d5bff

File tree

6 files changed

+940
-219
lines changed

6 files changed

+940
-219
lines changed

CHANGES.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
11
Changes
22
=======
33

4+
Unreleased
5+
----------
6+
7+
* The precedence of session param settings, request metadata keys and session
8+
config override methods has changed.
9+
10+
Before, priority from higher to lower was:
11+
12+
#. :meth:`~scrapy_zyte_api.SessionConfig.params`
13+
14+
#. :meth:`~scrapy_zyte_api.SessionConfig.location`
15+
16+
#. :reqmeta:`zyte_api_session_location`
17+
18+
#. :setting:`ZYTE_API_SESSION_LOCATION`
19+
20+
#. :reqmeta:`zyte_api_session_params`
21+
22+
#. :setting:`ZYTE_API_SESSION_PARAMS`
23+
24+
Now, it is:
25+
26+
#. :reqmeta:`zyte_api_session_params`
27+
28+
#. :reqmeta:`zyte_api_session_location`
29+
30+
#. :setting:`ZYTE_API_SESSION_PARAMS`
31+
32+
#. :setting:`ZYTE_API_SESSION_LOCATION`
33+
34+
#. :meth:`~scrapy_zyte_api.SessionConfig.location`
35+
36+
#. :meth:`~scrapy_zyte_api.SessionConfig.params`
37+
38+
439
0.20.0 (2024-06-26)
540
-------------------
641

docs/reference/meta.rst

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ Default: :setting:`ZYTE_API_SESSION_ENABLED`
9898
Whether to use :ref:`scrapy-zyte-api session management <session>` for the
9999
request (``True``) or not (``False``).
100100

101+
.. seealso:: :meth:`scrapy_zyte_api.SessionConfig.enabled`
102+
101103

102104
.. reqmeta:: zyte_api_session_location
103105

@@ -106,13 +108,19 @@ zyte_api_session_location
106108

107109
Default: ``{}``
108110

109-
Address for ``setLocation``-based session initialization. See
110-
:setting:`ZYTE_API_SESSION_LOCATION` for details.
111+
See :ref:`session-init` for general information about location configuration
112+
and parameter precedence.
113+
114+
Example:
111115

112-
This request metadata key, if not empty, takes precedence over the
113-
:setting:`ZYTE_API_SESSION_LOCATION` setting, the
114-
:setting:`ZYTE_API_SESSION_PARAMS` setting, and the
115-
:reqmeta:`zyte_api_session_location` request metadata key.
116+
.. code-block:: python
117+
118+
Request(
119+
"https://example.com",
120+
meta={
121+
"zyte_api_session_location": {"postalCode": "10001"},
122+
},
123+
)
116124
117125
118126
.. reqmeta:: zyte_api_session_params
@@ -122,10 +130,18 @@ zyte_api_session_params
122130

123131
Default: ``{}``
124132

125-
Parameters to use for session initialization. See
126-
:setting:`ZYTE_API_SESSION_PARAMS` for details.
133+
See :ref:`session-init` for general information about defining session
134+
initialization parameters and parameter precedence.
135+
136+
137+
.. reqmeta:: zyte_api_session_pool
138+
139+
zyte_api_session_pool
140+
=====================
141+
142+
Default: ``""``
143+
144+
Determines the ID of the session pool to assign to the request, overriding the
145+
:ref:`default pool assignment logic <session-pools>`.
127146

128-
This request metadata key, if not empty, takes precedence over the
129-
:setting:`ZYTE_API_SESSION_PARAMS` setting, but it can be overridden
130-
by the :setting:`ZYTE_API_SESSION_LOCATION` setting or the
131-
:reqmeta:`zyte_api_session_location` request metadata key.
147+
.. seealso:: :meth:`scrapy_zyte_api.SessionConfig.pool`

docs/reference/settings.rst

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -354,30 +354,16 @@ ZYTE_API_SESSION_LOCATION
354354

355355
Default: ``{}``
356356

357-
If defined, sessions are initialized using the ``setLocation``
358-
:http:`action <request:actions>`, and the value of this setting must be the
359-
target address :class:`dict`. For example:
357+
See :ref:`session-init` for general information about location configuration
358+
and parameter precedence.
359+
360+
Example:
360361

361362
.. code-block:: python
362363
:caption: settings.py
363364
364365
ZYTE_API_SESSION_LOCATION = {"postalCode": "10001"}
365366
366-
If the :setting:`ZYTE_API_SESSION_PARAMS` setting or the
367-
:reqmeta:`zyte_api_session_params` request metadata key set a ``"url"``, it
368-
will be used for session initialization as well. Otherwise, the URL of the
369-
request for which the session is being initialized will be used instead.
370-
371-
This setting, if not empty, takes precedence over the
372-
:setting:`ZYTE_API_SESSION_PARAMS` setting and the
373-
:reqmeta:`zyte_api_session_params` request metadata key, but it can be
374-
overridden by the :reqmeta:`zyte_api_session_location` request metadata key.
375-
376-
To disable the :setting:`ZYTE_API_SESSION_LOCATION` setting on a specific
377-
request, e.g. to use the :setting:`ZYTE_API_SESSION_PARAMS` setting or the
378-
:reqmeta:`zyte_api_session_params` request metadata key instead, set
379-
the :reqmeta:`zyte_api_session_location` request metadata key to ``{}``.
380-
381367
382368
.. setting:: ZYTE_API_SESSION_MAX_BAD_INITS
383369

@@ -430,20 +416,10 @@ to work even after an unsuccessful response. See :ref:`optimize-sessions`.
430416
ZYTE_API_SESSION_PARAMS
431417
=======================
432418

433-
Default: ``{"browserHtml": True}``
434-
435-
Parameters to use for session initialization.
436-
437-
It works similarly to :http:`request:sessionContextParams` from
438-
:ref:`server-managed sessions <zyte-api-session-contexts>`, but it supports
439-
arbitrary Zyte API parameters instead of a specific subset.
440-
441-
If it does not define a ``"url"``, the URL of the request for which the session
442-
is being initialized will be used.
419+
Default: ``{}``
443420

444-
This setting can be overridden by the :setting:`ZYTE_API_SESSION_LOCATION`
445-
setting, the :reqmeta:`zyte_api_session_location` request metadata key, or the
446-
:reqmeta:`zyte_api_session_params` request metadata key.
421+
See :ref:`session-init` for general information about defining session
422+
initialization parameters and parameter precedence.
447423

448424
Example:
449425

0 commit comments

Comments
 (0)