From ef4d46057e2057cdbc88b0c8df18ee07c76ab5e5 Mon Sep 17 00:00:00 2001 From: VarMonke Date: Thu, 7 Apr 2022 20:45:29 +0530 Subject: [PATCH] change to _session for consistency --- Github/exceptions.py | 8 +++++++- Github/http.py | 25 ++++++++++++++++--------- Github/main.py | 10 +++++++--- Github/urls.py | 2 ++ 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Github/exceptions.py b/Github/exceptions.py index 94b45af..c65d1b0 100644 --- a/Github/exceptions.py +++ b/Github/exceptions.py @@ -86,11 +86,17 @@ class AlreadyStarted(APIError): class NotStarted(APIError): """Raised when a call is made before start is called.""" def __init__(self): - msg = 'You must call `await .start()` before making this call.' + msg = 'You must call `await .start()` before making this call.' super().__init__(msg) class RepositoryAlreadyExists(APIError): """Raised when a repository already exists.""" def __init__(self): msg = 'The repository you are trying to create already exists.' + super().__init__(msg) + +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 diff --git a/Github/http.py b/Github/http.py index 4250411..779fe03 100644 --- a/Github/http.py +++ b/Github/http.py @@ -8,7 +8,7 @@ import re import json from .exceptions import * -from .exceptions import RepositoryAlreadyExists +from .exceptions import RepositoryAlreadyExists, GistNotFound from .objects import * from .urls import * @@ -109,7 +109,7 @@ class Paginator: raise WillExceedRatelimit(response, self.max_page) self.bare_link = groups[0][0][:-1] -GithubUserData = GithubRepoData = GithubIssueData = GithubOrgData = dict[str, str | int] +GithubUserData = GithubRepoData = GithubIssueData = GithubOrgData = GithubGistData = dict[str, str | int] class http: def __init__(self, headers: dict[str, str | int], auth: aiohttp.BasicAuth | None): @@ -176,6 +176,20 @@ class http: return await result.json() raise IssueNotFound + 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)) + if 200 <= result.status <= 299: + return await result.json() + raise OrganizationNotFound + + async def get_gist(self, gist_id: int) -> GithubGistData: + """Returns a gist's raw JSON from the given gist id.""" + result = await self.session.get(GIST_URL.format(gist_id)) + if 200 <= result.status <= 299: + return await result.json() + raise GistNotFound + async def create_repo(self, **kwargs: dict[str, str | bool]) -> GithubRepoData: """Creates a repo for you with given data""" data = { @@ -191,10 +205,3 @@ class http: if result.status == 401: raise NoAuthProvided raise RepositoryAlreadyExists - - 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)) - if 200 <= result.status <= 299: - return await result.json() - raise OrganizationNotFound diff --git a/Github/main.py b/Github/main.py index 1623928..2ec6f40 100644 --- a/Github/main.py +++ b/Github/main.py @@ -11,7 +11,7 @@ from getpass import getpass from .http import http from . import exceptions -from .objects import User, PartialUser, Repository, Organization, Issue +from .objects import User, PartialUser, Repository, Organization, Issue, Gist from .cache import UserCache, RepoCache class GHClient: @@ -121,10 +121,14 @@ class GHClient: async def create_repo(self, name: str, description: str, private: bool, gitignore_template: str) -> Repository: """Create a new Github repository.""" - return Repository(await self.http.make_repo(name, description, private, gitignore_template), self.http.session) + return Repository(await self.http.create_repo(name, description, private, gitignore_template), self.http.session) async def get_org(self, org: str) -> Organization: """Fetch a Github organization from it's name""" - return Organization(await http.get_org(org), self.http.session) + return Organization(await self.http.get_org(org), self.http.session) + + async def get_gist(self, gist: int) -> Gist: + """Fetch a Github gist from it's id""" + return Gist(await self.http.get_gist(gist), self.http.session) diff --git a/Github/urls.py b/Github/urls.py index 8f46552..0d4390d 100644 --- a/Github/urls.py +++ b/Github/urls.py @@ -30,6 +30,8 @@ REPO_URL = BASE_URL + '/repos/{0}/{1}' # a specific repo REPO_ISSUE_URL = REPO_URL + '/issues/{2}' # a specific issue +#== gist urls ==# +GIST_URL = BASE_URL + '/gists/{0}' # specific gist #== org urls ==# ORG_URL = BASE_URL + '/orgs/{0}' \ No newline at end of file