mirror of
https://github.com/RGBCube/GitHubWrapper
synced 2025-05-18 06:55:09 +00:00
Added the cache, adjusted kwargs for the client, added ability to verify token and username if provided
This commit is contained in:
parent
963029808b
commit
87eaf01679
1 changed files with 45 additions and 17 deletions
|
@ -4,15 +4,15 @@ __all__ = (
|
||||||
'GHClient',
|
'GHClient',
|
||||||
)
|
)
|
||||||
|
|
||||||
from getpass import getpass
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import functools
|
||||||
from Github.objects.repo import Issue
|
from getpass import getpass
|
||||||
|
|
||||||
from . import http
|
from . import http
|
||||||
from . import exceptions
|
from . import exceptions
|
||||||
from .objects import *
|
from .objects import User, PartialUser, Repository, Organization, Issue
|
||||||
|
from .cache import UserCache, RepoCache
|
||||||
|
|
||||||
class GHClient:
|
class GHClient:
|
||||||
_auth = None
|
_auth = None
|
||||||
|
@ -22,11 +22,17 @@ class GHClient:
|
||||||
*,
|
*,
|
||||||
username: str | None = None,
|
username: str | None = None,
|
||||||
token: str | None = None,
|
token: str | None = None,
|
||||||
|
user_cache_size: int = 30,
|
||||||
|
repo_cache_size: int = 15,
|
||||||
custom_headers: dict[str, str | int] = {}
|
custom_headers: dict[str, str | int] = {}
|
||||||
):
|
):
|
||||||
"""The main client, used to start most use-cases."""
|
"""The main client, used to start most use-cases."""
|
||||||
self._headers = custom_headers
|
self._headers = custom_headers
|
||||||
|
bound = lambda hi, lo, value: max(min(value, hi), lo)
|
||||||
|
self._user_cache = UserCache(bound(50, 0, user_cache_size))
|
||||||
|
self._repo_cache = RepoCache(bound(50, 0, repo_cache_size))
|
||||||
if username and token:
|
if username and token:
|
||||||
|
self.username = username
|
||||||
self._auth = aiohttp.BasicAuth(username, token)
|
self._auth = aiohttp.BasicAuth(username, token)
|
||||||
|
|
||||||
def __await__(self) -> 'GHClient':
|
def __await__(self) -> 'GHClient':
|
||||||
|
@ -58,29 +64,51 @@ class GHClient:
|
||||||
"""Main entry point to the wrapper, this creates the ClientSession."""
|
"""Main entry point to the wrapper, this creates the ClientSession."""
|
||||||
if self.has_started:
|
if self.has_started:
|
||||||
raise exceptions.AlreadyStarted
|
raise exceptions.AlreadyStarted
|
||||||
|
if self._auth:
|
||||||
|
try:
|
||||||
|
await self.get_self()
|
||||||
|
except exceptions.InvalidToken as exc:
|
||||||
|
raise exceptions.InvalidToken from exc
|
||||||
self.session = await http.make_session(headers=self._headers, authorization=self._auth)
|
self.session = await http.make_session(headers=self._headers, authorization=self._auth)
|
||||||
self.has_started = True
|
self.has_started = True
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def _cache(func, cache_type: str):
|
def _cache(*args, **kwargs):
|
||||||
async def wrapper(self: 'GHClient', name: str):
|
target_type = kwargs.get('type')
|
||||||
if cache_type == 'user':
|
def wrapper(func):
|
||||||
if (user := self._user_cache.get(name)):
|
@functools.wraps(func)
|
||||||
return user
|
async def wrapped(self, *args, **kwargs):
|
||||||
|
if target_type == 'User':
|
||||||
|
if (obj := self._user_cache.get(kwargs.get('user'))):
|
||||||
|
return obj
|
||||||
else:
|
else:
|
||||||
return await func(self, name)
|
res = await func(self, *args, **kwargs)
|
||||||
if cache_type == 'repo':
|
self._user_cache[kwargs.get('user')] = res
|
||||||
if (repo := self._repo_cache.get(name)):
|
return res
|
||||||
return repo
|
if target_type == 'Repo':
|
||||||
|
if (obj := self._repo_cache.get(kwargs.get('repo'))):
|
||||||
|
return obj
|
||||||
else:
|
else:
|
||||||
return await func(self, name)
|
res = await func(self, *args, **kwargs)
|
||||||
|
self._repo_cache[kwargs.get('repo')] = res
|
||||||
|
return res
|
||||||
|
return wrapped
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
async def get_self(self) -> User:
|
||||||
|
"""Returns the authenticated User object."""
|
||||||
|
if self._auth:
|
||||||
|
return await http.get_self(self.session)
|
||||||
|
else:
|
||||||
|
raise exceptions.NoAuthProvided
|
||||||
|
|
||||||
|
@_cache(type='User')
|
||||||
async def get_user(self, **kwargs) -> User:
|
async def get_user(self, **kwargs) -> User:
|
||||||
"""Fetch a Github user from their username."""
|
"""Fetch a Github user from their username."""
|
||||||
username = kwargs.get('user')
|
username = kwargs.get('user')
|
||||||
return User(await http.get_user(self.session, username), self.session)
|
return await http.get_user(self.session, username)
|
||||||
|
|
||||||
|
@_cache(type='Repo')
|
||||||
async def get_repo(self, **kwargs) -> Repository:
|
async def get_repo(self, **kwargs) -> Repository:
|
||||||
"""Fetch a Github repository from it's name."""
|
"""Fetch a Github repository from it's name."""
|
||||||
owner = kwargs.get('owner')
|
owner = kwargs.get('owner')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue