1
Fork 0
mirror of https://github.com/RGBCube/GitHubWrapper synced 2025-05-20 07:55:09 +00:00

Added create_gist and related File objects

This commit is contained in:
sudosnok 2022-04-10 20:26:49 +01:00
parent 353977c4b2
commit 6450ee6092
3 changed files with 57 additions and 6 deletions

View file

@ -1,11 +1,17 @@
#== http.py ==#
from __future__ import annotations
import io
import json
import re
from collections import namedtuple
from datetime import datetime
from types import SimpleNamespace
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .main import File
import aiohttp
@ -213,6 +219,29 @@ class http:
return await result.json()
raise GistNotFound
async def create_gist(
self,
*,
files: list['File'] = [],
description: str = 'Default description',
public: bool = False
) -> GithubGistData:
data = {}
data['description'] = description
data['public'] = public
data['files'] = {}
for file in files:
data['files'][file.filename] = {
'content': file.read()
}
data = json.dumps(data)
_headers = self.session.headers
result = self.session.post(CREATE_GIST_URL, data=data, headers=_headers|{'Accept': 'application/vnd.github.v3+json'})
if 201 == result.status:
return await result.json()
raise InvalidToken
async def create_repo(self, **kwargs: dict[str, str | bool]) -> GithubRepoData:
"""Creates a repo for you with given data"""
data = {

View file

@ -7,6 +7,7 @@ __all__ = (
import asyncio
import functools
import io
import aiohttp
@ -16,6 +17,20 @@ from .http import http
from .objects import Gist, Issue, Organization, Repository, User
class File:
def __init__(self, fp: str | io.StringIO, filename: str = 'DefaultFilename.txt'):
self.fp = fp
self.filename = filename
def read(self) -> str:
if isinstance(self.fp, str):
with open(self.fp) as fp:
data = fp.read()
return data
else:
return self.fp.read()
class GHClient:
_auth = None
has_started = False
@ -122,15 +137,20 @@ class GHClient:
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."""
"""Create a new Github repository, requires authorisation."""
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 self.http.get_org(org), self.http.session)
async def get_gist(self, gist: int) -> Gist:
"""Fetch a Github gist from it's id"""
"""Fetch a Github gist from it's id."""
return Gist(await self.http.get_gist(gist), self.http.session)
async def create_gist(self, *, files: list[File], description: str, public: bool) -> Gist:
"""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 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)

View file

@ -33,5 +33,7 @@ REPO_ISSUE_URL = REPO_URL + '/issues/{2}' # a specific issue
#== gist urls ==#
GIST_URL = BASE_URL + '/gists/{0}' # specific gist
CREATE_GIST_URL = BASE_URL + '/gists'
#== org urls ==#
ORG_URL = BASE_URL + '/orgs/{0}'