Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 7 additions & 1 deletion rdt_cli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .config import DEFAULT_CONFIG, RuntimeConfig
from .constants import (
ALL_URL,
API_FALLBACK_BASE_URL,
COMMENT_URL,
DEFAULT_LIMIT,
HOME_URL,
Expand All @@ -30,6 +31,7 @@
VOTE_URL,
)
from .exceptions import (
ForbiddenError,
RedditApiError,
)
from .fingerprint import BrowserFingerprint
Expand Down Expand Up @@ -262,7 +264,11 @@ def search(
}
if after:
params["after"] = after
return self._get(url, params=params)
try:
return self._get(url, params=params)
except ForbiddenError:
logger.info("search.json blocked, retrying via %s", API_FALLBACK_BASE_URL)
return self._get(API_FALLBACK_BASE_URL + url.removesuffix(".json"), params=params)

# ── User ────────────────────────────────────────────────────────

Expand Down
3 changes: 3 additions & 0 deletions rdt_cli/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
# Search
SEARCH_URL = "/search.json"
SUBREDDIT_SEARCH_URL = "/r/{subreddit}/search.json"
# Reddit answers 403 for www.reddit.com/search.json; the api. host still serves
# the same Listing JSON for the same cookies (jackwener/rdt-cli#27).
API_FALLBACK_BASE_URL = "https://api.reddit.com"

# User
USER_ABOUT_URL = "/user/{username}/about.json"
Expand Down
41 changes: 40 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

from unittest.mock import patch

import pytest

from rdt_cli.auth import Credential
from rdt_cli.client import RedditClient
from rdt_cli.exceptions import RedditApiError
from rdt_cli.exceptions import ForbiddenError, RedditApiError


def test_validate_session_success_updates_username_and_capabilities() -> None:
Expand Down Expand Up @@ -67,3 +69,40 @@ def test_get_more_comments_uses_api_morechildren() -> None:
"raw_json": 1,
},
)


def test_search_falls_back_to_api_host_when_search_json_is_forbidden() -> None:
cred = Credential(cookies={"reddit_session": "abc"})
with RedditClient(cred) as client:
with patch.object(client, "_get", side_effect=[ForbiddenError(), {"ok": True}]) as mock_get:
data = client.search("test", limit=5)

assert data == {"ok": True}
assert mock_get.call_count == 2
first, second = mock_get.call_args_list
assert first.args[0] == "/search.json"
assert second.args[0] == "https://api.reddit.com/search"
assert first.kwargs["params"] == second.kwargs["params"]


def test_subreddit_search_falls_back_to_api_host_when_search_json_is_forbidden() -> None:
cred = Credential(cookies={"reddit_session": "abc"})
with RedditClient(cred) as client:
with patch.object(client, "_get", side_effect=[ForbiddenError(), {"ok": True}]) as mock_get:
data = client.search("asyncio", subreddit="python", limit=3)

assert data == {"ok": True}
first, second = mock_get.call_args_list
assert first.args[0] == "/r/python/search.json"
assert second.args[0] == "https://api.reddit.com/r/python/search"
assert second.kwargs["params"]["restrict_sr"] == "on"


def test_search_does_not_fall_back_on_other_errors() -> None:
cred = Credential(cookies={"reddit_session": "abc"})
with RedditClient(cred) as client:
with patch.object(client, "_get", side_effect=RedditApiError("boom")) as mock_get:
with pytest.raises(RedditApiError):
client.search("test")

assert mock_get.call_count == 1