Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Commit aefb118

Browse files
author
henninge
committed
Add pagination_format parameter to api methods.
1 parent 95dc1c5 commit aefb118

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

instagram/bind.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ class InstagramAPIMethod(object):
4747
def __init__(self, api, *args, **kwargs):
4848
self.api = api
4949
self.as_generator = kwargs.pop("as_generator", False)
50+
if self.as_generator:
51+
self.pagination_format = 'next_url'
52+
else:
53+
self.pagination_format = kwargs.pop('pagination_format', 'next_url')
5054
self.return_json = kwargs.pop("return_json", False)
5155
self.max_pages = kwargs.pop("max_pages", 3)
5256
self.parameters = {}
@@ -87,6 +91,15 @@ def _build_path(self):
8791
self.path = self.path.replace(variable, value)
8892
self.path = self.path + '.%s' % self.api.format
8993

94+
def _build_pagination_info(self, content_obj):
95+
"""Extract pagination information in the desired format."""
96+
pagination = content_obj.get('pagination', {})
97+
if self.pagination_format == 'next_url':
98+
return pagination.get('next_url')
99+
if self.pagination_format == 'dict':
100+
return pagination
101+
raise Exception('Invalid value for pagination_format: %s' % self.pagination_format)
102+
90103
def _do_api_request(self, url, method="GET", body=None, headers=None):
91104
headers = headers or {}
92105
response, content = OAuth2Request(self.api).make_request(url, method=method, body=body, headers=headers)
@@ -119,7 +132,7 @@ def _do_api_request(self, url, method="GET", body=None, headers=None):
119132
api_responses = self.root_class.object_from_dictionary(data)
120133
elif self.response_type == 'empty':
121134
pass
122-
return api_responses, content_obj.get('pagination', {}).get('next_url')
135+
return api_responses, self._build_pagination_info(content_obj)
123136
else:
124137
raise InstagramAPIError(status_code, content_obj['meta']['error_type'], content_obj['meta']['error_message'])
125138

0 commit comments

Comments
 (0)