twelvedata / twelvedata-python

Twelve Data Python Client - Financial data API & WebSocket
https://twelvedata.com
MIT License
392 stars 57 forks source link

Allow user to control requests session #89

Closed Ax6 closed 2 months ago

Ax6 commented 2 months ago

Unless there is a better way already in place (If so, I'd be interested to know how), this PR would allow the user to have more control on the requests session: caching, rate limiter etc...

from pyrate_limiter import Duration, RequestRate, Limiter
from requests_ratelimiter import LimiterMixin, SQLiteBucket
from requests_cache import CacheMixin, SQLiteCache
from requests import Session

class CachedLimiterSession(CacheMixin, LimiterMixin, Session):
    pass

session = CachedLimiterSession(
    limiter=Limiter(
        RequestRate(8, Duration.MINUTE), RequestRate(800, Duration.DAY)
    ),
    expire_after=timedelta(hours=48),
    bucket_class=SQLiteBucket,
    backend=SQLiteCache(".cache/requests.db"),
)

twelve_data_client = TDClient(apikey=TWELVEDATA_KEY)
twelve_data_client.ctx.http_client.session = session
sgacode commented 2 months ago

Hi. Thank you. Merged, tests updated. New version published 1.2.24