Skip to content

Commit

Permalink
Swap out httpx-cache for hishel
Browse files Browse the repository at this point in the history
  • Loading branch information
gizmo385 committed Jul 16, 2024
1 parent d015735 commit ef3841a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
19 changes: 13 additions & 6 deletions lazy_github/lib/github/client.py
Original file line number Diff line number Diff line change
@@ -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"""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ requires-python = ">=3.11"
dependencies = [
"PyGithub",
"httpx",
"httpx-cache",
"hishel",
"pydantic",
"textual",
"textual-dev",
Expand Down
25 changes: 8 additions & 17 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ef3841a

Please sign in to comment.