Skip to content

Commit

Permalink
[#139] Update aiohttp-client-cache to v0.10.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
netomi committed Oct 31, 2023
1 parent 2394381 commit 493c6ea
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 324 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

### Changed

- Updated library `aiohttp-client-cache` to v0.10.0 to support conditional requests natively. ([#139](https://gitlab.eclipse.org/eclipsefdn/security/otterdog/-/issues/139))
- Support renaming the current `default_branch` if the new branch does not exist yet. ([#76](https://gitlab.eclipse.org/eclipsefdn/security/otterdog/-/issues/76))
- Use async io for to speed up retrieval of current resources from GitHub. ([#114](https://gitlab.eclipse.org/eclipsefdn/security/otterdog/-/issues/114))
- Changed Operation `canonical-diff` to ignore ordering of keys.
Expand Down
85 changes: 15 additions & 70 deletions otterdog/providers/github/rest/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
from requests import Response
from requests_cache import CachedSession

from aiohttp import ClientSession
from aiohttp_client_cache.session import CachedSession as AsyncCachedSession
from aiohttp_client_cache.backends import FileBackend
from aiohttp_client_cache.cache_control import CacheActions

from otterdog.providers.github.exception import BadCredentialsException, GitHubException
from otterdog.utils import print_debug, print_trace, is_debug_enabled, is_trace_enabled
Expand Down Expand Up @@ -184,75 +182,22 @@ async def async_request_raw(

async with AsyncCachedSession(cache=FileBackend(cache_name=_AIOHTTP_CACHE_DIR, use_temp=False)) as session:
url = self._build_url(url_path)
key = session.cache.create_key(method, url, params=params)
cached_response = await session.cache.get_response(key)

if cached_response is not None and method == "GET":
# if the url is present in the cache, try to refresh it from the server
refresh_headers = headers.copy()

if "ETag" in cached_response.headers:
refresh_headers["If-None-Match"] = cached_response.headers["ETag"]

if "Last-Modified" in cached_response.headers:
refresh_headers["If-Modified-Since"] = cached_response.headers["Last-Modified"]

# the actual refresh must happen without a cache
# we initialize a new client session as the current version of the cache has a bug
# where responses are modify the cache even if it is disabled.
async with ClientSession() as nocache_session:
response = await nocache_session.request(
method,
url=url,
headers=refresh_headers,
params=params,
data=data,
)

if response.status == 304:
# we received a not-modified response, re-request the item from the cache
print_trace(
f"async '{method}' result = ({response.status}, {await response.text()}), "
f"not-modified, requesting from cache"
async with session.request(
method, url=url, headers=headers, params=params, data=data, refresh=True
) as response:
text = await response.text()
status = response.status

if is_debug_enabled():
if not response.from_cache: # type: ignore
print_debug(
f"'{method}' {url_path}: rate-limit-used = {response.headers.get('x-ratelimit-used', None)}"
)
pass
elif response.status == 200:
# update the cache with the received response and return it
actions = CacheActions.from_headers(key, response.headers)
await session.cache.save_response(response, key, actions.expires)

if is_trace_enabled():
print_trace(
f"async '{method}' result = ({response.status}, {await response.text()}), "
f"refreshing cache"
)

return response.status, await response.text()
else:
# in all other cases, remove the item from the cache
await session.cache.delete(key)

if is_trace_enabled():
print_trace(
f"async '{method}' result = ({response.status}, {await response.text()}), "
f"deleting from cache"
)

return response.status, await response.text()

response = await session.request(method, url=url, headers=headers, params=params, data=data)
if is_debug_enabled():
if not response.from_cache: # type: ignore
print_debug(
f"'{method}' {url_path}: rate-limit-used = {response.headers.get('x-ratelimit-used', None)}"
)

if is_trace_enabled():
print_trace(f"async '{method}' result = ({response.status}, {await response.text()})")

text = await response.text()
response.close()
return response.status, text

if is_trace_enabled():
print_trace(f"async '{method}' result = ({status}, {text})")

return status, text

def _check_response(self, url_path: str, status_code: int, body: str) -> None:
if status_code >= 400:
Expand Down
Loading

0 comments on commit 493c6ea

Please sign in to comment.