From aa7200d1c8b23aa1e8dc13854bb728a979496d2d Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Wed, 22 Sep 2021 07:43:26 +0200 Subject: [PATCH] [IMP] conftest: use conditional requests for PR data Currently any access to a PR's information triggers an unconditional fetch. Cache the result and the conditional request data so we can perform conditional requests on accesses 1+. And this could lead to somewhat lower API usage as according to github conditional requests should not count towards rate limit use. --- conftest.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/conftest.py b/conftest.py index 2bb1d779..842e0743 100644 --- a/conftest.py +++ b/conftest.py @@ -805,21 +805,33 @@ class PR: self.repo = repo self.number = number self.labels = LabelsProxy(self) + self._cache = None, {} @property def _pr(self): - r = self.repo._session.get('https://api.github.com/repos/{}/pulls/{}'.format(self.repo.name, self.number)) - assert 200 <= r.status_code < 300, r.json() - return r.json() + previous, caching = self._cache + r = self.repo._session.get( + 'https://api.github.com/repos/{}/pulls/{}'.format(self.repo.name, self.number), + headers=caching + ) + assert r.ok, r.json() + if r.status_code == 304: + return previous + contents, caching = self._cache = r.json(), {} + if r.headers.get('etag'): + caching['If-None-Match'] = r.headers['etag'] + if r.headers.get('last-modified'): + caching['If-Modified-Since']= r.headers['Last-Modified'] + return contents @property def title(self): - raise NotImplementedError() + return self._pr['title'] title = title.setter(lambda self, v: self._set_prop('title', v)) @property def base(self): - raise NotImplementedError() + return self._pr['base'] base = base.setter(lambda self, v: self._set_prop('base', v)) @property