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
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
def test_synchronize_raises_SynchronizationError_if_create_fails(self): error = KintoException() error.response = mock.MagicMock(content='foobar') with mock.patch('xml2kinto.synchronize.KintoRecords') as KintoRecords: KintoRecords.return_value.records = [] KintoRecords.return_value.find.return_value = None KintoRecords.return_value.create.side_effect = error here = os.path.dirname(__file__) test_file = os.path.join(here, 'test_synchronize.xml') with pytest.raises(SynchronizationError): synchronize(FIELDS, xml_options={'filename': test_file}, kinto_options=mock.MagicMock())
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
def get_http_error(status): exception = KintoException() exception.response = mock.MagicMock() exception.response.status_code = status exception.request = mock.sentinel.request return exception