Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 2b8fa24

Browse files
author
Kenneth Reitz
committed
repo api works :)
1 parent 2b52351 commit 2b8fa24

3 files changed

Lines changed: 72 additions & 6 deletions

File tree

github3/api.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ def get(*path, **params):
4949
api.get('accounts', 'verify')
5050
"""
5151

52-
url = '{0}{1}'.format(API_URL, '/'.join(map(str, path)))
52+
path = list(path)
53+
path.insert(0, '')
5354

55+
url = '{0}{1}'.format(API_URL, '/'.join(map(str, path)))
56+
print url
5457
# params = kwargs.get('params', None)
5558

5659
r = requests.get(url, params=params, auth=None)
@@ -60,7 +63,8 @@ def get(*path, **params):
6063

6164
def post(params, *path):
6265

63-
url = '%s%s%s' % (API_URL, '/'.join(map(str, path)), '.json')
66+
path += API_URL
67+
url = '%s%s' % ('/'.join(map(str, path)), '.json')
6468
r = requests.post(url, params=params, auth=auth)
6569
return _safe_response(r)
6670

github3/core.py

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111

1212
from .api import API_URL, get
13-
13+
import json
14+
import models
15+
# TODO: switch to anyjson
1416

1517

1618
class GitHub(object):
@@ -25,9 +27,14 @@ def __init__(self, apiurl=API_URL):
2527
self.__basic_auth = None
2628

2729

28-
def _get(self, *path):
30+
def _get(self, *path, **kwargs):
31+
"""optional json=False, paged=False"""
32+
2933
headers = {'Accept': self.accept}
3034

35+
is_json = kwargs.get('json', False)
36+
is_paged = kwargs.get('paged', False)
37+
3138
r = get(*path, auth=self.__basic_auth, headers=headers)
3239

3340
rate_left = r.headers.get('x-ratelimit-remaining', None)
@@ -37,6 +44,12 @@ def _get(self, *path):
3744
self.rate_limit = rate_limit
3845
self.rate_left = rate_left
3946

47+
if is_json:
48+
r = json.loads(r.content)
49+
50+
if is_paged:
51+
pass
52+
# TODO: paged support (__iter__)
4053
return r
4154

4255

@@ -61,7 +74,49 @@ def logged_in(self):
6174
else:
6275
return False
6376

64-
77+
def repo(self, username, reponame):
78+
d = self._get('repos', username, '{0}.json'.format(reponame), json=True)
79+
80+
81+
repo = models.Repo()
82+
repo.from_dict(d)
83+
84+
return repo
85+
86+
87+
# {
88+
# "has_downloads": true,
89+
# "forks": 10,
90+
# "url": "https://api.github.com/repos/kennethreitz/requests.json",
91+
# "created_at": "2011-02-13T18:38:17Z",
92+
# "watchers": 166,
93+
# "description": "Python HTTP modules suck. This one doesn't.",
94+
# "master_branch": "develop",
95+
# "has_wiki": true,
96+
# "open_issues": 5,
97+
# "fork": false,
98+
# "html_url": "https://github.com/kennethreitz/requests",
99+
# "homepage": "http://pypi.python.org/pypi/requests/",
100+
# "has_issues": true,
101+
# "pushed_at": "2011-04-21T21:39:45Z",
102+
# "language": "Python",
103+
# "private": false,
104+
# "size": 2748,
105+
# "integrate_branch": null,
106+
# "owner": {
107+
# "email": "_@kennethreitz.com",
108+
# "type": "User",
109+
# "url": "https://api.github.com/users/kennethreitz.json",
110+
# "login": "kennethreitz",
111+
# "created_at": "2009-08-26T21:17:47Z",
112+
# "gravatar_url": "https://secure.gravatar.com/avatar/2eccc4005572c1e2b12a9c00580bc86f?s=30&d=https://d3nwyuy0nl342s.cloudfront.net%2Fimages%2Fgravatars%2Fgravatar-140.png",
113+
# "blog": "http://kennethreitz.com",
114+
# "name": "Kenneth Reitz",
115+
# "company": "NetApp, Inc",
116+
# "location": "Washington, DC"
117+
# },
118+
# "name": "requests"
119+
# }
65120

66121
# Default instance
67122
github = GitHub()

github3/models.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ def __init__(self):
3131
self.company = None,
3232
self.location = None
3333

34+
def __repr__(self):
35+
return '<user \'{0}\''.format(self.name)
36+
3437
def from_dict(self, d):
3538
self.email = d.get('email', None),
3639
self.type = d.get('type', None),
@@ -73,6 +76,9 @@ def __init__(self):
7376
self.owner = None,
7477
self.name = None
7578

79+
def __repr__(self):
80+
return '<repo \'{0}/{1}\''.format(self.owner, self.name)
81+
7682
def from_dict(self, d):
7783
self.has_downloads = d.get('has_downloads', None),
7884
self.forks = d.get('forks', None),
@@ -92,7 +98,8 @@ def from_dict(self, d):
9298
self.private = d.get('private', None),
9399
self.size = d.get('size', None),
94100
self.integrate_branch = d.get('integrate_branch', None),
95-
self.owner = d.get('owner', None),
101+
self.owner = User()
102+
self.owner.from_dict(d.get('owner', dict()))
96103
self.name = d.get('name', None),
97104

98105

0 commit comments

Comments
 (0)