From cc44cb67e570658da5af1fed245732565b7650ca Mon Sep 17 00:00:00 2001 From: VarMonke Date: Sun, 17 Apr 2022 11:26:55 +0530 Subject: [PATCH] add http.data meth and make owner optional in delete_repo and fix repo licenses --- Github/http.py | 13 +++++++++++-- Github/main.py | 9 +++++++-- Github/objects.py | 8 ++++---- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/Github/http.py b/Github/http.py index 51e834a..953226a 100644 --- a/Github/http.py +++ b/Github/http.py @@ -9,6 +9,7 @@ from collections import namedtuple from datetime import datetime from types import SimpleNamespace from typing import TYPE_CHECKING +import platform if TYPE_CHECKING: from .main import File @@ -19,6 +20,7 @@ from .exceptions import * from .exceptions import GistNotFound, RepositoryAlreadyExists, MissingPermissions from .objects import APIObject, User, Gist, Repository, Organization from .urls import * +__version__ = '0.0.1' __all__ = ( 'Paginator', @@ -62,7 +64,7 @@ trace_config.on_request_end.append(on_req_end) async def make_session(*, headers: dict[str, str], authorization: aiohttp.BasicAuth | None) -> aiohttp.ClientSession: """This makes the ClientSession, attaching the trace config and ensuring a UA header is present.""" if not headers.get('User-Agent'): - headers['User-Agent'] = 'Github-API-Wrapper' + headers['User-Agent'] = f'Github-API-Wrapper (https://github.com/VarMonke/Github-Api-Wrapper) @ {__version__} Python {platform.python_version()} aiohttp {aiohttp.__version__}' session = aiohttp.ClientSession( auth=authorization, @@ -124,7 +126,7 @@ GithubUserData = GithubRepoData = GithubIssueData = GithubOrgData = GithubGistDa class http: def __init__(self, headers: dict[str, str | int], auth: aiohttp.BasicAuth | None): if not headers.get('User-Agent'): - headers['User-Agent'] = 'Github-API-Wrapper' + headers['User-Agent'] = f'Github-API-Wrapper (https://github.com/VarMonke/Github-Api-Wrapper) @ {__version__} Python {platform.python_version()} aiohttp {aiohttp.__version__}' self._rates = Rates('', '', '', '', '') self.headers = headers self.auth = auth @@ -158,6 +160,13 @@ class http: trace_configs=config ) + def data(self): + #return session headers and auth + headers = {**self.session.headers} + auth = {'username': self.auth.login, 'token': self.auth.password} + + return {'headers': headers, 'auth': auth} + async def get_self(self) -> GithubUserData: """Returns the authenticated User's data""" result = await self.session.get(SELF_URL) diff --git a/Github/main.py b/Github/main.py index 9ac8509..ac92e1a 100644 --- a/Github/main.py +++ b/Github/main.py @@ -60,7 +60,12 @@ class GHClient: async def update_auth(self, username: str, token: str) -> None: """Allows you to input auth information after instantiating the client.""" - await self.http.update_auth(username, token) + #check if username and token is valid + await self.http.update_auth(username=username, token=token) + try: + await self.http.get_self() + except exceptions.InvalidToken as exc: + raise exceptions.InvalidToken from exc async def start(self) -> 'GHClient': """Main entry point to the wrapper, this creates the ClientSession.""" @@ -124,7 +129,7 @@ class GHClient: async def create_repo(self, name: str, description: str = 'Repository created using Github-Api-Wrapper.', public: bool = False,gitignore: str = None, license: str = None) -> Repository: return Repository(await self.http.create_repo(name,description,public,gitignore,license), self.http.session) - async def delete_repo(self, owner: str = None, repo: str= None) -> None: + async def delete_repo(self, repo: str= None, owner: str = None) -> None: """Delete a Github repository, requires authorisation.""" owner = owner or self.username return await self.http.delete_repo(owner, repo) diff --git a/Github/objects.py b/Github/objects.py index d298ca4..1d71690 100644 --- a/Github/objects.py +++ b/Github/objects.py @@ -165,10 +165,10 @@ class Repository(APIObject): continue if 'license' in key: - if value is None: - setattr(self, key,None) - setattr(self, key, value['name']) - continue + if value is not None: + setattr(self, key, value.get('name')) + continue + setattr(self, key, None) else: setattr(self, key, value)