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_after = resp.headers.get("Backoff") if retry_after: message = '{0} - {1}'.format(resp.status_code, resp.json()) exception = BackoffException(message, retry_after) exception.request = resp.request exception.response = resp raise exception 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 request(self, method, endpoint, data=None, permissions=None, payload=None, **kwargs): current_time = time.time() if self.backoff and self.backoff > current_time: seconds = int(self.backoff - current_time) raise BackoffException("Retry after {} seconds".format(seconds), seconds) 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) if kwargs.get('headers') is None: kwargs['headers'] = dict() if kwargs.get('params') is not None: params = dict() for key, value in kwargs["params"].items(): if key.startswith('in_') or key.startswith('exclude_'): params[key] = ','.join(value) elif isinstance(value, str): params[key] = value else: params[key] = json.dumps(value) kwargs['params'] = params if not isinstance(kwargs['headers'], dict): raise TypeError("headers must be a dict (got {})".format( kwargs['headers'])) # Set the default User-Agent if not already defined. # In the meantime, clone the header dict to avoid changing the # user header dict when adding information. kwargs['headers'] = {"User-Agent": USER_AGENT, **kwargs["headers"]} payload = payload or {} if data is not None: payload['data'] = data if permissions is not None: if hasattr(permissions, 'as_dict'): permissions = permissions.as_dict() payload['permissions'] = permissions if method not in ('get', 'head'): if 'files' in kwargs: kwargs.setdefault('data', payload) else: kwargs.setdefault('data', utils.json_dumps(payload)) kwargs['headers'].setdefault('Content-Type', 'application/json') retry = self.nb_retry while retry >= 0: resp = requests.request(method, actual_url, **kwargs) backoff_seconds = resp.headers.get("Backoff") if backoff_seconds: self.backoff = time.time() + int(backoff_seconds) else: self.backoff = None retry = retry - 1 if 200 <= resp.status_code < 400: # Success break else: if retry >= 0 and (resp.status_code >= 500 or resp.status_code == 409): # Wait and try again. # If not forced, use retry-after header and wait. if self.retry_after is None: retry_after = int(resp.headers.get("Retry-After", 0)) else: retry_after = self.retry_after time.sleep(retry_after) continue # Retries exhausted, raise expection. try: message = '{0} - {1}'.format(resp.status_code, resp.json()) except ValueError: # In case the response is not JSON, fallback to text. message = '{0} - {1}'.format(resp.status_code, resp.text) exception = KintoException(message) exception.request = resp.request exception.response = resp raise exception if resp.status_code == 304 or method == 'head': body = None else: body = resp.json() return body, resp.headers
def request(self, method, endpoint, data=None, permissions=None, payload=None, **kwargs): current_time = time.time() if self.backoff and self.backoff > current_time: seconds = int(self.backoff - current_time) raise BackoffException("Retry after {} seconds".format(seconds), seconds) 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 if permissions is not None: if hasattr(permissions, 'as_dict'): permissions = permissions.as_dict() payload['permissions'] = permissions if method not in ('get', 'head'): payload_kwarg = 'data' if 'files' in kwargs else 'json' kwargs.setdefault(payload_kwarg, payload) # Set the default User-Agent if not already defined. if not isinstance(kwargs.get('headers'), dict): kwargs['headers'] = {} kwargs['headers'].setdefault('User-Agent', USER_AGENT) retry = self.nb_retry while retry >= 0: resp = requests.request(method, actual_url, **kwargs) backoff_seconds = resp.headers.get("Backoff") if backoff_seconds: self.backoff = time.time() + int(backoff_seconds) else: self.backoff = None retry = retry - 1 if 200 <= resp.status_code < 400: # Success break else: 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 = int(resp.headers.get("Retry-After", 0)) else: retry_after = self.retry_after time.sleep(retry_after) continue # Retries exhausted, raise expection. try: message = '{0} - {1}'.format(resp.status_code, resp.json()) except ValueError: # In case the response is not JSON, fallback to text. message = '{0} - {1}'.format(resp.status_code, resp.text) exception = KintoException(message) exception.request = resp.request exception.response = resp raise exception if resp.status_code == 304 or method == 'head': body = None else: body = resp.json() return body, resp.headers
def request(self, method, endpoint, data=None, permissions=None, payload=None, **kwargs): current_time = time.time() if self.backoff and self.backoff > current_time: seconds = int(self.backoff - current_time) raise BackoffException("Retry after {} seconds".format(seconds), seconds) parsed = urlparse(endpoint) if not parsed.scheme: actual_url = utils.urljoin(self.server_url, endpoint) else: actual_url = endpoint if self.timeout is not False: kwargs.setdefault("timeout", self.timeout) if self.auth is not None: kwargs.setdefault("auth", self.auth) if kwargs.get("params") is not None: params = dict() for key, value in kwargs["params"].items(): if key.startswith("in_") or key.startswith("exclude_"): params[key] = ",".join(value) elif isinstance(value, str): params[key] = value else: params[key] = json.dumps(value) kwargs["params"] = params overridden_headers = kwargs.get("headers") or {} # Set the default User-Agent if not already defined. kwargs["headers"] = {"User-Agent": USER_AGENT, **self.headers, **overridden_headers} payload = payload or {} if data is not None: payload["data"] = data if permissions is not None: if hasattr(permissions, "as_dict"): permissions = permissions.as_dict() payload["permissions"] = permissions if method not in ("get", "head"): if "files" in kwargs: kwargs.setdefault("data", payload) else: kwargs.setdefault("data", utils.json_dumps(payload)) kwargs["headers"].setdefault("Content-Type", "application/json") retry = self.nb_retry while retry >= 0: resp = requests.request(method, actual_url, **kwargs) backoff_seconds = resp.headers.get("Backoff") if backoff_seconds: self.backoff = time.time() + int(backoff_seconds) else: self.backoff = None retry = retry - 1 if 200 <= resp.status_code < 400: # Success break else: if retry >= 0 and (resp.status_code >= 500 or resp.status_code == 409): # Wait and try again. # If not forced, use retry-after header and wait. if self.retry_after is None: retry_after = int(resp.headers.get("Retry-After", 0)) else: retry_after = self.retry_after time.sleep(retry_after) continue # Retries exhausted, raise expection. try: message = "{0} - {1}".format(resp.status_code, resp.json()) except ValueError: # In case the response is not JSON, fallback to text. message = "{0} - {1}".format(resp.status_code, resp.text) exception = KintoException(message) exception.request = resp.request exception.response = resp raise exception if resp.status_code == 204 or resp.status_code == 304 or method == "head": body = None else: body = resp.json() return body, resp.headers