From ef3841a1be37d71f33994874eb809089ccd2fe4b Mon Sep 17 00:00:00 2001 From: gizmo385 Date: Tue, 16 Jul 2024 05:44:28 +0000 Subject: [PATCH] Swap out httpx-cache for hishel --- lazy_github/lib/github/client.py | 19 +++++++++++++------ pyproject.toml | 2 +- requirements.txt | 25 ++++++++----------------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/lazy_github/lib/github/client.py b/lazy_github/lib/github/client.py index 9b599c3..640cad8 100644 --- a/lazy_github/lib/github/client.py +++ b/lazy_github/lib/github/client.py @@ -1,21 +1,28 @@ -import httpx_cache +from typing import Optional + +import hishel from lazy_github.lib.config import Config from lazy_github.lib.github.constants import JSON_CONTENT_ACCEPT_TYPE from lazy_github.models.github import User -class GithubClient(httpx_cache.AsyncClient): +class GithubClient(hishel.AsyncCacheClient): def __init__(self, config: Config, access_token: str) -> None: - cache = httpx_cache.FileCache(cache_dir=config.cache.cache_directory) - super().__init__(cache=cache, base_url=config.api.base_url) + storage = hishel.AsyncFileStorage(base_path=config.cache.cache_directory) + super().__init__(storage=storage, base_url=config.api.base_url) self.config = config self.access_token = access_token self._user: User | None = None - def headers_with_auth_accept(self, accept: str = JSON_CONTENT_ACCEPT_TYPE) -> dict[str, str]: + def headers_with_auth_accept( + self, accept: str = JSON_CONTENT_ACCEPT_TYPE, cache_duration: Optional[int] = None + ) -> dict[str, str]: """Helper function to build a request with specific headers""" - return {"Accept": accept, "Authorization": f"Bearer {self.access_token}"} + headers = {"Accept": accept, "Authorization": f"Bearer {self.access_token}"} + max_age = cache_duration or self.config.cache.default_ttl + headers["Cache-Control"] = f"max-age={max_age}" + return headers async def user(self) -> User: """Returns the authed user for this client""" diff --git a/pyproject.toml b/pyproject.toml index c54f54f..cc6b83e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ requires-python = ">=3.11" dependencies = [ "PyGithub", "httpx", - "httpx-cache", + "hishel", "pydantic", "textual", "textual-dev", diff --git a/requirements.txt b/requirements.txt index 00f1093..e69746b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,21 +1,15 @@ # This file was autogenerated by uv via the following command: -# uv pip compile pyproject.toml -o requirements.txt +# uv pip compile -o requirements.txt pyproject.toml aiohttp==3.9.5 # via textual-dev -aiorwlock==1.4.0 - # via httpx-cache aiosignal==1.3.1 # via aiohttp annotated-types==0.7.0 # via pydantic anyio==3.7.1 - # via - # httpx - # httpx-cache + # via httpx attrs==23.2.0 - # via - # aiohttp - # httpx-cache + # via aiohttp certifi==2024.6.2 # via # httpcore @@ -33,22 +27,20 @@ cryptography==42.0.8 # via pyjwt deprecated==1.2.14 # via pygithub -fasteners==0.17.3 - # via httpx-cache frozenlist==1.4.1 # via # aiohttp # aiosignal h11==0.14.0 # via httpcore +hishel==0.0.30 + # via lazy-github (pyproject.toml) httpcore==1.0.5 # via httpx httpx==0.27.0 # via # lazy-github (pyproject.toml) - # httpx-cache -httpx-cache==0.13.0 - # via lazy-github (pyproject.toml) + # hishel idna==3.7 # via # anyio @@ -67,9 +59,7 @@ mdit-py-plugins==0.4.1 mdurl==0.1.2 # via markdown-it-py msgpack==1.0.8 - # via - # httpx-cache - # textual-dev + # via textual-dev multidict==6.0.5 # via # aiohttp @@ -104,6 +94,7 @@ textual-dev==1.5.1 # via lazy-github (pyproject.toml) typing-extensions==4.12.2 # via + # hishel # pydantic # pydantic-core # pygithub