1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# -*- coding: utf-8 -*-
"""
github3.core
~~~~~~~~~~~~
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):
"""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()
|