Skip to content

Commit

Permalink
Make 'rates' a non-variable argument so it can work with a mixin sup…
Browse files Browse the repository at this point in the history
…erclass
  • Loading branch information
JWCook committed Sep 4, 2021
1 parent f94e2a4 commit c0a8653
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ from requests_cache import CacheMixin, RedisCache
from requests_ratelimiter import LimiterMixin


class CachedLimiterSession(LimiterMixin, CacheMixin, Session):
class CachedLimiterSession(CacheMixin, LimiterMixin, Session):
"""Session class with caching and rate-limiting behavior. Accepts arguments for both
LimiterSession and CachedSession.
"""


# Optionally use Redis as both the bucket backend and the cache backend
session = CachedLimiterSession(
RequestRate(5, Duration.SECOND),
rates=RequestRate(5, Duration.SECOND),
bucket_class=RedisBucket,
backend=RedisCache(),
)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "requests-ratelimiter"
version = "0.1.1"
version = "0.2.0"
description = "Rate-limiting for the requests library"
authors = ["Jordan Cook"]
license = "MIT"
Expand Down
7 changes: 5 additions & 2 deletions requests_ratelimiter/requests_ratelimiter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Dict, Type
from typing import TYPE_CHECKING, Dict, Iterable, Type, Union
from urllib.parse import urlparse
from uuid import uuid4

Expand Down Expand Up @@ -29,7 +29,7 @@ class LimiterMixin(MixinBase):

def __init__(
self,
*rates: RequestRate,
rates: Union[RequestRate, Iterable[RequestRate]],
bucket_class: Type[AbstractBucket] = MemoryQueueBucket,
bucket_kwargs: Dict = None,
per_host: bool = False,
Expand All @@ -39,6 +39,9 @@ def __init__(
bucket_kwargs = bucket_kwargs or {}
bucket_kwargs.setdefault('bucket_name', self._default_bucket)

if not isinstance(rates, Iterable):
rates = [rates]

self.limiter = Limiter(*rates, bucket_class=bucket_class, bucket_kwargs=bucket_kwargs)
self.per_host = per_host
super().__init__(**kwargs)
Expand Down

0 comments on commit c0a8653

Please sign in to comment.