repo api works :)

This commit is contained in:
Kenneth Reitz
2011-04-24 21:32:05 -04:00
parent 2b52351b21
commit 2b8fa24053
3 changed files with 71 additions and 5 deletions
+6 -2
View File
@@ -49,8 +49,11 @@ def get(*path, **params):
api.get('accounts', 'verify')
"""
url = '{0}{1}'.format(API_URL, '/'.join(map(str, path)))
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)
@@ -60,7 +63,8 @@ def get(*path, **params):
def post(params, *path):
url = '%s%s%s' % (API_URL, '/'.join(map(str, path)), '.json')
path += API_URL
url = '%s%s' % ('/'.join(map(str, path)), '.json')
r = requests.post(url, params=params, auth=auth)
return _safe_response(r)
+57 -2
View File
@@ -10,7 +10,9 @@ This module contains the core GitHub 3 interface.
from .api import API_URL, get
import json
import models
# TODO: switch to anyjson
class GitHub(object):
@@ -25,9 +27,14 @@ class GitHub(object):
self.__basic_auth = None
def _get(self, *path):
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)
@@ -37,6 +44,12 @@ class GitHub(object):
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
@@ -61,7 +74,49 @@ class GitHub(object):
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 -1
View File
@@ -31,6 +31,9 @@ class User(GitHubModel):
self.company = None,
self.location = None
def __repr__(self):
return '<user \'{0}\''.format(self.name)
def from_dict(self, d):
self.email = d.get('email', None),
self.type = d.get('type', None),
@@ -73,6 +76,9 @@ class Repo(GitHubModel):
self.owner = None,
self.name = None
def __repr__(self):
return '<repo \'{0}/{1}\''.format(self.owner, self.name)
def from_dict(self, d):
self.has_downloads = d.get('has_downloads', None),
self.forks = d.get('forks', None),
@@ -92,7 +98,8 @@ class Repo(GitHubModel):
self.private = d.get('private', None),
self.size = d.get('size', None),
self.integrate_branch = d.get('integrate_branch', None),
self.owner = d.get('owner', None),
self.owner = User()
self.owner.from_dict(d.get('owner', dict()))
self.name = d.get('name', None),