Skip to content

Commit a111e50

Browse files
author
Kenneth Reitz
committed
get a user
1 parent ae29192 commit a111e50

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

github3/api.py

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99

1010
import omnijson as json
1111

12+
from .models import *
1213
from .helpers import is_collection, to_python, to_api, get_scope
1314
from .config import settings
1415

16+
17+
import requests
18+
19+
1520
class GithubCore(object):
1621

1722
@staticmethod
@@ -30,49 +35,54 @@ def _resource_deserialize(s):
3035
raise ResponseError('The API Response was not valid.')
3136

3237

33-
def _to_map(self, obj, iterable):
34-
"""Maps given dict iterable to a given Resource object."""
38+
@staticmethod
39+
def _generate_url(endpoint):
40+
"""Generates proper endpoint URL."""
3541

36-
a = []
42+
if is_collection(endpoint):
43+
resource = map(str, endpoint)
44+
resource = '/'.join(endpoint)
45+
else:
46+
resource = endpoint
3747

38-
for it in iterable:
39-
a.append(obj.new_from_dict(it, rdd=self))
48+
return (settings.base_url + resource)
4049

41-
return a
4250

51+
def _get_http_resource(self, endpoint, params=None):
4352

44-
def _get_resources(self, key, obj, limit=None, **kwargs):
45-
"""GETs resources of given path, maps them to objects, and
46-
handles paging.
47-
"""
48-
pass
53+
url = self._generate_url(endpoint)
54+
r = requests.get(url, params=params)
55+
r.raise_for_status()
4956

57+
return r
5058

51-
def _get_resource(self, http_resource, obj, **kwargs):
52-
"""GETs API Resource of given path."""
5359

54-
item = self._get_http_resource(http_resource, params=kwargs)
55-
item = self._resource_deserialize(item)
5660

57-
return obj.new_from_dict(item, rdd=self)
61+
def _get_resource(self, resource, obj, **kwargs):
5862

63+
r = self._get_http_resource(resource, params=kwargs)
64+
item = self._resource_deserialize(r.content)
5965

60-
def _post_resource(self, http_resource, **kwargs):
61-
"""POSTs API Resource of given path."""
66+
return obj.new_from_dict(item, gh=self)
6267

63-
r = self._post_http_resource(http_resource, params=kwargs)
6468

65-
return r
69+
def _to_map(self, obj, iterable):
70+
"""Maps given dict iterable to a given Resource object."""
6671

67-
def _delete_resource(self, http_resource):
68-
"""DELETEs API Resource of given path."""
72+
a = list()
6973

70-
r = self._delete_http_resource(http_resource)
74+
for it in iterable:
75+
a.append(obj.new_from_dict(it, rdd=self))
7176

72-
if r['status'] in ('200', '204'):
73-
return True
74-
else:
75-
return False
77+
return a
78+
79+
def _get_url(self, resource):
80+
81+
if is_collection(resource):
82+
resource = map(str, resource)
83+
resource = '/'.join(resource)
84+
85+
return resource
7686

7787

7888

@@ -83,6 +93,12 @@ def __init__(self):
8393
super(Github, self).__init__()
8494

8595

96+
def get_user(self, username):
97+
# return 'kennethreitz'
98+
return self._get_resource(('users', username), User)
99+
# return User()
100+
101+
86102

87103

88104
class ResponseError(Exception):

0 commit comments

Comments
 (0)