2
2
"""This module provides the basic models used in github3.py."""
3
3
from __future__ import unicode_literals
4
4
5
- from json import dumps , loads
6
- from logging import getLogger
5
+ import json as jsonlib
6
+ import logging
7
7
8
8
import dateutil .parser
9
9
import requests
10
- from requests .compat import is_py2 , urlparse
10
+ import requests .compat
11
11
12
12
from . import exceptions
13
- from .session import GitHubSession
13
+ from . import session
14
14
15
- __logs__ = getLogger (__package__ )
15
+ LOG = logging . getLogger (__package__ )
16
16
17
17
18
18
class GitHubCore (object ):
@@ -22,13 +22,18 @@ class GitHubCore(object):
22
22
basic attributes and methods to other sub-classes that are very useful to
23
23
have.
24
24
"""
25
+
25
26
_ratelimit_resource = 'core'
26
27
27
28
def __init__ (self , json , session ):
28
- if hasattr (session , 'session' ):
29
- # i.e. session is actually a GitHubCore instance
30
- session = session .session
31
- self .session = session
29
+ """Initialize our basic object.
30
+
31
+ Pretty much every object will pass in decoded JSON and a Session.
32
+ """
33
+ # Either or 'session' is an instance of a GitHubCore sub-class or it
34
+ # is a session. In the former case it will have a 'session' attribute.
35
+ # If it doesn't, we can just default to using the passed in session.
36
+ self .session = getattr (session , 'session' , session )
32
37
33
38
# set a sane default
34
39
self ._github_url = 'https://api.github.com'
@@ -76,7 +81,7 @@ def as_json(self):
76
81
:returns: this object's attributes as a JSON string
77
82
:rtype: str
78
83
"""
79
- return dumps (self ._json_data )
84
+ return jsonlib . dumps (self ._json_data )
80
85
81
86
@classmethod
82
87
def _get_attribute (cls , data , attribute , fallback = None ):
@@ -129,7 +134,7 @@ def _strptime(cls, time_str):
129
134
130
135
def __repr__ (self ):
131
136
repr_string = self ._repr ()
132
- if is_py2 :
137
+ if requests . compat . is_py2 :
133
138
return repr_string .encode ('utf-8' )
134
139
return repr_string
135
140
@@ -141,7 +146,7 @@ def from_dict(cls, json_dict, session):
141
146
@classmethod
142
147
def from_json (cls , json , session ):
143
148
"""Return an instance of this class formed from ``json``."""
144
- return cls (loads (json ), session )
149
+ return cls (jsonlib . loads (json ), session )
145
150
146
151
def __eq__ (self , other ):
147
152
return self ._uniq == other ._uniq
@@ -176,9 +181,9 @@ def _instance_or_null(self, instance_class, json):
176
181
def _json (self , response , status_code , include_cache_info = True ):
177
182
ret = None
178
183
if self ._boolean (response , status_code , 404 ) and response .content :
179
- __logs__ .info ('Attempting to get JSON information from a Response '
180
- 'with status code %d expecting %d' ,
181
- response .status_code , status_code )
184
+ LOG .info ('Attempting to get JSON information from a Response '
185
+ 'with status code %d expecting %d' ,
186
+ response .status_code , status_code )
182
187
ret = response .json ()
183
188
headers = response .headers
184
189
if (include_cache_info and
@@ -188,7 +193,7 @@ def _json(self, response, status_code, include_cache_info=True):
188
193
'Last-Modified' , ''
189
194
)
190
195
ret ['ETag' ] = response .headers .get ('ETag' , '' )
191
- __logs__ .info ('JSON was %sreturned' , 'not ' if ret is None else '' )
196
+ LOG .info ('JSON was %sreturned' , 'not ' if ret is None else '' )
192
197
return ret
193
198
194
199
def _boolean (self , response , true_code , false_code ):
@@ -212,29 +217,29 @@ def _request(self, method, *args, **kwargs):
212
217
raise exceptions .TransportError (exc )
213
218
214
219
def _delete (self , url , ** kwargs ):
215
- __logs__ .debug ('DELETE %s with %s' , url , kwargs )
220
+ LOG .debug ('DELETE %s with %s' , url , kwargs )
216
221
return self ._request ('delete' , url , ** kwargs )
217
222
218
223
def _get (self , url , ** kwargs ):
219
- __logs__ .debug ('GET %s with %s' , url , kwargs )
224
+ LOG .debug ('GET %s with %s' , url , kwargs )
220
225
return self ._request ('get' , url , ** kwargs )
221
226
222
227
def _patch (self , url , ** kwargs ):
223
- __logs__ .debug ('PATCH %s with %s' , url , kwargs )
228
+ LOG .debug ('PATCH %s with %s' , url , kwargs )
224
229
return self ._request ('patch' , url , ** kwargs )
225
230
226
231
def _post (self , url , data = None , json = True , ** kwargs ):
227
232
if json :
228
- data = dumps (data ) if data is not None else data
229
<
A3E2
/td>
- __logs__ .debug ('POST %s with %s, %s' , url , data , kwargs )
233
+ data = jsonlib . dumps (data ) if data is not None else data
234
+ LOG .debug ('POST %s with %s, %s' , url , data , kwargs )
230
235
return self ._request ('post' , url , data , ** kwargs )
231
236
232
237
def _put (self , url , ** kwargs ):
233
- __logs__ .debug ('PUT %s with %s' , url , kwargs )
238
+ LOG .debug ('PUT %s with %s' , url , kwargs )
234
239
return self ._request ('put' , url , ** kwargs )
235
240
236
241
def _build_url (self , * args , ** kwargs ):
237
- """Builds a new API url from scratch."""
242
+ """Build a new API url from scratch."""
238
243
return self .session .build_url (* args , ** kwargs )
239
244
240
245
@property
@@ -247,7 +252,7 @@ def _api(self):
247
252
@_api .setter
248
253
def _api (self , uri ):
249
254
if uri :
250
- self ._uri = urlparse (uri )
255
+ self ._uri = requests . compat . urlparse (uri )
251
256
self .url = uri
252
257
253
258
def _iter (self , count , url , cls , params = None , etag = None , headers = None ):
@@ -316,34 +321,11 @@ def refresh(self, conditional=False):
316
321
return self
317
322
318
323
def new_session (self ):
319
- """Helper function to generate a new session"""
320
- return GitHubSession ()
321
-
322
-
323
- class BaseCommit (GitHubCore ):
324
-
325
- """This abstracts a lot of the common attributes for commit-like objects.
326
-
327
- The :class:`BaseCommit <BaseCommit>` object serves as the base for
328
- the various types of commit objects returned by the API.
329
- """
330
-
331
- def _update_attributes (self , commit ):
332
- self ._api = self ._get_attribute (commit , 'url' )
333
-
334
- #: SHA of this commit.
335
- self .sha = self ._get_attribute (commit , 'sha' )
336
-
337
- #: Commit message
338
- self .message = self ._get_attribute (commit , 'message' )
324
+ """Generate a new session.
339
325
340
- #: List of parents to this commit.
341
- self .parents = self ._get_attribute (commit , 'parents' , [])
342
-
343
- #: URL to view the commit on GitHub
344
- self .html_url = self ._get_attribute (commit , 'html_url' )
345
- if not self .sha :
346
- i = self ._api .rfind ('/' )
347
- self .sha = self ._api [i + 1 :]
348
-
349
- self ._uniq = self .sha
326
+ :returns:
327
+ A brand new session
328
+ :rtype:
329
+ :class:`~github3.session.GitHubSession`
330
+ """
331
+ return session .GitHubSession ()
0 commit comments