python-gitlab
python-gitlab copied to clipboard
docs: How to use requests-cache together with gitlab?
This shows how to use reclosedev/requests-cache and could be added to the FAQs. Want a PR?
import gitlab as glapi
GITLAB_URL = "https://…"
class GitLab(glapi.Gitlab):
""" GitLab API.
* https://python-gitlab.readthedocs.io/en/stable/api/gitlab.html
* https://docs.gitlab.com/ce/api/#resources
"""
API_BASEURL = GITLAB_URL
CACHE_EXPIRATION = 2 * 60 * 60 # seconds
def __init__(self):
account, token = security.Credentials(self.API_BASEURL).auth_pair()
session = requests_cache.CachedSession(
cache_name=config.cache_file(type(self).__name__),
expire_after=self.CACHE_EXPIRATION)
with session.cache_disabled():
super().__init__(self.API_BASEURL, private_token=token, session=session)
def auth(self):
""" Performs a non-cached authentication.
"""
with self.session.cache_disabled():
return super().auth()
@jhermann Sorry for the late response. Yes, we're always open for merge requests, but I'm not sure that the docs would be in scope for python-gitlab. I'm afraid that it could confuse people, maybe?