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

Commit ba703ed

Browse files
author
Mike Krieger
committed
Merge pull request #11 from shreyansb/master
added a flag 'return_json', to return the raw api response instead of media objects
2 parents 00e1fef + 9e63cf5 commit ba703ed

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

instagram/bind.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class InstagramAPIMethod(object):
4242
def __init__(self, api, *args, **kwargs):
4343
self.api = api
4444
self.as_generator = kwargs.pop("as_generator", False)
45+
self.return_json = kwargs.pop("return_json", False)
4546
self.max_pages = kwargs.pop("max_pages", 3)
4647
self.parameters = {}
4748
self._build_parameters(args, kwargs)
@@ -86,28 +87,35 @@ def _do_api_request(self, url, method="GET", body=None, headers={}):
8687
if response['status'] == '503':
8788
raise InstagramAPIError(response['status'], "Rate limited", "Your client is making too many request per second")
8889
content_obj = simplejson.loads(content)
89-
response_objects = []
90+
api_responses = []
9091
status_code = content_obj['meta']['code']
9192
if status_code == 200:
9293
if not self.objectify_response:
9394
return content_obj, None
9495

9596
if self.response_type == 'list':
9697
for entry in content_obj['data']:
97-
obj = self.root_class.object_from_dictionary(entry)
98-
response_objects.append(obj)
98+
if self.return_json:
99+
api_responses.append(entry)
100+
else:
101+
obj = self.root_class.object_from_dictionary(entry)
102+
api_responses.append(obj)
99103
elif self.response_type == 'entry':
100-
response_objects = self.root_class.object_from_dictionary(content_obj['data'])
101-
return response_objects, content_obj.get('pagination', {}).get('next_url')
104+
data = content_obj['data']
105+
if self.return_json:
106+
api_responses = data
107+
else:
108+
api_responses = self.root_class.object_from_dictionary(data)
109+
return api_responses, content_obj.get('pagination', {}).get('next_url')
102110
else:
103111
raise InstagramAPIError(status_code, content_obj['meta']['error_type'], content_obj['meta']['error_message'])
104112

105113
def _paginator_with_url(self, url, method="GET", body=None, headers={}):
106114
pages_read = 0
107115
while url and pages_read < self.max_pages:
108-
response_objects, url = self._do_api_request(url, method, body, headers)
109-
pages_read += 1
110-
yield response_objects, url
116+
api_responses, url = self._do_api_request(url, method, body, headers)
117+
pages_read += 1
118+
yield api_responses, url
111119
return
112120

113121
def execute(self):

0 commit comments

Comments
 (0)