def wrap_api_call(self, func, *args, **kw): """ Wrap a self.<rest function> with exception handling :param func: The function to wrap """ # Prepend the endpoint URI args = list(args) args[0] = '%s/%s' % (self.endpoint, args[0]) if self.insecure is True: kw['verify'] = False # Trigger the request response = func(*args, **kw) # Decode is response, if possible try: response_payload = response.json() except ValueError: response_payload = {} if response.status_code == 400: raise exceptions.BadRequest(**response_payload) elif response.status_code in (401, 403): raise exceptions.Forbidden(**response_payload) elif response.status_code == 404: raise exceptions.NotFound(**response_payload) elif response.status_code == 409: raise exceptions.Conflict(**response_payload) elif response.status_code >= 500: raise exceptions.Unknown(**response_payload) else: return response
def request(self, *args, **kwargs): kwargs.setdefault('raise_exc', False) if self.timeout is not None: kwargs.setdefault('timeout', self.timeout) kwargs.setdefault('headers', {}).setdefault( 'Content-Type', 'application/json') response, body = super(self.__class__, self).request(*args, **kwargs) # Decode is response, if possible try: response_payload = response.json() except ValueError: response_payload = {} if response.status_code == 400: raise exceptions.BadRequest(**response_payload) elif response.status_code in (401, 403): raise exceptions.Forbidden(**response_payload) elif response.status_code == 404: raise exceptions.NotFound(**response_payload) elif response.status_code == 409: raise exceptions.Conflict(**response_payload) elif response.status_code >= 500: raise exceptions.Unknown(**response_payload) return response, body
def wrap_api_call(self, func, *args, **kw): """ Wrap a self.<rest function> with exception handling :param func: The function to wrap """ kw['raise_exc'] = False kw.setdefault('headers', {}) kw['headers'].setdefault('Content-Type', 'application/json') if self.all_tenants: kw['headers'].update({'X-Auth-All-Projects': 'true'}) if self.edit_managed: kw['headers'].update({'X-Designate-Edit-Managed-Records': 'true'}) # Trigger the request response = func(*args, **kw) # Decode is response, if possible try: response_payload = response.json() except ValueError: response_payload = {} if response.status_code == 400: raise exceptions.BadRequest(**response_payload) elif response.status_code in (401, 403, 413): raise exceptions.Forbidden(**response_payload) elif response.status_code == 404: raise exceptions.NotFound(**response_payload) elif response.status_code == 409: raise exceptions.Conflict(**response_payload) elif response.status_code >= 500: raise exceptions.Unknown(**response_payload) else: return response
def request(self, *args, **kwargs): kwargs.setdefault('raise_exc', False) if self.timeout is not None: kwargs.setdefault('timeout', self.timeout) kwargs.setdefault('headers', {}) if self.all_projects: kwargs['headers'].setdefault( 'X-Auth-All-Projects', # backported fix from https://review.opendev.org/#/c/390965/ str(self.all_projects)) if self.edit_managed: kwargs['headers'].setdefault('X-Designate-Edit-Managed-Records', self.edit_managed) if self.sudo_project_id is not None: kwargs['headers'].setdefault('X-Auth-Sudo-Project-ID', self.sudo_project_id) kwargs['headers'].setdefault('Content-Type', 'application/json') response, body = super(self.__class__, self).request(*args, **kwargs) # Decode is response, if possible try: response_payload = response.json() except ValueError: response_payload = {} body = response.text if response.status_code == 400: raise exceptions.BadRequest(**response_payload) elif response.status_code in (401, 403): raise exceptions.Forbidden(**response_payload) elif response.status_code == 404: raise exceptions.NotFound(**response_payload) elif response.status_code == 409: raise exceptions.Conflict(**response_payload) elif response.status_code >= 500: raise exceptions.Unknown(**response_payload) return response, body