Skip to content

Commit 38d38e4

Browse files
committed
Patch for Cisco Spark 'max=null' in next URL bug
Patch for Cisco Spark Defect: 'next' URL returned in the Link headers of the responses contain an errant 'max=null' parameter, which causes the next request (to this URL) to fail if the URL is requested as-is. This patch parses the next_url to remove the max=null parameter.
1 parent 4b4c6f8 commit 38d38e4

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

ciscosparkapi/restsession.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,43 @@
99
raise_if_extra_kwargs, check_response_code, extract_and_parse_json
1010

1111

12+
def _fix_next_url(next_url):
13+
"""Remove max=null parameter from URL.
14+
15+
Patch for Cisco Spark Defect: 'next' URL returned in the Link headers of
16+
the responses contain an errant 'max=null' parameter, which causes the
17+
next request (to this URL) to fail if the URL is requested as-is.
18+
19+
This patch parses the next_url to remove the max=null parameter.
20+
21+
Args:
22+
next_url(unicode, str): The 'next' URL to be parsed and cleaned.
23+
24+
Returns:
25+
str: The clean URL to be used for the 'next' request.
26+
27+
Raises:
28+
AssertionError: If the parameter types are incorrect.
29+
ciscosparkapiException: If 'next_url' does not contain a valid API
30+
endpoint URL (scheme, netloc and path).
31+
32+
"""
33+
assert isinstance(next_url, basestring)
34+
parsed_url = urlparse.urlparse(next_url)
35+
if not parsed_url.scheme or not parsed_url.netloc or not parsed_url.path:
36+
error_message = "'next_url' must be a valid API endpoint URL, " \
37+
"minimally containing a scheme, netloc and path."
38+
raise ciscosparkapiException(error_message)
39+
if parsed_url.query:
40+
query_list = parsed_url.query.split('&')
41+
if 'max=null' in query_list:
42+
query_list.remove('max=null')
43+
new_query = '&'.join(query_list)
44+
parsed_url = list(parsed_url)
45+
parsed_url[4] = new_query
46+
return urlparse.urlunparse(parsed_url)
47+
48+
1249
class RestSession(object):
1350
def __init__(self, access_token, base_url, timeout=None):
1451
super(RestSession, self).__init__()
@@ -83,6 +120,8 @@ def get_pages(self, url, params=None, **kwargs):
83120
# Get next page
84121
if response.links.get('next'):
85122
next_url = response.links.get('next').get('url')
123+
# Patch for Cisco Spark 'max=null' in next URL bug.
124+
next_url = _fix_next_url(next_url)
86125
# API request - get next page
87126
response = self._req_session.get(next_url, timeout=timeout)
88127
else:

0 commit comments

Comments
 (0)