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

add http.data meth and make owner optional in delete_repo and fix repo licenses

This commit is contained in:
VarMonke 2022-04-17 11:26:55 +05:30
parent f75886bc92
commit cc44cb67e5
3 changed files with 22 additions and 8 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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'])
if value is not None:
setattr(self, key, value.get('name'))
continue
setattr(self, key, None)
else:
setattr(self, key, value)