Skip to content

Commit 573f258

Browse files
committed
Added GitHubObject.
This is now what every other object inherits from in part. Anything not inheriting from GitHubCore should inherit from this so it has the to_json and from_json methods.
1 parent 878f0bb commit 573f258

6 files changed

Lines changed: 37 additions & 28 deletions

File tree

github3/gist.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
"""
88

99
from json import dumps
10-
from .models import GitHubCore, BaseComment
10+
from .models import GitHubObject, GitHubCore, BaseComment
1111
from .user import User
1212

1313

14-
class GistFile(object):
14+
class GistFile(GitHubObject):
1515
"""The :class:`GistFile <GistFile>` object. This is used to represent a
1616
file object returned by GitHub while interacting with gists.
1717
"""

github3/git.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
from base64 import b64decode
1010
from json import dumps
11-
from .models import GitHubCore, BaseCommit
11+
from .models import GitHubObject, GitHubCore, BaseCommit
1212
from .user import User
1313

1414

15-
class Blob(object):
15+
class Blob(GitHubObject):
1616
"""The :class:`Blob <Blob>` object."""
1717
def __init__(self, blob):
1818
super(Blob, self).__init__()
@@ -231,7 +231,7 @@ def tree(self):
231231
return self._tree
232232

233233

234-
class Hash(object):
234+
class Hash(GitHubObject):
235235
"""The :class:`Hash <Hash>` object."""
236236
def __init__(self, info):
237237
super(Hash, self).__init__()

github3/models.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,25 @@
1111
from requests import session
1212

1313

14-
class GitHubCore(object):
14+
class GitHubObject(object):
15+
"""The :class:`GitHubObject <GitHubObject>` object. A basic class to be
16+
subclassed by GitHubCore and other classes that would otherwise subclass
17+
object."""
18+
def __init__(self, json):
19+
super(GitHubObject, self).__init__()
20+
self._json_data = json
21+
22+
def to_json(self):
23+
"""Return the json representing this object."""
24+
return self._json_data
25+
26+
@classmethod
27+
def from_json(cls, json):
28+
"""Return an instance of ``cls`` formed from ``json``."""
29+
return cls(json)
30+
31+
32+
class GitHubCore(GitHubObject):
1533
"""The :class:`GitHubCore <GitHubCore>` object. This class provides some
1634
basic attributes to other classes that are very useful to have.
1735
"""
@@ -35,14 +53,14 @@ def _json(self, request, status_code):
3553
if request.status_code == status_code and request.content:
3654
ret = request.json
3755
if request.status_code >= 400:
38-
raise Error(request)
56+
raise GitHubError(request)
3957
return ret
4058

4159
def _boolean(self, request, true_code, false_code):
4260
if request.status_code == true_code:
4361
return True
4462
if request.status_code != false_code and request.status_code >= 400:
45-
raise Error(request)
63+
raise GitHubError(request)
4664
return False
4765

4866
def _delete(self, url, **kwargs):
@@ -96,15 +114,6 @@ def ratelimit_remaining(self):
96114
self._remaining = json.get('rate', {}).get('remaining', 0)
97115
return self._remaining
98116

99-
def to_json(self):
100-
"""Return the json representing this object."""
101-
return self._json_data
102-
103-
@classmethod
104-
def from_json(cls, json):
105-
"""Return an instance of ``cls`` formed from ``json``."""
106-
return cls(json)
107-
108117
@staticmethod
109118
def requires_auth(func):
110119
"""Decorator to note which class methods require authorization."""
@@ -117,7 +126,7 @@ def auth_wrapper(self, *args, **kwargs):
117126
if auth:
118127
return func(self, *args, **kwargs)
119128
else:
120-
raise Error(type('Faux Request', (object, ),
129+
raise GitHubError(type('Faux Request', (object, ),
121130
{'status_code': 401, 'message': 'Requires authentication'}
122131
))
123132

@@ -373,9 +382,9 @@ def public_repos(self):
373382
return self._public_repos
374383

375384

376-
class Error(Exception):
385+
class GitHubError(Exception):
377386
def __init__(self, resp):
378-
super(Error, self).__init__()
387+
super(GitHubError, self).__init__()
379388
self._code = resp.status_code
380389
error = resp.json
381390
self._message = error.get('message')

github3/pulls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from json import dumps
1010
from .git import Commit
11-
from .models import GitHubCore, BaseComment
11+
from .models import GitHubObject, GitHubCore, BaseComment
1212
from .user import User
1313

1414

@@ -58,7 +58,7 @@ def user(self):
5858
return self._user
5959

6060

61-
class PullFile(object):
61+
class PullFile(GitHubObject):
6262
"""The :class:`PullFile <PullFile>` object."""
6363
def __init__(self, pfile):
6464
super(PullFile, self).__init__()

github3/repo.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .event import Event
1212
from .issue import Issue, Label, Milestone, issue_params
1313
from .git import Blob, Commit, Reference, Tag, Tree
14-
from .models import GitHubCore, BaseComment, BaseCommit
14+
from .models import GitHubObject, GitHubCore, BaseComment, BaseCommit
1515
from .pulls import PullRequest
1616
from .user import User, Key
1717

@@ -1297,7 +1297,7 @@ def name(self):
12971297
return self._name
12981298

12991299

1300-
class Contents(object):
1300+
class Contents(GitHubObject):
13011301
"""The :class:`Contents <Contents>` object. It holds the information
13021302
concerning any content in a repository requested via the API.
13031303
"""
@@ -1573,7 +1573,7 @@ def updated_at(self):
15731573
return self._updated
15741574

15751575

1576-
class RepoTag(object):
1576+
class RepoTag(GitHubObject):
15771577
"""The :class:`RepoTag <RepoTag>` object. This stores the information
15781578
representing a tag that was created on a repository.
15791579
"""
@@ -1759,7 +1759,7 @@ def total(self):
17591759
return self._total
17601760

17611761

1762-
class Comparison(object):
1762+
class Comparison(GitHubObject):
17631763
"""The :class:`Comparison <Comparison>` object. This encapsulates the
17641764
information returned by GitHub comparing two commit objects in a
17651765
repository."""

github3/user.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from json import dumps
1010
from .event import Event
11-
from .models import GitHubCore, BaseAccount
11+
from .models import GitHubObject, GitHubCore, BaseAccount
1212

1313

1414
class Key(GitHubCore):
@@ -70,7 +70,7 @@ def update(self, title, key):
7070
return False
7171

7272

73-
class Plan(object):
73+
class Plan(GitHubObject):
7474
"""The :class:`Plan <Plan>` object. This makes interacting with the plan
7575
information about a user easier.
7676
"""

0 commit comments

Comments
 (0)