Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail on SPM + transparent mode #152

Merged
merged 7 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions scrapy_zyte_api/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,29 @@ def __init__(
raise NotConfigured(
"Zyte API is disabled. Set ZYTE_API_ENABLED to True to enable it."
)
if settings.getbool("ZYTE_API_TRANSPARENT_MODE", False) and (
settings.getbool("ZYTE_SMARTPROXY_ENABLED", False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible to disable SPM using spider attributes; how do we handle this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we move this to a signal handler for spider_opened, where we can check the spider as well, and close the spider from there in this scenario?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I think there is also a middleware method (is_enabled?) which can be useful, but I haven't really checked it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or settings.getbool("CRAWLERA_ENABLED", False)
):
spm_setting = (
"ZYTE_SMARTPROXY_ENABLED"
if settings.getbool("ZYTE_SMARTPROXY_ENABLED", False)
else "CRAWLERA_ENABLED"
)
raise NotConfigured(
f"Both ZYTE_API_TRANSPARENT_MODE and {spm_setting} are "
f"enabled. You should only enable one of those at the same "
f"time.\n"
f"\n"
f"To combine requests that use Zyte API and requests that use "
f"Zyte Smart Proxy Manager in the same spider:\n"
f"\n"
f"1. Leave {spm_setting} as True.\n"
f"2. Unset ZYTE_API_TRANSPARENT_MODE or set it to False.\n"
f"3. To send a specific request through Zyte API, use "
f"request.meta to set dont_proxy to True and zyte_api_automap "
f"either to True or to a dictionary of extra request fields."
)
if not hasattr(crawler, "zyte_api_client"):
if not client:
client = self._build_client(settings)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,23 @@ def parse(self, response):
await crawler.crawl()

assert crawler.stats.get_value("finish_reason") == "zyte_api_suspended_account"


@pytest.mark.parametrize(
"spm_setting",
("ZYTE_SMARTPROXY_ENABLED", "CRAWLERA_ENABLED"),
)
def test_spm_conflict(spm_setting):
settings = {
"ZYTE_API_TRANSPARENT_MODE": True,
spm_setting: True,
**SETTINGS,
}
crawler = get_crawler(settings_dict=settings)

with pytest.raises(NotConfigured):
create_instance(
ScrapyZyteAPIDownloadHandler,
settings=None,
crawler=crawler,
)
Loading