示例#1
0
    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
示例#2
0
 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)
示例#3
0
    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)
示例#4
0
    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)
示例#5
0
    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
示例#6
0
 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)