1
Fork 0
mirror of https://github.com/RGBCube/GitHubWrapper synced 2025-05-16 22:15:09 +00:00

Adding user object

Editing Exceptions and changing _state to session
This commit is contained in:
VarMonke 2022-03-27 11:03:47 +05:30
parent 25710629f2
commit 77de27ea5a
3 changed files with 40 additions and 11 deletions

View file

@ -28,23 +28,23 @@ class Ratelimited(APIError):
class WillExceedRatelimit(APIError): class WillExceedRatelimit(APIError):
"""Raised when the library predicts the call will exceed the ratelimit, will abort the call by default.""" """Raised when the library predicts the call will exceed the ratelimit, will abort the call by default."""
def __init__(self, response, count): def __init__(self, response, count):
msg = 'Performing this action will exceed the ratelimit, aborting.\n{} remaining available calls, calls to make: {}' msg = 'Performing this action will exceed the ratelimit, aborting.\n{} remaining available calls, calls to make: {}.'
msg = msg.format(response.header['X-RateLimit-Remaining'], count) msg = msg.format(response.header['X-RateLimit-Remaining'], count)
super().__init__(msg) super().__init__(msg)
class UserNotFound(APIError): class UserNotFound(APIError):
def __init__(self): def __init__(self):
msg = 'User not found' msg = 'User not found.'
super().__init__(msg) super().__init__(msg)
class OrganizationNotFound(APIError): class OrganizationNotFound(APIError):
def __init__(self): def __init__(self):
msg = 'Organization not found' msg = 'Organization not found.'
super().__init__(msg) super().__init__(msg)
class RepositoryNotFound(APIError): class RepositoryNotFound(APIError):
def __init__(self): def __init__(self):
msg = 'Repository not found' msg = 'Repository not found.'
super().__init__(msg) super().__init__(msg)
class ObjectNotFound(APIError): class ObjectNotFound(APIError):
@ -55,7 +55,7 @@ class ObjectNotFound(APIError):
class NoAuthProvided(APIError): class NoAuthProvided(APIError):
"""Raised when no proper authorization or invalid authorization is given to the client""" """Raised when no proper authorization or invalid authorization is given to the client"""
def __init__(self): def __init__(self):
msg = 'Without authorization, this client doesn\'t have it\'s own repository' msg = 'Without authorization, this client doesn\'t have it\'s own repository.'
super().__init__(msg) super().__init__(msg)
class InvalidAuthCombination(APIError): class InvalidAuthCombination(APIError):

View file

@ -12,7 +12,7 @@ from .objects import User
class Github: class Github:
_auth = None _auth = None
_state = aiohttp.ClientSession session = aiohttp.ClientSession
def __init__( def __init__(
self, self,
*, *,
@ -28,8 +28,8 @@ class Github:
async def start(self): async def start(self):
"""Main entry point to the wrapper, this creates the ClientSession.""" """Main entry point to the wrapper, this creates the ClientSession."""
self._state = await http.make_session(headers=self._headers, authorization=self._auth) self.session = await http.make_session(headers=self._headers, authorization=self._auth)
async def get_user(self, username: str): async def get_user(self, username: str):
"""Fetch a Github user from their username.""" """Fetch a Github user from their username."""
return User(await http.get_user(self._state, username), self._state) return User(await http.get_user(self.session, username), self.session)

View file

@ -11,15 +11,23 @@ __all__ = (
'User', 'User',
) )
def dt_formatter(time_str):
if time_str is not None:
return datetime.strptime(time_str, r"%Y-%m-%dT%H:%M:%SZ")
return None
def repr_dt(time_str):
return time_str.strftime(r'%d-%m-%Y, %H:%M:%S')
class APIOBJECT: class APIOBJECT:
__slots__ = ( __slots__ = (
'_response', '_response',
'_state' 'session'
) )
def __init__(self, response: dict[str, str | int], session: aiohttp.ClientSession) -> None: def __init__(self, response: dict[str, str | int], session: aiohttp.ClientSession) -> None:
self._response = response self._response = response
self._state = session self.session = session
def __repr__(self) -> str: def __repr__(self) -> str:
@ -52,7 +60,28 @@ class User(_BaseUser):
) )
def __init__(self, response: dict, session: aiohttp.ClientSession) -> None: def __init__(self, response: dict, session: aiohttp.ClientSession) -> None:
super().__init__(response, session) super().__init__(response, session)
... tmp = self.__slots__
keys = {key: value for key,value in self.response.items() if key in tmp}
for key, value in key.items():
if key == 'login':
self.login = value
return
if '_at' in key and value is not None:
setattr(self, key, dt_formatter(value))
return
setattr(self, key, value)
def __repr__(self):
return f'<User; login: {self.login}, id: {self.id}, created_at: {repr_dt(self.created_at)}>'
@classmethod
async def get_user(self, session: aiohttp.ClientSession, username: str) -> 'User':
"""Returns a User object from the username, with the mentions slots."""
response = await http.get_user(session, username)
return User(response, session)