diff --git a/Github/exceptions.py b/Github/exceptions.py index c65d1b0..a883834 100644 --- a/Github/exceptions.py +++ b/Github/exceptions.py @@ -99,4 +99,10 @@ class GistNotFound(APIError): """Raised when a gist is not found.""" def __init__(self): msg = 'The gist you are trying to access does not exist.' - super().__init__(msg) \ No newline at end of file + super().__init__(msg) + +class MissingPermissions(NoAuthProvided): #Why did I subclass NoAuthProvided? + """Raised when a user does not have permissions to perform an action.""" + def __init__(self): + msg = 'You do not have permission to perform this action.' + super().__init__(msg) diff --git a/Github/http.py b/Github/http.py index 3aaad0b..22865cb 100644 --- a/Github/http.py +++ b/Github/http.py @@ -16,7 +16,7 @@ if TYPE_CHECKING: import aiohttp from .exceptions import * -from .exceptions import GistNotFound, RepositoryAlreadyExists +from .exceptions import GistNotFound, RepositoryAlreadyExists, MissingPermissions from .objects import APIObject, User, Gist, Repository, Organization from .urls import * @@ -82,6 +82,7 @@ class Paginator: types: dict[str, APIObject] = { 'user': User, 'gist' : Gist, + 'repo' : Repository } self.target_type = types[target_type] self.pages = {} @@ -206,6 +207,24 @@ class http: return await result.json() raise IssueNotFound + async def delete_repo(self, owner: str,repo_name: str) -> None: + """Deletes a Repo from the given owner and repo name.""" + result = await self.session.delete(REPO_URL.format(owner, repo_name)) + if 204 <= result.status <= 299: + return 'Successfully deleted repository.' + if result.status == 403: + raise MissingPermissions + raise RepositoryNotFound + + async def delete_gist(self, gist_id: int) -> None: + """Deletes a Gist from the given gist id.""" + result = await self.session.delete(GIST_URL.format(gist_id)) + if result.status == 204: + return 'Successfully deleted gist.' + if result.status == 403: + raise MissingPermissions + raise GistNotFound + async def get_org(self, org_name: str) -> GithubOrgData: """Returns an org's public data in JSON format.""" result = await self.session.get(ORG_URL.format(org_name)) @@ -243,14 +262,14 @@ class http: raise InvalidToken - async def create_repo(self, **kwargs: dict[str, str | bool]) -> GithubRepoData: + async def create_repo(self, name: str, description: str, public: bool, gitignore: str, license: str) -> GithubRepoData: """Creates a repo for you with given data""" data = { - 'name': kwargs.get('name'), - 'description': kwargs.get('description'), - 'private': kwargs.get('private', True), - 'gitignore_template': kwargs.get('gitignore'), - 'license': kwargs.get('license'), + 'name': name, + 'description': description, + 'public': public, + 'gitignore_template': gitignore, + 'license': license, } result = await self.session.post(CREATE_REPO_URL, data=json.dumps(data)) if 200 <= result.status <= 299: @@ -258,3 +277,4 @@ class http: if result.status == 401: raise NoAuthProvided raise RepositoryAlreadyExists + diff --git a/Github/main.py b/Github/main.py index 6e17f37..b77b938 100644 --- a/Github/main.py +++ b/Github/main.py @@ -114,7 +114,7 @@ class GHClient: return wrapped return wrapper - @_cache(type='User') + #@_cache(type='User') async def get_self(self) -> User: """Returns the authenticated User object.""" if self._auth: @@ -136,9 +136,12 @@ class GHClient: """Fetch a Github repository from it's name.""" return Issue(await self.http.get_repo_issue(owner, repo, issue), self.http.session) - async def create_repo(self, name: str, description: str, private: bool, gitignore_template: str) -> Repository: - """Create a new Github repository, requires authorisation.""" - return Repository(await self.http.create_repo(name, description, private, gitignore_template), self.http.session) + 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 , repo: str) -> None: + """Delete a Github repository, requires authorisation.""" + await self.http.delete_repo(owner, repo) async def get_gist(self, gist: int) -> Gist: """Fetch a Github gist from it's id.""" @@ -148,6 +151,10 @@ class GHClient: """Creates a Gist with the given files, requires authorisation.""" return Gist(await self.http.create_gist(files=files, description=description, public=public), self.http.session) + async def delete_gist(self, gist: int) -> None: + """Delete a Github gist, requires authorisation.""" + await self.http.delete_gist(gist) + async def get_org(self, org: str) -> Organization: """Fetch a Github organization from it's name.""" return Organization(await self.http.get_org(org), self.http.session)