Skip to content

Commit

Permalink
Merge pull request #193 from corenting/chore/generic-403-handling
Browse files Browse the repository at this point in the history
chore: generic 403 handling
  • Loading branch information
corenting authored Aug 24, 2024
2 parents a800e62 + df073a1 commit 7118239
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 294 deletions.
9 changes: 4 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ USER eddrit
# for some architectures directly so that they can be cached.
# To keep in sync with poetry.lock to speedup CI
RUN /usr/local/bin/pip install --user \
uvloop==0.19.0 \
lxml==5.2.2 \
uvloop==0.20.0 \
lxml==5.3.0 \
httptools==0.6.1 \
MarkupSafe==2.1.5 \
pyyaml==6.0.1 \
hiredis==2.3.2
pyyaml==6.0.2

# Dependencies
WORKDIR /app/
Expand All @@ -29,7 +28,7 @@ ENV CPPFLAGS=-I/usr/local/include/python3.12/ \
RUN /usr/local/bin/pip install --user .

# Prod image (app and default config)
FROM python:3.12-slim as prod
FROM python:3.12-slim AS prod

COPY --from=base /home/eddrit/.local /home/eddrit/.local
COPY --from=base /usr/bin/dumb-init /usr/bin/
Expand Down
13 changes: 3 additions & 10 deletions eddrit/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,9 @@ class SubredditNotFoundError(SubredditUnavailableError):
message = "Subreddit not found"


class SubredditIsPrivateError(SubredditUnavailableError):
message = "Subreddit is private"


class SubredditIsQuarantinedError(SubredditUnavailableError):
message = "Subreddit is quarantined"


class SubredditIsBannedError(SubredditUnavailableError):
message = "Subreddit is banned"
class SubredditCannotBeViewedError(SubredditUnavailableError):
def __init__(self, reason: str) -> None:
self.message = f"Subreddit is {reason}"


class RateLimitedError(Exception):
Expand Down
16 changes: 5 additions & 11 deletions eddrit/reddit/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
from eddrit import models
from eddrit.constants import REDDIT_BASE_API_URL
from eddrit.exceptions import (
SubredditIsBannedError,
SubredditIsPrivateError,
SubredditIsQuarantinedError,
SubredditCannotBeViewedError,
SubredditNotFoundError,
UserNotFoundError,
)
Expand Down Expand Up @@ -232,15 +230,11 @@ def _raise_if_subreddit_is_not_available(api_res: httpx.Response) -> None:

# Check for banned subreddits
if api_res.status_code == 404 and json.get("reason") == "banned":
raise SubredditIsBannedError()
raise SubredditCannotBeViewedError("banned")

# Check for quarantined subreddits
if api_res.status_code == 403 and json.get("reason") == "quarantined":
raise SubredditIsQuarantinedError()

# Check for private subreddits
if api_res.status_code == 403 and json.get("reason") == "private":
raise SubredditIsPrivateError()
# Check for subreddits that cannot be viewed (quarantine, privated, gated)
if api_res.status_code == 403 and (reason := json.get("reason")):
raise SubredditCannotBeViewedError(reason)

# Check for subreddit not found
if len(api_res.history) > 0 and api_res.history[0].status_code != 200:
Expand Down
4 changes: 2 additions & 2 deletions eddrit/utils/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def _get_login_headers_from_cache() -> dict[str, Any]:
Get the login headers from cache
"""
valkey_client = valkey.Valkey(connection_pool=_valkey_connection_pool)
with valkey_client.lock(_valkey_lock_key, blocking_timeout=5):
with valkey_client.lock(_valkey_lock_key, timeout=20, blocking_timeout=5):
# Check if we have login headers in cache
if not valkey_client.exists(_valkey_headers_key):
oauth_login()
Expand All @@ -133,7 +133,7 @@ def oauth_login() -> None:
logger.debug("Performing OAuth login")
valkey_client = valkey.Valkey(connection_pool=_valkey_connection_pool)

with valkey_client.lock(_valkey_lock_key, blocking_timeout=5):
with valkey_client.lock(_valkey_lock_key, timeout=20, blocking_timeout=5):
# Generate identity: unique ID + random user-agent
unique_uuid = str(uuid4())
android_app_version = random.choice(OFFICIAL_ANDROID_APP_VERSIONS) # noqa: S311
Expand Down
Loading

0 comments on commit 7118239

Please sign in to comment.