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

Commit efe46fc

Browse files
committed
Merge branch 'henninge-pagination-format'
2 parents d51700b + 51a8ed4 commit efe46fc

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
@@ -51,6 +51,10 @@ class InstagramAPIMethod(object):
5151
def __init__(self, api, *args, **kwargs):
5252
self.api = api
5353
self.as_generator = kwargs.pop("as_generator", False)
54+
if self.as_generator:
55+
self.pagination_format = 'next_url'
56+
else:
57+
self.pagination_format = kwargs.pop('pagination_format', 'next_url')
5458
self.return_json = kwargs.pop("return_json", False)
5559
self.max_pages = kwargs.pop("max_pages", 3)
5660
self.with_next_url = kwargs.pop("with_next_url", None)
@@ -92,6 +96,15 @@ def _build_path(self):
9296
self.path = self.path.replace(variable, value)
9397
self.path = self.path + '.%s' % self.api.format
9498

99+
def _build_pagination_info(self, content_obj):
100+
"""Extract pagination information in the desired format."""
101+
pagination = content_obj.get('pagination', {})
102+
if self.pagination_format == 'next_url':
103+
return pagination.get('next_url')
104+
if self.pagination_format == 'dict':
105+
return pagination
106+
raise Exception('Invalid value for pagination_format: %s' % self.pagination_format)
107+
95108
def _do_api_request(self, url, method="GET", body=None, headers=None):
96109
headers = headers or {}
97110
response, content = OAuth2Request(self.api).make_request(url, method=method, body=body, headers=headers)
@@ -133,7 +146,7 @@ def _do_api_request(self, url, method="GET", body=None, headers=None):
133146
api_responses = self.root_class.object_from_dictionary(data)
134147
elif self.response_type == 'empty':
135148
pass
136-
return api_responses, content_obj.get('pagination', {}).get('next_url')
149+
return api_responses, self._build_pagination_info(content_obj)
137150
else:
138151
raise InstagramAPIError(status_code, content_obj['meta']['error_type'], content_obj['meta']['error_message'])
139152

0 commit comments

Comments
 (0)