-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for conditional requests (#188)
* Basic version of conditional requests. * Some fixes. * Improve github example. * Some cleanups, add documentation. * Add future annotations to avoid type issues with older python versions. * Add test for conditional requests. * Make flake happy. * Use typing.Tuple. * AsyncMock is not available for python 3.7. * Addd proper decorator to skip tests for py37. * Add test case when conditional requests are not supported.
- Loading branch information
Showing
5 changed files
with
189 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env python3 | ||
# fmt: off | ||
""" | ||
An example of making conditional requests to the GitHub Rest API` | ||
""" | ||
|
||
import asyncio | ||
import logging | ||
|
||
from aiohttp_client_cache import CachedSession, FileBackend | ||
|
||
CACHE_DIR = "~/.cache/aiohttp-requests" | ||
|
||
|
||
async def main(): | ||
cache = FileBackend(cache_name=CACHE_DIR, use_temp=True) | ||
await cache.clear() | ||
|
||
org = "requests-cache" | ||
url = f"https://api.github.com/orgs/{org}/repos" | ||
|
||
# we make 2 requests for the same resource (list of all repos of the requests-cache organization) | ||
# the second request refreshes the cached response with the remote server | ||
# the debug output should illustrate that the cached response gets refreshed | ||
async with CachedSession(cache=cache) as session: | ||
response = await session.get(url) | ||
print(f"url = {response.url}, status = {response.status}, " | ||
f"ratelimit-used = {response.headers['x-ratelimit-used']}") | ||
|
||
await asyncio.sleep(1) | ||
|
||
response = await session.get(url, refresh=True) | ||
print(f"url = {response.url}, status = {response.status}, " | ||
f"ratelimit-used = {response.headers['x-ratelimit-used']}") | ||
|
||
|
||
if __name__ == "__main__": | ||
logging.basicConfig(level=logging.INFO) | ||
logging.getLogger("aiohttp_client_cache").setLevel(logging.DEBUG) | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters