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
|
# -*- coding: utf-8 -*-
"""
github3.api
~~~~~~~~~~~
This module provies the core GitHub3 API interface.
"""
import omnijson as json
from .models import *
from .helpers import is_collection, to_python, to_api, get_scope
from .config import settings
import requests
class GithubCore(object):
@staticmethod
def _resource_serialize(o):
"""Returns JSON serialization of given object."""
return json.dumps(o)
@staticmethod
def _resource_deserialize(s):
"""Returns dict deserialization of a given JSON string."""
try:
return json.loads(s)
except ValueError:
raise ResponseError('The API Response was not valid.')
@staticmethod
def _generate_url(endpoint):
"""Generates proper endpoint URL."""
if is_collection(endpoint):
resource = map(str, endpoint)
resource = '/'.join(endpoint)
else:
resource = endpoint
return (settings.base_url + resource)
def _get_http_resource(self, endpoint, params=None):
url = self._generate_url(endpoint)
r = requests.get(url, params=params)
r.raise_for_status()
return r
def _get_resource(self, resource, obj, **kwargs):
r = self._get_http_resource(resource, params=kwargs)
item = self._resource_deserialize(r.content)
return obj.new_from_dict(item, gh=self)
def _to_map(self, obj, iterable):
"""Maps given dict iterable to a given Resource object."""
a = list()
for it in iterable:
a.append(obj.new_from_dict(it, rdd=self))
return a
def _get_url(self, resource):
if is_collection(resource):
resource = map(str, resource)
resource = '/'.join(resource)
return resource
class Github(GithubCore):
"""docstring for Github"""
def __init__(self):
super(Github, self).__init__()
def get_user(self, username):
# return 'kennethreitz'
return self._get_resource(('users', username), User)
# return User()
class ResponseError(Exception):
"""The API Response was unexpected."""
|