Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Add throttling classes for thumbnail endpoint #864

Merged
merged 2 commits into from
Aug 16, 2022
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
9 changes: 9 additions & 0 deletions api/catalog/api/utils/throttle.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class OneThousandPerMinute(AbstractAnonRateThrottle):
rate = "1000/min"


class OneHundredFiftyPerMinute(AbstractAnonRateThrottle):
rate = "150/minute"
krysal marked this conversation as resolved.
Show resolved Hide resolved


class OnePerSecond(AbstractAnonRateThrottle):
rate = "1/second"

Expand Down Expand Up @@ -90,6 +94,11 @@ def get_cache_key(self, request, view):
return self.cache_format % {"scope": self.scope, "ident": ident}


class OAuth2IdThumbsRateThrottle(AbstractOAuth2IdRateThrottle):
applies_to_rate_limit_model = "standard"
scope = "oauth2_client_credentials_thumbs"


class OAuth2IdSustainedRateThrottle(AbstractOAuth2IdRateThrottle):
applies_to_rate_limit_model = "standard"
scope = "oauth2_client_credentials_sustained"
Expand Down
7 changes: 5 additions & 2 deletions api/catalog/api/views/image_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
)
from catalog.api.serializers.media_serializers import MediaThumbnailRequestSerializer
from catalog.api.utils.exceptions import get_api_exception
from catalog.api.utils.throttle import OneThousandPerMinute
from catalog.api.utils.throttle import (
OAuth2IdThumbsRateThrottle,
OneHundredFiftyPerMinute,
)
from catalog.api.utils.watermark import watermark
from catalog.api.views.media_views import MediaViewSet

Expand Down Expand Up @@ -93,7 +96,7 @@ def oembed(self, request, *_, **__):
url_path="thumb",
url_name="thumb",
serializer_class=MediaThumbnailRequestSerializer,
throttle_classes=[OneThousandPerMinute],
throttle_classes=[OneHundredFiftyPerMinute, OAuth2IdThumbsRateThrottle],
)
def thumbnail(self, request, *_, **__):
image = self.get_object()
Expand Down
3 changes: 3 additions & 0 deletions api/catalog/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@

THROTTLE_ANON_BURST = config("THROTTLE_ANON_BURST", default="5/hour")
THROTTLE_ANON_SUSTAINED = config("THROTTLE_ANON_SUSTAINED", default="100/day")
THROTTLE_OAUTH2_THUMBS = config("THROTTLE_OAUTH2_THUMBS", default="500/minute")

REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": (
Expand All @@ -143,6 +144,7 @@
"DEFAULT_THROTTLE_CLASSES": (
"catalog.api.utils.throttle.BurstRateThrottle",
"catalog.api.utils.throttle.SustainedRateThrottle",
"catalog.api.utils.throttle.OAuth2IdThumbsRateThrottle",
"catalog.api.utils.throttle.OAuth2IdSustainedRateThrottle",
"catalog.api.utils.throttle.OAuth2IdBurstRateThrottle",
"catalog.api.utils.throttle.EnhancedOAuth2IdSustainedRateThrottle",
Expand All @@ -152,6 +154,7 @@
"DEFAULT_THROTTLE_RATES": {
"anon_burst": THROTTLE_ANON_BURST,
"anon_sustained": THROTTLE_ANON_SUSTAINED,
"oauth2_client_credentials_thumbs": THROTTLE_OAUTH2_THUMBS,
"oauth2_client_credentials_sustained": "10000/day",
"oauth2_client_credentials_burst": "100/min",
"enhanced_oauth2_client_credentials_sustained": "20000/day",
Expand Down