|
9 | 9 | raise_if_extra_kwargs, check_response_code, extract_and_parse_json
|
10 | 10 |
|
11 | 11 |
|
| 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 | + |
12 | 49 | class RestSession(object):
|
13 | 50 | def __init__(self, access_token, base_url, timeout=None):
|
14 | 51 | super(RestSession, self).__init__()
|
@@ -83,6 +120,8 @@ def get_pages(self, url, params=None, **kwargs):
|
83 | 120 | # Get next page
|
84 | 121 | if response.links.get('next'):
|
85 | 122 | 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) |
86 | 125 | # API request - get next page
|
87 | 126 | response = self._req_session.get(next_url, timeout=timeout)
|
88 | 127 | else:
|
|
0 commit comments