diff options
author | 2011-04-24 21:32:05 -0400 | |
---|---|---|
committer | 2011-04-24 21:32:05 -0400 | |
commit | 2b8fa24053877e37ccf362476d5fd58d47ed9753 (patch) | |
tree | cae50de13f21cca5ecadef31f54612afffad77e0 /github3 | |
parent | model improvements (diff) | |
download | python-github3-2b8fa24053877e37ccf362476d5fd58d47ed9753.tar.xz python-github3-2b8fa24053877e37ccf362476d5fd58d47ed9753.zip |
repo api works :)
Diffstat (limited to 'github3')
-rw-r--r-- | github3/api.py | 8 | ||||
-rw-r--r-- | github3/core.py | 61 | ||||
-rw-r--r-- | github3/models.py | 9 |
3 files changed, 72 insertions, 6 deletions
diff --git a/github3/api.py b/github3/api.py index 8b30d52..3225e43 100644 --- a/github3/api.py +++ b/github3/api.py @@ -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) diff --git a/github3/core.py b/github3/core.py index db8b213..73bbea2 100644 --- a/github3/core.py +++ b/github3/core.py @@ -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()
\ No newline at end of file diff --git a/github3/models.py b/github3/models.py index 5546080..eed8efc 100644 --- a/github3/models.py +++ b/github3/models.py @@ -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), |