Ejemplo n.º 1
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)
     self.initial['uri'] =  util.make_uri(self.initial['uri'], path, 
                                 charset=self.charset, 
                                 safe=self.safe, 
                                 encode_keys=self.encode_keys)
Ejemplo n.º 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)
     self.initial['uri'] =  util.make_uri(self.initial['uri'], path, 
                                 charset=self.charset, 
                                 safe=self.safe, 
                                 encode_keys=self.encode_keys)
Ejemplo n.º 3
0
    def request(self,
                method,
                path=None,
                payload=None,
                headers=None,
                params_dict=None,
                **params):
        """ HTTP request

        This method may be the only one you want to override when
        subclassing `restkit.rest.Resource`.

        - payload: string or File object passed to the body of the request
        - path: string  additionnal path to the uri
        - headers: dict, optionnal headers that will
            be added to HTTP request.
        :params_dict: Options parameters added to the request as a dict
        - params: Optionnal parameterss added to the request
        """

        params = params or {}
        params.update(params_dict or {})

        while True:
            uri = util.make_uri(self.uri,
                                path,
                                charset=self.charset,
                                safe=self.safe,
                                encode_keys=self.encode_keys,
                                **self.make_params(params))

            # make request

            resp = self.client.request(uri,
                                       method=method,
                                       body=payload,
                                       headers=self.make_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):
                    if self.unauthorized(resp):
                        raise Unauthorized(resp.body_string(),
                                           http_code=resp.status_int,
                                           response=resp)
                elif resp.status_int == 410:
                    raise ResourceGone(resp.body_string(), response=resp)
                else:
                    raise RequestFailed(resp.body_string(),
                                        http_code=resp.status_int,
                                        response=resp)
            else:
                break

        return resp
Ejemplo n.º 4
0
 def uri_str(self, path, params={}):
     return util.make_uri(self.uri,
                          path,
                          charset=self.charset,
                          safe=self.safe,
                          encode_keys=self.encode_keys,
                          **self.make_params(params))
Ejemplo n.º 5
0
    def request(self, method, path=None, payload=None, headers=None, 
        params_dict=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.
        :params_dict: Options parameters added to the request as a dict
        :param params: Optionnal parameterss added to the request
        """
        
        params = params or {}
        params.update(params_dict or {})

        while True:
            uri = util.make_uri(self.uri, path, charset=self.charset, 
                        safe=self.safe, encode_keys=self.encode_keys,
                        **self.make_params(params))
        
            # make request
            
            resp = self.client.request(uri, method=method, body=payload, 
                        headers=self.make_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):
                    if self.unauthorized(resp):
                        raise Unauthorized(resp.body_string(), 
                                http_code=resp.status_int, 
                                response=resp)
                elif resp.status_int == 410:
                    raise ResourceGone(resp.body_string(), response=resp)
                else:
                    raise RequestFailed(resp.body_string(), 
                                http_code=resp.status_int,
                                response=resp)
            else:
                break

        return resp
Ejemplo n.º 6
0
def test_002():
    t.eq(util.make_uri("http://localhost", "/"), "http://localhost/")
    t.eq(util.make_uri("http://localhost/"), "http://localhost/")
    t.eq(util.make_uri("http://localhost/", "/test/echo"),
         "http://localhost/test/echo")
    t.eq(util.make_uri("http://localhost/", "/test/echo/"),
         "http://localhost/test/echo/")
    t.eq(util.make_uri("http://localhost", "/test/echo/"),
         "http://localhost/test/echo/")
    t.eq(util.make_uri("http://localhost", "test/echo"),
         "http://localhost/test/echo")
    t.eq(util.make_uri("http://localhost", "test/echo/"),
         "http://localhost/test/echo/")
Ejemplo n.º 7
0
    def __call__(self, path):
        """if you want to add a path to resource uri, you can do:
        
        .. code-block:: python

            Resource("/path").get()
        """

        uri = self.initial['uri']

        new_uri = util.make_uri(uri, path, charset=self.charset, 
                        safe=self.safe, encode_keys=self.encode_keys)
                                                
        obj = type(self)(new_uri, **self.initial['client_opts'])
        return obj
Ejemplo n.º 8
0
    def __call__(self, path):
        """if you want to add a path to resource uri, you can do:
        
        .. code-block:: python

            Resource("/path").get()
        """

        uri = self.initial['uri']

        new_uri = util.make_uri(uri, path, charset=self.charset, 
                        safe=self.safe, encode_keys=self.encode_keys)
                                                
        obj = type(self)(new_uri, **self.initial['client_opts'])
        return obj