From de025b9c368287b4dee931faa61326bf2dce1a41 Mon Sep 17 00:00:00 2001 From: Samuel Ainsworth Date: Wed, 8 Apr 2020 18:12:41 -0700 Subject: [PATCH 1/4] fix import issues --- simplegist/__init__.py | 108 +++++++++++++++++++++++++++++++++++++-- simplegist/comments.py | 4 +- simplegist/do.py | 4 +- simplegist/mygist.py | 4 +- simplegist/simplegist.py | 105 ------------------------------------- 5 files changed, 111 insertions(+), 114 deletions(-) delete mode 100644 simplegist/simplegist.py diff --git a/simplegist/__init__.py b/simplegist/__init__.py index 6c5f810..fc4bec0 100644 --- a/simplegist/__init__.py +++ b/simplegist/__init__.py @@ -5,8 +5,110 @@ (c) 2013 Varun Malhotra. MIT License """ -from simplegist import * - __author__ = 'Varun Malhotra' __version__ = '1.0.1' -__license__ = 'MIT' \ No newline at end of file +__license__ = 'MIT' + +import requests +import json + +from simplegist.config import USERNAME, API_TOKEN, BASE_URL, GIST_URL +from simplegist.mygist import Mygist +from simplegist.do import Do +from simplegist.comments import Comments + +class Simplegist: + """ + Gist Base Class + + This class is to used to instantiate the wrapper and authenticate. + + Authenticate with providing Github Username and API-Token to use + it for all future API requests + """ + + def __init__(self, **args): + # Save our username and api_token (If given) for later use. + if 'username' in args: + self.username = args['username'] + else: + if not USERNAME: + raise Exception('Please provide your Github username.') + else: + self.username = USERNAME + + if 'api_token' in args: + self.api_token = args['api_token'] + else: + if not API_TOKEN: + raise Exception('Please provide your Github API Token.') + else: + self.api_token = API_TOKEN + + + # Set header information in every request. + self.header = { 'X-Github-Username': self.username, + 'Content-Type': 'application/json', + 'Authorization': 'token %s' %self.api_token + } + + def profile(self): + return Mygist(self) + + def search(self, user): + return Mygist(self,user=user) + + def do(self): + return Do(self) + + def comments(self): + return Comments(self) + + def create(self, **args): + if 'description' in args: + self.description = args['description'] + else: + self.description = '' + + if 'name' in args: + self.gist_name = args['name'] + else: + self.gist_name = '' + + if 'public' in args: + self.public = args['public'] + else: + self.public = 1 + + if 'content' in args: + self.content = args['content'] + else: + raise Exception('Gist content can\'t be empty') + + url = '/gists' + + data = {"description": self.description, + "public": self.public, + "files": { + self.gist_name: { + "content": self.content + } + } + } + + r = requests.post( + '%s%s' % (BASE_URL, url), + data=json.dumps(data), + headers=self.header + ) + if (r.status_code == 201): + response = { + 'Gist-Link': '%s/%s/%s' %(GIST_URL,self.username,r.json()['id']), + 'Clone-Link': '%s/%s.git' %(GIST_URL,r.json()['id']), + 'Embed-Script': '' %(GIST_URL,self.username,r.json()['id']), - 'id': r.json()['id'], - 'created_at': r.json()['created_at'], - - } - return response - raise Exception('Gist not created: server response was [%s] %s' % (r.status_code, r.text)) - From af029e8f3ceab8585091da4960034468c838a63e Mon Sep 17 00:00:00 2001 From: Samuel Ainsworth Date: Wed, 8 Apr 2020 18:14:33 -0700 Subject: [PATCH 2/4] stop overwriting the self.content method in Mygist.edit() --- simplegist/mygist.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/simplegist/mygist.py b/simplegist/mygist.py index 0d7dfa0..46a9313 100644 --- a/simplegist/mygist.py +++ b/simplegist/mygist.py @@ -153,7 +153,7 @@ def edit(self, **args): raise Exception('Gist Name/ID must be provided') if 'content' in args: - self.content = args['content'] + content = args['content'] else: raise Exception('Gist content can\'t be empty') @@ -162,7 +162,7 @@ def edit(self, **args): data = {"description": self.description, "files": { self.gist_name: { - "content": self.content + "content": content } } } @@ -170,7 +170,7 @@ def edit(self, **args): data = {"description": self.description, "files": { self.gist_name: { - "content": self.content + "content": content } } } @@ -185,7 +185,7 @@ def edit(self, **args): if (r.status_code == 200): r_text = json.loads(r.text) response = { - 'updated_content': self.content, + 'updated_content': content, 'created_at': r.json()['created_at'], 'comments':r.json()['comments'] } From 0ff768e04d2a6d0460b372529bd3e0afc52349e5 Mon Sep 17 00:00:00 2001 From: Samuel Ainsworth Date: Wed, 8 Apr 2020 18:36:07 -0700 Subject: [PATCH 3/4] use items() instead of iteritems() --- simplegist/comments.py | 2 +- simplegist/do.py | 2 +- simplegist/mygist.py | 10 +++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/simplegist/comments.py b/simplegist/comments.py index c8f358b..f23bceb 100644 --- a/simplegist/comments.py +++ b/simplegist/comments.py @@ -21,7 +21,7 @@ def getMyID(self,gist_name): limit = len(r.json()) for g,no in zip(r_text, range(0,limit)): - for ka,va in r.json()[no]['files'].iteritems(): + for ka,va in r.json()[no]['files'].items(): if str(va['filename']) == str(gist_name): return r.json()[no]['id'] return 0 diff --git a/simplegist/do.py b/simplegist/do.py index 22cdd71..e701ea0 100644 --- a/simplegist/do.py +++ b/simplegist/do.py @@ -20,7 +20,7 @@ def getMyID(self,gist_name): limit = len(r.json()) for g,no in zip(r_text, range(0,limit)): - for ka,va in r.json()[no]['files'].iteritems(): + for ka,va in r.json()[no]['files'].items(): if str(va['filename']) == str(gist_name): return r.json()[no]['id'] return 0 diff --git a/simplegist/mygist.py b/simplegist/mygist.py index 46a9313..8d149fb 100644 --- a/simplegist/mygist.py +++ b/simplegist/mygist.py @@ -27,7 +27,7 @@ def listall(self): limit = len(r.json()) if (r.status_code == 200 ): for g,no in zip(r_text, range(0,limit)): - for key,value in r.json()[no]['files'].iteritems(): + for key,value in r.json()[no]['files'].items(): file_name.append(value['filename']) return file_name @@ -51,7 +51,7 @@ def list(self, offset): limit = offset if (offset <= len(r.json()) ) else len(r.json()) for g,no in zip(r_text, range(0,limit)): - for key,value in r.json()[no]['files'].iteritems(): + for key,value in r.json()[no]['files'].items(): file_name.append(value['filename']) return file_name raise Exception('Username not found') @@ -70,7 +70,7 @@ def getMyID(self,gist_name): limit = len(r.json()) for g,no in zip(r_text, range(0,limit)): - for ka,va in r.json()[no]['files'].iteritems(): + for ka,va in r.json()[no]['files'].items(): if str(va['filename']) == str(gist_name): return r.json()[no]['id'] return 0 @@ -102,7 +102,7 @@ def content(self, **args): if self.gist_name!='': content = r.json()['files'][self.gist_name]['content'] else: - for key,value in r.json()['files'].iteritems(): + for key,value in r.json()['files'].items(): content = r.json()['files'][value['filename']]['content'] return content @@ -122,7 +122,7 @@ def getgist(self, **args): ) if (r.status_code == 200): - for key,value in r.json()['files'].iteritems(): + for key,value in r.json()['files'].items(): content = value['filename'] return content From cb598fbbae8180ee399c17edf3ea8e29c2d9cabf Mon Sep 17 00:00:00 2001 From: Samuel Ainsworth Date: Wed, 8 Apr 2020 18:43:20 -0700 Subject: [PATCH 4/4] fix public boolean-ness --- simplegist/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simplegist/__init__.py b/simplegist/__init__.py index fc4bec0..a9e03d2 100644 --- a/simplegist/__init__.py +++ b/simplegist/__init__.py @@ -78,7 +78,7 @@ def create(self, **args): if 'public' in args: self.public = args['public'] else: - self.public = 1 + self.public = True if 'content' in args: self.content = args['content'] @@ -88,7 +88,7 @@ def create(self, **args): url = '/gists' data = {"description": self.description, - "public": self.public, + "public": "true" if self.public else "false", "files": { self.gist_name: { "content": self.content