def request(self, method, path=None, payload=None, headers=None, **params): """ HTTP request This method may be the only one you want to override when subclassing `restkit.rest.Resource`. :param payload: string or File object passed to the body of the request :param path: string additionnal path to the uri :param headers: dict, optionnal headers that will be added to HTTP request. :param params: Optionnal parameterss added to the request """ headers = headers or [] uri = util.make_uri( self.uri, path, charset=self.charset, safe=self.safe, encode_keys=self.encode_keys, **params ) resp = self.do_request(uri, method=method, payload=payload, headers=headers) if resp is None: # race condition raise ValueError("Unkown error: response object is None") if resp.status_int >= 400: if resp.status_int == 404: raise ResourceNotFound(resp.body_string(), response=resp) elif resp.status_int in (401, 403): raise Unauthorized(resp.body_string(), http_code=resp.status_int, response=resp) else: raise RequestFailed(resp.body_string(), http_code=resp.status_int, response=resp) return resp
def update_uri(self, path): """ to set a new uri absolute path """ self.uri = util.make_uri(self.uri, path, charset=self.charset, safe=self.safe, encode_keys=self.encode_keys)
def __call__(self, path): """if you want to add a path to resource uri, you can do: .. code-block:: python Resource("/path").get() """ client_opts = self.client_opts.copy() client_opts["filters"] = self.filters new_uri = util.make_uri(self.uri, path, charset=self.charset, safe=self.safe, encode_keys=self.encode_keys) obj = type(self)(new_uri, headers=self._headers, **client_opts) return self._set_default_attrs(obj)
def request(self, method, path=None, payload=None, headers=None, **params): """ HTTP request This method may be the only one you want to override when subclassing `restkit.rest.Resource`. :param payload: string or File object passed to the body of the request :param path: string additionnal path to the uri :param headers: dict, optionnal headers that will be added to HTTP request. :param params: Optionnal parameterss added to the request """ headers = headers or [] uri = util.make_uri(self.uri, path, charset=self.charset, safe=self.safe, encode_keys=self.encode_keys, **params) resp = self.do_request(uri, method=method, payload=payload, headers=headers) if resp is None: # race condition raise ValueError("Unkown error: response object is None") if resp.status_int >= 400: if resp.status_int == 404: raise ResourceNotFound(resp.body_string(), response=resp) elif resp.status_int in (401, 403): raise Unauthorized(resp.body_string(), http_code=resp.status_int, response=resp) else: raise RequestFailed(resp.body_string(), http_code=resp.status_int, response=resp) return resp