mirror of
https://github.com/kennethreitz/python-github3.git
synced 2026-06-05 23:10:17 +00:00
clean slate
This commit is contained in:
@@ -10,87 +10,3 @@ This module implements the GitHub3 API wrapper objects.
|
||||
|
||||
"""
|
||||
|
||||
|
||||
from . import models
|
||||
from .packages.anyjson import deserialize
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
|
||||
|
||||
API_URL = 'https://api.github.com'
|
||||
API_MIME = 'application/vnd.github.v3+json'
|
||||
|
||||
|
||||
|
||||
|
||||
# =======
|
||||
# Helpers
|
||||
# =======
|
||||
|
||||
def _safe_response(r, error=None):
|
||||
|
||||
try:
|
||||
r.raise_for_status()
|
||||
return r
|
||||
except requests.HTTPError:
|
||||
if (r.status_code == 404) or (r.status_code == 401):
|
||||
raise LoginFailed
|
||||
else:
|
||||
raise APIError(error) if error else APIError
|
||||
|
||||
|
||||
def get(*path, **params):
|
||||
"""
|
||||
Accepts optional error parameter, which will be passed in the event of a
|
||||
non-401 HTTP error.
|
||||
|
||||
api.get('groups')
|
||||
api.get('groups', 'id')
|
||||
api.get('accounts', 'verify')
|
||||
"""
|
||||
|
||||
path = list(path)
|
||||
path.insert(0, '')
|
||||
|
||||
url = '{0}{1}'.format(API_URL, '/'.join(map(str, path)))
|
||||
print url
|
||||
# params = kwargs.get('params', None)
|
||||
|
||||
r = requests.get(url, params=params, auth=None)
|
||||
|
||||
return _safe_response(r)
|
||||
|
||||
|
||||
def post(params, *path):
|
||||
|
||||
path += API_URL
|
||||
url = '%s%s' % ('/'.join(map(str, path)), '.json')
|
||||
r = requests.post(url, params=params, auth=auth)
|
||||
return _safe_response(r)
|
||||
|
||||
|
||||
# ==========
|
||||
# Exceptions
|
||||
# ==========
|
||||
|
||||
class LoginFailed(RuntimeError):
|
||||
"""Login falied!"""
|
||||
|
||||
class APIError(RuntimeError):
|
||||
"""There was a problem properly accessing the Convore API."""
|
||||
|
||||
|
||||
|
||||
def login(username, password):
|
||||
"""Configured API Credentials"""
|
||||
global auth
|
||||
|
||||
auth = (username, password)
|
||||
# print requests.auth_manager.__dict__
|
||||
|
||||
# ==========
|
||||
# End Points
|
||||
# ==========
|
||||
|
||||
|
||||
+1
-113
@@ -4,119 +4,7 @@
|
||||
github3.core
|
||||
~~~~~~~~~~~~
|
||||
|
||||
This module contains the core GitHub 3 interface.
|
||||
This module contains the core GitHub3 interface.
|
||||
|
||||
"""
|
||||
|
||||
|
||||
from .api import API_URL, get
|
||||
import json
|
||||
import models
|
||||
# TODO: switch to anyjson
|
||||
|
||||
|
||||
class GitHub(object):
|
||||
"""Central GitHub object."""
|
||||
|
||||
rate_limit = None
|
||||
rate_left = None
|
||||
per_page = 30
|
||||
accept = 'application/vnd.github.v3+json'
|
||||
|
||||
def __init__(self, apiurl=API_URL):
|
||||
self.__basic_auth = None
|
||||
|
||||
|
||||
def _get(self, *path, **kwargs):
|
||||
"""optional json=False, paged=False"""
|
||||
|
||||
headers = {'Accept': self.accept}
|
||||
|
||||
is_json = kwargs.get('json', False)
|
||||
is_paged = kwargs.get('paged', False)
|
||||
|
||||
r = get(*path, auth=self.__basic_auth, headers=headers)
|
||||
|
||||
rate_left = r.headers.get('x-ratelimit-remaining', None)
|
||||
rate_limit = r.headers.get('x-ratelimit-limit', None)
|
||||
|
||||
if (rate_limit is not None) or (rate_left is not None):
|
||||
self.rate_limit = rate_limit
|
||||
self.rate_left = rate_left
|
||||
|
||||
if is_json:
|
||||
r = json.loads(r.content)
|
||||
|
||||
if is_paged:
|
||||
pass
|
||||
# TODO: paged support (__iter__)
|
||||
return r
|
||||
|
||||
|
||||
|
||||
def auth(self, username, password):
|
||||
self.__basic_auth = (username, password)
|
||||
return self.logged_in
|
||||
|
||||
|
||||
def oauth(self):
|
||||
# TODO: oAuth
|
||||
pass
|
||||
|
||||
|
||||
@property
|
||||
def logged_in(self):
|
||||
r = self._get('')
|
||||
print
|
||||
|
||||
if r.status_code == 200 and self.__basic_auth:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def repo(self, username, reponame):
|
||||
d = self._get('repos', username, '{0}.json'.format(reponame), json=True)
|
||||
|
||||
|
||||
repo = models.Repo()
|
||||
repo.from_dict(d)
|
||||
|
||||
return repo
|
||||
|
||||
|
||||
# {
|
||||
# "has_downloads": true,
|
||||
# "forks": 10,
|
||||
# "url": "https://api.github.com/repos/kennethreitz/requests.json",
|
||||
# "created_at": "2011-02-13T18:38:17Z",
|
||||
# "watchers": 166,
|
||||
# "description": "Python HTTP modules suck. This one doesn't.",
|
||||
# "master_branch": "develop",
|
||||
# "has_wiki": true,
|
||||
# "open_issues": 5,
|
||||
# "fork": false,
|
||||
# "html_url": "https://github.com/kennethreitz/requests",
|
||||
# "homepage": "http://pypi.python.org/pypi/requests/",
|
||||
# "has_issues": true,
|
||||
# "pushed_at": "2011-04-21T21:39:45Z",
|
||||
# "language": "Python",
|
||||
# "private": false,
|
||||
# "size": 2748,
|
||||
# "integrate_branch": null,
|
||||
# "owner": {
|
||||
# "email": "_@kennethreitz.com",
|
||||
# "type": "User",
|
||||
# "url": "https://api.github.com/users/kennethreitz.json",
|
||||
# "login": "kennethreitz",
|
||||
# "created_at": "2009-08-26T21:17:47Z",
|
||||
# "gravatar_url": "https://secure.gravatar.com/avatar/2eccc4005572c1e2b12a9c00580bc86f?s=30&d=https://d3nwyuy0nl342s.cloudfront.net%2Fimages%2Fgravatars%2Fgravatar-140.png",
|
||||
# "blog": "http://kennethreitz.com",
|
||||
# "name": "Kenneth Reitz",
|
||||
# "company": "NetApp, Inc",
|
||||
# "location": "Washington, DC"
|
||||
# },
|
||||
# "name": "requests"
|
||||
# }
|
||||
|
||||
# Default instance
|
||||
github = GitHub()
|
||||
@@ -8,144 +8,3 @@ This module provides the GitHub3 object models.
|
||||
|
||||
"""
|
||||
|
||||
|
||||
class GitHubModel(object):
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class User(GitHubModel):
|
||||
pass
|
||||
|
||||
def __init__(self):
|
||||
self.email = None
|
||||
self.type = None
|
||||
self.url = None
|
||||
self.login = None
|
||||
self.created_at = None
|
||||
self.gravatar_url = None
|
||||
self.blog = None
|
||||
self.name = None
|
||||
self.company = None
|
||||
self.location = None
|
||||
|
||||
def __repr__(self):
|
||||
return '<user \'{0}\'>'.format(self.login)
|
||||
|
||||
def from_dict(self, d):
|
||||
self.email = d.get('email', None)
|
||||
self.type = d.get('type', None)
|
||||
self.url = d.get('url', None)
|
||||
self.login = d.get('login', None)
|
||||
self.created_at = d.get('created_at', None)
|
||||
self.gravatar_url = d.get('gravatar_url', None)
|
||||
self.blog = d.get('blog', None)
|
||||
self.name = d.get('name', None)
|
||||
self.company = d.get('company', None)
|
||||
self.location = d.get('location', None)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Repo(GitHubModel):
|
||||
"""GitHub Repository."""
|
||||
|
||||
def __init__(self):
|
||||
self.has_downloads = None
|
||||
self.forks = None
|
||||
self.url = None
|
||||
self.created_at = None
|
||||
self.watchers = None
|
||||
self.description = None
|
||||
self.master_branch = None
|
||||
self.has_wiki = None
|
||||
self.open_issues = None
|
||||
self.fork = None
|
||||
self.html_url = None
|
||||
self.homepage = None
|
||||
self.has_issues = None
|
||||
self.pushed_at = None
|
||||
self.language = None
|
||||
self.private = None
|
||||
self.size = None
|
||||
self.integrate_branch = None
|
||||
self.owner = None
|
||||
self.name = None
|
||||
|
||||
def __repr__(self):
|
||||
return '<repo \'{0}/{1}\'>'.format(self.owner.login, self.name)
|
||||
|
||||
def from_dict(self, d):
|
||||
self.has_downloads = d.get('has_downloads', None)
|
||||
self.forks = d.get('forks', None)
|
||||
self.url = d.get('url', None)
|
||||
self.created_at = d.get('created_at', None)
|
||||
self.watchers = d.get('watchers', None)
|
||||
self.description = d.get('description', None)
|
||||
self.master_branch = d.get('master_branch', None)
|
||||
self.has_wiki = d.get('has_wiki', None)
|
||||
self.open_issues = d.get('open_issues', None)
|
||||
self.fork = d.get('fork', None)
|
||||
self.html_url = d.get('html_url', None)
|
||||
self.homepage = d.get('homepage', None)
|
||||
self.has_issues = d.get('has_issues', None)
|
||||
self.pushed_at = d.get('pushed_at', None)
|
||||
self.language = d.get('language', None)
|
||||
self.private = d.get('private', None)
|
||||
self.size = d.get('size', None)
|
||||
self.integrate_branch = d.get('integrate_branch', None)
|
||||
self.owner = User()
|
||||
self.owner.from_dict(d.get('owner', dict()))
|
||||
self.name = d.get('name', None)
|
||||
|
||||
|
||||
|
||||
class Gist(GitHubModel):
|
||||
"""GitHub Gist.
|
||||
|
||||
gist.files['filename.py']
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.api_url = None
|
||||
|
||||
|
||||
|
||||
class GistComment(GitHubModel):
|
||||
"""GitHub GistComment."""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
class Issue(GitHubModel):
|
||||
|
||||
|
||||
def __init__(self):
|
||||
self.number = None
|
||||
self.updated_at = None
|
||||
self.closed_at = None
|
||||
self.labels = []
|
||||
self.title= None
|
||||
self.comments = []
|
||||
self.user = None
|
||||
self.body = None
|
||||
self.url = None
|
||||
self.state = None
|
||||
self.api_url = None
|
||||
|
||||
# api
|
||||
self.milestone = None
|
||||
self.assignee = None
|
||||
|
||||
|
||||
|
||||
class Milestone(GitHubModel):
|
||||
|
||||
def __init__(self):
|
||||
self.api_url = None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user