10BC0 Fixup pull request #459 · pythonthings/github3.py@ea94c9b · GitHub
[go: up one dir, main page]

Skip to content

Commit ea94c9b

Browse files
committed
Fixup pull request sigmavirus24#459
1 parent 0193afd commit ea94c9b

File tree

4 files changed

+70
-49
lines changed

4 files changed

+70
-49
lines changed

github3/github.py

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -363,30 +363,53 @@ def feeds(self):
363363
364364
:returns: dictionary parsed to include URITemplates
365365
"""
366-
url = self._build_url('feeds')
367-
data = self._json(self._get(url), 200)
368-
369-
def template_href(wrapper):
370-
if wrapper and 'href' in wrapper:
371-
wrapper['href'] = URITemplate(wrapper['href'])
366+
def replace_href(feed_dict):
367+
if not feed_dict:
368+
return feed_dict
369+
ret_dict = {}
370+
# Let's pluck out what we're most interested in, the href value
371+
href = feed_dict.pop('href', None)
372+
# Then we update the return dictionary with the rest of the values
373+
ret_dict.update(feed_dict)
374+
if href is not None:
375+
# So long as there is something to template, let's template it
376+
ret_dict['href'] = URITemplate(href)
377+
return ret_dict
372378

373-
links = data.get('_links', {})
374-
for d in links.values():
375-
if type(d) == list:
376-
map(template_href, d)
379+
url = self._build_url('feeds')
380+
json = self._json(self._get(url), 200, include_cache_info=False)
381+
if json is None: # If something went wrong, get out early
382+
return None
383+
384+
# We have a response body to parse
385+
feeds = {}
386+
387+
# Let's pop out the old links so we don't have to skip them below
388+
old_links = json.pop('_links', {})
389+
_links = {}
390+
# If _links is in the response JSON, iterate over that and recreate it
391+
# so that any templates contained inside can be turned into
392+
# URITemplates
393+
for key, value in old_links.items():
394+
if isinstance(value, list):
395+
# If it's an array/list of links, let's replace that with a
396+
# new list of links
397+
_links[key] = [replace_href(d) for d in value]
377398
else:
378-
template_href(d)
399+
# Otherwise, just use the new value
400+
_links[key] = replace_href(value)
379401

380-
for key, value in data.items():
381-
if key == '_links':
382-
continue
402+
# Start building up our return dictionary
403+
feeds['_links'] = _links
383404

384-
if type(value) == list:
385-
data[key] = list(map(URITemplate, value))
405+
for key, value in json.items():
406+
# This should roughly be the same logic as above.
407+
if isinstance(value, list):
408+
feeds[key] = [URITemplate(v) for v in value]
386409
else:
387-
data[key] = URITemplate(value)
410+
feeds[key] = URITemplate(value)
388411

389-
return data
412+
return feeds
390413

391414
@requires_auth
392415
def follow(self, username):

github3/models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,16 @@ def _instance_or_null(self, instance_class, json):
152152
except TypeError: # instance_class is not a subclass of GitHubCore
153153
return instance_class(json)
154154

155-
def _json(self, response, status_code):
155+
def _json(self, response, status_code, include_cache_info=True):
156156
ret = None
157157
if self._boolean(response, status_code, 404) and response.content:
158158
__logs__.info('Attempting to get JSON information from a Response '
159159
'with status code %d expecting %d',
160160
response.status_code, status_code)
161161
ret = response.json()
162162
headers = response.headers
163-
if ((headers.get('Last-Modified') or headers.get('ETag')) and
163+
if (include_cache_info and
164+
(headers.get('Last-Modified') or headers.get('ETag')) and
164165
isinstance(ret, dict)):
165166
ret['Last-Modified'] = response.headers.get(
166167
'Last-Modified', ''

tests/integration/test_github.py

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -95,41 +95,28 @@ def test_emojis(self):
9595
# Asserts that it's a string and looks ilke the URLs we expect to see
9696
assert emojis['+1'].startswith('https://github')
9797

98-
def test_feeds_values(self):
98+
def test_feeds(self):
9999
"""Test the ability to retrieve a user's timelime URLs."""
100100
self.basic_login()
101101
cassette_name = self.cassette_name('feeds')
102102
with self.recorder.use_cassette(cassette_name):
103103
feeds = self.gh.feeds()
104104

105-
# _links is tested in its own test:
106-
del feeds['_links']
107-
108-
def check(wrapper):
109-
assert isinstance(wrapper, uritemplate.URITemplate)
110-
111-
for v in feeds.values():
112-
if type(v) is list:
113-
map(check, v)
114-
else:
115-
check(v)
116-
117-
def test_feeds__links(self):
118-
"""Test the ability to retrieve a user's timelime URLs."""
119-
self.basic_login()
120-
cassette_name = self.cassette_name('feeds')
121-
with self.recorder.use_cassette(cassette_name):
122-
feeds = self.gh.feeds()
123-
124-
def check(wrapper):
125-
if v:
126-
assert isinstance(wrapper['href'], uritemplate.URITemplate)
127< 6D3F code>-
128-
for v in feeds['_links'].values():
129-
if type(v) is list:
130-
map(check, v)
131-
else:
132-
check(v)
105+
_links = feeds.pop('_links')
106+
107+
for urls in feeds.values():
108+
if not isinstance(urls, list):
109+
urls = [urls]
110+
for url in urls:
111+
assert isinstance(url, uritemplate.URITemplate)
112+
113+
for links in _links.values():
114+
if not isinstance(links, list):
115+
links = [links]
116+
for link in links:
117+
href = link.get('href')
118+
assert (href is None or
119+
isinstance(href, uritemplate.URITemplate))
133120

134121
def test_gist(self):
135122
"""Test the ability to retrieve a single gist."""

tests/unit/test_github.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ def test_emojis(self):
114114

115115
self.session.get.assert_called_once_with(url_for('emojis'))
116116

117+
def test_feeds(self):
118+
self.instance.feeds()
119+
120+
self.session.get.assert_called_once_with(url_for('feeds'))
121+
117122
def test_follow(self):
118123
"""Test the request to follow a user."""
119124
self.instance.follow('username')
@@ -867,6 +872,11 @@ def test_emails(self):
867872
with pytest.raises(AuthenticationFailed):
868873
self.instance.emails()
869874

875+
def test_feeds(self):
876+
"""Show that one needs to authenticate to use #feeds."""
877+
with pytest.raises(AuthenticationFailed):
878+
self.instance.feeds()
879+
870880
def test_follow(self):
871881
"""Show that one needs to authenticate to use #follow."""
872882
with pytest.raises(AuthenticationFailed):

0 commit comments

Comments
 (0)
0