Пример #1
0
    def request(self,
                method,
                endpoint,
                data=None,
                permissions=None,
                payload=None,
                **kwargs):
        parsed = urlparse(endpoint)
        if not parsed.scheme:
            actual_url = utils.urljoin(self.server_url, endpoint)
        else:
            actual_url = endpoint

        if self.auth is not None:
            kwargs.setdefault('auth', self.auth)

        payload = payload or {}
        # if data is not None:
        payload['data'] = data or {}
        if permissions is not None:
            if hasattr(permissions, 'as_dict'):
                permissions = permissions.as_dict()
            payload['permissions'] = permissions
        if payload and method not in ('get', 'head'):
            payload_kwarg = 'data' if 'files' in kwargs else 'json'
            kwargs.setdefault(payload_kwarg, payload)

        retry = self.nb_retry
        while retry >= 0:
            resp = requests.request(method, actual_url, **kwargs)
            retry = retry - 1
            if not (200 <= resp.status_code < 400):
                if resp.status_code >= 500 and retry >= 0:
                    # Wait and try again.
                    # If not forced, use retry-after header and wait.
                    if self.retry_after is None:
                        retry_after = resp.headers.get("Retry-After", 0)
                    else:
                        retry_after = self.retry_after
                    time.sleep(retry_after)
                    continue

                # Retries exhausted, raise expection.
                message = '{0} - {1}'.format(resp.status_code, resp.json())
                exception = KintoException(message)
                exception.request = resp.request
                exception.response = resp
                raise exception

        if resp.status_code == 304:
            body = None
        else:
            body = resp.json()
        # XXX Add the status code.
        return body, resp.headers
Пример #2
0
    def request(self, method, endpoint, data=None, permissions=None,
                payload=None, **kwargs):
        parsed = urlparse(endpoint)
        if not parsed.scheme:
            actual_url = utils.urljoin(self.server_url, endpoint)
        else:
            actual_url = endpoint

        if self.auth is not None:
            kwargs.setdefault('auth', self.auth)

        payload = payload or {}
        # if data is not None:
        payload['data'] = data or {}
        if permissions is not None:
            if hasattr(permissions, 'as_dict'):
                permissions = permissions.as_dict()
            payload['permissions'] = permissions
        if payload and method not in ('get', 'head'):
            payload_kwarg = 'data' if 'files' in kwargs else 'json'
            kwargs.setdefault(payload_kwarg, payload)

        retry = self.nb_retry
        while retry >= 0:
            resp = requests.request(method, actual_url, **kwargs)
            retry = retry - 1
            if not (200 <= resp.status_code < 400):
                if resp.status_code >= 500 and retry >= 0:
                    # Wait and try again.
                    # If not forced, use retry-after header and wait.
                    if self.retry_after is None:
                        retry_after = resp.headers.get("Retry-After", 0)
                    else:
                        retry_after = self.retry_after
                    time.sleep(retry_after)
                    continue

                # Retries exhausted, raise expection.
                message = '{0} - {1}'.format(resp.status_code, resp.json())
                exception = KintoException(message)
                exception.request = resp.request
                exception.response = resp
                raise exception

        if resp.status_code == 304:
            body = None
        else:
            body = resp.json()
        # XXX Add the status code.
        return body, resp.headers
Пример #3
0
 def send(self):
     result = []
     requests = self._build_requests()
     for chunk in utils.chunks(requests, self.batch_max_requests):
         kwargs = dict(method="POST", endpoint=self.endpoints.get("batch"), payload={"requests": chunk})
         resp, headers = self.session.request(**kwargs)
         for i, response in enumerate(resp["responses"]):
             status_code = response["status"]
             if not (200 <= status_code < 400):
                 message = "{0} - {1}".format(status_code, response["body"])
                 exception = KintoException(message)
                 exception.request = chunk[i]
                 exception.response = response
                 raise exception
         result.append((resp, headers))
     return result
Пример #4
0
 def send(self):
     result = []
     requests = self._build_requests()
     for chunk in utils.chunks(requests, self.batch_max_requests):
         kwargs = dict(method='POST',
                       endpoint=self.endpoints.get('batch'),
                       payload={'requests': chunk})
         resp, headers = self.session.request(**kwargs)
         for i, response in enumerate(resp['responses']):
             status_code = response['status']
             if not (200 <= status_code < 400):
                 message = '{0} - {1}'.format(status_code, response['body'])
                 exception = KintoException(message)
                 exception.request = chunk[i]
                 exception.response = response
                 raise exception
         result.append((resp, headers))
     return result
Пример #5
0
 def send(self):
     result = []
     requests = self._build_requests()
     for chunk in utils.chunks(requests, self.batch_max_requests):
         kwargs = dict(method='POST',
                       endpoint=self.endpoints.get('batch'),
                       payload={'requests': chunk})
         resp, headers = self.session.request(**kwargs)
         for i, response in enumerate(resp['responses']):
             status_code = response['status']
             if not (200 <= status_code < 400):
                 message = '{0} - {1}'.format(status_code, response['body'])
                 exception = KintoException(message)
                 exception.request = chunk[i]
                 exception.response = response
                 raise exception
         result.append((resp, headers))
     return result
Пример #6
0
def get_http_error(status):
    exception = KintoException()
    exception.response = mock.MagicMock()
    exception.response.status_code = status
    exception.request = mock.sentinel.request
    return exception
Пример #7
0
def get_http_error(status):
    exception = KintoException()
    exception.response = mock.MagicMock()
    exception.response.status_code = status
    exception.request = mock.sentinel.request
    return exception