8000 Add requires_auth decorator to GitHubCore. · jsullivanlive/github3.py@087c288 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 087c288

Browse files
committed
Add requires_auth decorator to GitHubCore.
GitHubCore is imported in just about every module so it only makes sense to place it there.
1 parent 38efe43 commit 087c288

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

github3/models.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,22 @@ def to_json(self):
100100
"""Return the json representing this object."""
101101
return self._json_data
102102

103+
@staticmethod
104+
def requires_auth(func):
105+
"""Decorator to note which class methods require authorization."""
106+
def auth_wrapper(self, *args, **kwargs):
107+
auth = False
108+
if hasattr(self, '_session'):
109+
auth = self._session.auth or \
110+
self._session.headers['Authorization']
111+
112+
if auth:
113+
return func(self, *args, **kwargs)
114+
else:
115+
raise Error(type('Faux Request', (object, ),
116+
{'status_code': 401, 'message': 'Requires authentication'}
117+
))
118+
103119

104120
class BaseComment(GitHubCore):
105121
"""The :class:`BaseComment <BaseComment>` object. A basic class for Gist,
@@ -150,13 +166,15 @@ def created_a 8D47 t(self):
150166
"""datetime object representing when the comment was created."""
151167
return self._created
152168

169+
@GitHubCore.requires_auth
153170
def delete(self):
154171
"""Delete this comment.
155172
156173
:returns: bool
157174
"""
158-
return self._delete(self._api)
175+
return self._boolean(self._delete(self._api), 204, 404)
159176

177+
@GitHubCore.requires_auth
160178
def edit(self, body):
161179
"""Edit this comment.
162180

0 commit comments

Comments
 (0)
0