Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CachedSession to be usable as a mixin class #183

Merged
merged 2 commits into from
Mar 5, 2021
Merged

Refactor CachedSession to be usable as a mixin class #183

merged 2 commits into from
Mar 5, 2021

Conversation

JWCook
Copy link
Member

@JWCook JWCook commented Mar 4, 2021

Closes:

See notes on those issues for more background info. See examples below for usage info.
Examples have also been added to the docs.

Example with requests-html (#149)

import requests
from requests_cache import CacheMixin
from requests_html import HTMLSession

class CachedHTMLSession(CacheMixin, HTMLSession):
    """Session with features from both CachedSession and HTMLSession"""

session = CachedHTMLSession()
r = session.get("https://github.com/")
print(r.from_cache, r.html.links)

Or, using the monkey-patch method:

from requests_cache import install_cache

install_cache(session_factory=CachedHTMLSession)
r = requests.get("https://github.com/")
print(r.from_cache, r.html.links)

Note that this usage would be more ideal if requests-html could also be used as a mixin.

Example with internetarchive (#158)

Pretty much the same as above:

import requests
from requests_cache import CacheMixin
from internetarchive.session import ArchiveSession

class CachedArchiveSession(CacheMixin, ArchiveSession):
    """Session with features from both CachedSession and ArchiveSession"""

Example with requests-mock (#87)

import requests
from requests_mock import Mocker
from requests_cache import CachedSession

session = CachedSession()
with Mocker(session=session) as m:
    m.get('http://test.com', text='mock_response')
    response = session.get('http://test.com')
    print(response.text)

@coveralls
Copy link

coveralls commented Mar 4, 2021

Coverage Status

Coverage increased (+1.2%) to 85.644% when pulling 0394a90 on JWCook:cache-mixin into bfe214e on reclosedev:master.

@JWCook JWCook added the bug label Mar 5, 2021
@JWCook JWCook marked this pull request as ready for review March 5, 2021 02:59
@JWCook
Copy link
Member Author

JWCook commented Mar 5, 2021

@kousu @jeremydouglass If you get the chance, test those changes out for me and let me know if that does the trick. Changes are in the current master branch, not yet released.

@JWCook JWCook deleted the cache-mixin branch March 6, 2021 01:27
@jeremydouglass
Copy link

Hi @JWCook, this is extremely exciting, and apologies for not seeing this earlier. I'll try to take a look shortly based on my original use case from Jan 2020 in #158

@JWCook
Copy link
Member Author

JWCook commented May 12, 2021

Thanks! Any feedback on that would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment