1
Fork 0
mirror of https://github.com/RGBCube/GitHubWrapper synced 2025-05-23 17:35:08 +00:00

Delete methods, partially done

This commit is contained in:
VarMonke 2022-04-12 20:16:02 +05:30
parent 56760c2752
commit 6483909960
3 changed files with 45 additions and 12 deletions

View file

@ -100,3 +100,9 @@ class GistNotFound(APIError):
def __init__(self):
msg = 'The gist you are trying to access does not exist.'
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)

View file

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

View file

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