Ejemplo n.º 1
0
        def _do_api_request(self, url, method="GET", body=None, headers=None):
            headers = headers or {}
            response, content = OAuth2Request(self.api).make_request(
                url, method=method, body=body, headers=headers)
            if response['status'] == '503':
                raise InstagramAPIError(
                    response['status'], "Rate limited",
                    "Your client is making too many request per second")

            try:
                content_obj = simplejson.loads(content)
            except ValueError:
                raise InstagramClientError(
                    'Unable to parse response, not valid JSON.',
                    status_code=response['status'])

            # Handle OAuthRateLimitExceeded from Instagram's Nginx which uses different format to documented api responses
            if not content_obj.has_key('meta'):
                if content_obj.get('code') == 420:
                    error_message = content_obj.get(
                        'error_message'
                    ) or "Your client is making too many request per second"
                    raise InstagramAPIError(420, "Rate limited", error_message)
                raise InstagramAPIError(content_obj.has_key('code'),
                                        content_obj.has_key('error_type'),
                                        content_obj.has_key('error_message'))

            api_responses = []
            status_code = content_obj['meta']['code']
            self.api.x_ratelimit_remaining = response.get(
                "x-ratelimit-remaining", None)
            self.api.x_ratelimit = response.get("x-ratelimit-limit", None)
            if status_code == 200:
                if not self.objectify_response:
                    return content_obj, None

                if self.response_type == 'list':
                    for entry in content_obj['data']:
                        if self.return_json:
                            api_responses.append(entry)
                        else:
                            obj = self.root_class.object_from_dictionary(entry)
                            api_responses.append(obj)
                elif self.response_type == 'entry':
                    data = content_obj['data']
                    if self.return_json:
                        api_responses = data
                    else:
                        api_responses = self.root_class.object_from_dictionary(
                            data)
                elif self.response_type == 'empty':
                    pass
                return api_responses, content_obj.get('pagination',
                                                      {}).get('next_url')
            else:
                raise InstagramAPIError(status_code,
                                        content_obj['meta']['error_type'],
                                        content_obj['meta']['error_message'])
Ejemplo n.º 2
0
 def execute(self):
     url, method, body, headers = OAuth2Request(self.api).prepare_request(self.method, 
                                                                          self.path, 
                                                                          self.parameters, 
                                                                          include_secret = self.include_secret)
     if self.as_generator:
         return self._paginator_with_url(url, method, body, headers)
     else:
         content, next = self._do_api_request(url, method, body, headers)
     if self.paginates:
         return content, next
     else:
         return content
Ejemplo n.º 3
0
        def _do_api_request(self, url, method="GET", body=None, headers=None):
            headers = headers or {}
            if self.signature and self.api.client_ips != None and self.api.client_secret != None:
                secret = self.api.client_secret
                ips = self.api.client_ips
                signature = hmac.new(secret, ips, sha256).hexdigest()
                headers['X-Insta-Forwarded-For'] = '|'.join([ips, signature])

            response, content = OAuth2Request(self.api).make_request(url, method=method, body=body, headers=headers)
            if response['status'] == '503' or response['status'] == '429':
                raise InstagramAPIError(response['status'], "Rate limited", "Your client is making too many request per second")

            try:
                content_obj = simplejson.loads(content)
            except ValueError:
                raise InstagramClientError('Unable to parse response, not valid JSON.', status_code=response['status'])

            # Handle OAuthRateLimitExceeded from Instagram's Nginx which uses different format to documented api responses
            if not content_obj.has_key('meta'):
                if content_obj.get('code') == 420 or content_obj.get('code') == 429:
                    error_message = content_obj.get('error_message') or "Your client is making too many request per second"
                    raise InstagramAPIError(content_obj.get('code'), "Rate limited", error_message)
                raise InstagramAPIError(content_obj.has_key('code'), content_obj.has_key('error_type'), content_obj.has_key('error_message'))

            api_responses = []
            status_code = content_obj['meta']['code']
            self.api.x_ratelimit_remaining = response.get("x-ratelimit-remaining",None)
            self.api.x_ratelimit = response.get("x-ratelimit-limit",None)
            if status_code == 200:
                if not self.objectify_response:
                    return content_obj, None

                if self.response_type == 'list':
                    for entry in content_obj['data']:
                        if self.return_json:
                            api_responses.append(entry)
                        else:
                            obj = self.root_class.object_from_dictionary(entry)
                            api_responses.append(obj)
                elif self.response_type == 'entry':
                    data = content_obj['data']
                    if self.return_json:
                        api_responses = data
                    else:
                        api_responses = self.root_class.object_from_dictionary(data)
                elif self.response_type == 'empty':
                    pass
                return api_responses, self._build_pagination_info(content_obj)
            else:
                raise InstagramAPIError(status_code, content_obj['meta']['error_type'], content_obj['meta']['error_message'])
Ejemplo n.º 4
0
        def _do_api_request(self, url, method="GET", body=None, headers=None):
            headers = headers or {}
            response, content = OAuth2Request(self.api).make_request(
                url, method=method, body=body, headers=headers)
            if response['status'] == '503':
                raise InstagramAPIError(
                    response['status'], "Rate limited",
                    "Your client is making too many request per second")

            try:
                content_obj = simplejson.loads(content)
            except ValueError:
                raise InstagramClientError(
                    'Unable to parse response, not valid JSON.')
            #self.response_type = 'list'
            api_responses = []
            status_code = content_obj['meta']['code']
            if status_code == 200:
                if not self.objectify_response:
                    return content_obj, None

                if self.response_type == 'list':
                    for entry in content_obj['data']:
                        if self.return_json:
                            api_responses.append(entry)
                        else:
                            obj = self.root_class.object_from_dictionary(entry)
                            api_responses.append(obj)
                elif self.response_type == 'entry':
                    data = content_obj['data']
                    if self.return_json:
                        api_responses = data
                    else:
                        api_responses = self.root_class.object_from_dictionary(
                            data)
                elif self.response_type == 'empty':
                    pass
                return api_responses, content_obj.get('pagination',
                                                      {}).get('next_url')
            else:
                print 'error:%s' % url
                raise InstagramAPIError(status_code,
                                        content_obj['meta']['error_type'],
                                        content_obj['meta']['error_message'])
Ejemplo n.º 5
0
        def _do_api_request(self, url, method="GET", body=None, headers={}):
            response, content = OAuth2Request(self.api).make_request(url, method=method, body=body, headers=headers)
            if response['status'] == '503':
                raise InstagramAPIError(response['status'], "Rate limited", "Your client is making too many request per second")
            content_obj = simplejson.loads(content)
            response_objects = []
            status_code = content_obj['meta']['code']
            if status_code == 200:
                if not self.objectify_response:
                    return content_obj, None

                if self.response_type == 'list':
                    for entry in content_obj['data']:
                        obj = self.root_class.object_from_dictionary(entry)
                        response_objects.append(obj)
                elif self.response_type == 'entry':
                    response_objects = self.root_class.object_from_dictionary(content_obj['data'])
                return response_objects, content_obj.get('pagination', {}).get('next_url') 
            else:
                raise InstagramAPIError(status_code, content_obj['meta']['error_type'], content_obj['meta']['error_message'])