Beispiel #1
0
    def prepare_payload(op, method, uri, data):
        """Return the URI (modified perhaps) and body and headers.

        - For GET requests, encode parameters in the query string.

        - Otherwise always encode parameters in the request body.

        - Except op; this can always go in the query string.

        :param method: The HTTP method.
        :param uri: The URI of the action.
        :param data: A dict or iterable of name=value pairs to pack into the
            body or headers, depending on the type of request.
        """
        if method == "GET":
            query = data if op is None else chain([("op", op)], data)
            body, headers = None, {}
        else:
            query = [] if op is None else [("op", op)]
            if data:
                body, headers = encode_multipart_data(data)
            else:
                body, headers = None, {}

        uri = urlparse(uri)._replace(query=urlencode(query)).geturl()
        return uri, body, headers
Beispiel #2
0
    def prepare_payload(cls, op, method, uri, data):
        """Return the URI (modified perhaps) and body and headers.

        - For GET requests, encode parameters in the query string.

        - Otherwise always encode parameters in the request body.

        - Except op; this can always go in the query string.

        :param method: The HTTP method.
        :param uri: The URI of the action.
        :param data: An iterable of ``name, value`` or ``name, opener``
            tuples (see `name_value_pair`) to pack into the body or
            query, depending on the type of request.
        """
        query = [] if op is None else [("op", op)]

        def slurp(opener):
            with opener() as fd:
                return fd.read()

        if method in ["GET", "DELETE"]:
            query.extend((name, slurp(value) if callable(value) else value)
                         for name, value in data)
            body, headers = None, []
        else:
            if data is None or len(data) == 0:
                body, headers = None, []
            else:
                message = build_multipart_message(data)
                headers, body = encode_multipart_message(message)

        uri = urlparse(uri)._replace(query=urlencode(query)).geturl()
        return uri, body, headers
Beispiel #3
0
 def get_context_data(self, **kwargs):
     context = super(ZoneView, self).get_context_data(**kwargs)
     query_string = urlencode(
         [('query', 'zone:(%s)' % self.get_object().name)])
     context["node_list_link"] = (
         reverse('index') + "#/nodes" + "?" + query_string)
     return context
Beispiel #4
0
    def prepare_payload(op, method, uri, data):
        """Return the URI (modified perhaps) and body and headers.

        - For GET requests, encode parameters in the query string.

        - Otherwise always encode parameters in the request body.

        - Except op; this can always go in the query string.

        :param method: The HTTP method.
        :param uri: The URI of the action.
        :param data: A dict or iterable of name=value pairs to pack into the
            body or headers, depending on the type of request.
        """
        if method == "GET":
            query = data if op is None else chain([("op", op)], data)
            body, headers = None, {}
        else:
            query = [] if op is None else [("op", op)]
            if data:
                body, headers = encode_multipart_data(data)
            else:
                body, headers = None, {}

        uri = urlparse(uri)._replace(query=urlencode(query)).geturl()
        return uri, body, headers
Beispiel #5
0
 def test_urlencode_encodes_utf8_and_quotes(self):
     # urlencode UTF-8 encodes unicode strings and applies standard query
     # string quoting rules, and always returns a byte string.
     data = [("=\u1234", "&\u4321")]
     query = urlencode(data)
     self.assertThat(
         query,
         MatchesAll(Equals("%3D%E1%88%B4=%26%E4%8C%A1"), IsInstance(str)))
Beispiel #6
0
 def test_urlencode_roundtrip_through_django(self):
     # Check that urlencode's approach works with Django, as described on
     # https://docs.djangoproject.com/en/dev/ref/unicode/.
     data = [("=\u1234", "&\u4321")]
     query = urlencode(data)
     name, value = query.split("=")
     name, value = unquote(name), unquote(value)
     name, value = smart_text(name), smart_text(value)
     self.assertEqual(data, [(name, value)])
Beispiel #7
0
 def test_urlencode_roundtrip_through_django(self):
     # Check that urlencode's approach works with Django, as described on
     # https://docs.djangoproject.com/en/dev/ref/unicode/.
     data = [("=\u1234", "&\u4321")]
     query = urlencode(data)
     name, value = query.split(b"=")
     name, value = unquote(name), unquote(value)
     name, value = smart_unicode(name), smart_unicode(value)
     self.assertEqual(data, [(name, value)])
Beispiel #8
0
 def test_urlencode_encodes_utf8_and_quotes(self):
     # urlencode UTF-8 encodes unicode strings and applies standard query
     # string quoting rules, and always returns a byte string.
     data = [("=\u1234", "&\u4321")]
     query = urlencode(data)
     self.assertThat(
         query, MatchesAll(
             Equals(b"%3D%E1%88%B4=%26%E4%8C%A1"),
             IsInstance(bytes)))
Beispiel #9
0
    def _formulate_get(self, path, params=None):
        """Return URL and headers for a GET request.

        This is similar to _formulate_change, except parameters are encoded
        into the URL.

        :param path: Path to the object to issue a GET on.
        :param params: Optional dict of parameter values.
        :return: A tuple: URL and headers for the request.
        """
        url = self._make_url(path)
        if params is not None and len(params) > 0:
            url += "?" + urlencode(self._flatten(params))
        headers = {}
        self.auth.sign_request(url, headers)
        return url, headers
Beispiel #10
0
    def _formulate_get(self, path, params=None):
        """Return URL and headers for a GET request.

        This is similar to _formulate_change, except parameters are encoded
        into the URL.

        :param path: Path to the object to issue a GET on.
        :param params: Optional dict of parameter values.
        :return: A tuple: URL and headers for the request.
        """
        url = self._make_url(path)
        if params is not None and len(params) > 0:
            url += "?" + urlencode(params.items())
        headers = {}
        self.auth.sign_request(url, headers)
        return url, headers
Beispiel #11
0
    def prepare_payload(cls, op, method, uri, data):
        """Return the URI (modified perhaps) and body and headers.

        - For GET requests, encode parameters in the query string.

        - Otherwise always encode parameters in the request body.

        - Except op; this can always go in the query string.

        :param method: The HTTP method.
        :param uri: The URI of the action.
        :param data: An iterable of ``name, value`` or ``name, opener``
            tuples (see `name_value_pair`) to pack into the body or
            query, depending on the type of request.
        """
        center('RestClient.prepare_payload')
        cdebug('    op : %s' % op)
        cdebug('method : %s' % method)
        cdebug('   uri : %s' % uri)
        cdebug('  data : %s' % data)
        query = [] if op is None else [("op", op)]

        def slurp(opener):
            with opener() as fd:
                return fd.read()

        if method == "GET":
            query.extend(
                (name, slurp(value) if callable(value) else value)
                for name, value in data)
            body, headers = None, []
        else:
            if data is None or len(data) == 0:
                body, headers = None, []
            else:
                cdebug('encode multipart')
                message = build_multipart_message(data)
                headers, body = encode_multipart_message(message)

        uri = urlparse(uri)._replace(query=urlencode(query)).geturl()
        cleave('RestClient.prepare_payload')
        cdebug('    uri : %s' % uri)
        cdebug('   body : %s' % body)
        cdebug('headers : %s' % headers)
        return uri, body, headers
Beispiel #12
0
    def prepare_payload(cls, op, method, uri, data):
        """Return the URI (modified perhaps) and body and headers.

        - For GET requests, encode parameters in the query string.

        - Otherwise always encode parameters in the request body.

        - Except op; this can always go in the query string.

        :param method: The HTTP method.
        :param uri: The URI of the action.
        :param data: An iterable of ``name, value`` or ``name, opener``
            tuples (see `name_value_pair`) to pack into the body or
            query, depending on the type of request.
        """
        params_in_qs = method in ("GET", "DELETE")
        query = [("op", op)] if op else []

        def slurp(opener):
            with opener("r" if params_in_qs else "rb") as fd:
                return fd.read()

        # read content for file-based parameters
        data = [
            (key, slurp(value) if callable(value) else value)
            for key, value in data
        ]

        headers, body = [], None
        if params_in_qs:
            query.extend(data)
        elif data:
            message = build_multipart_message(data)
            headers, body = encode_multipart_message(message)

        uri = urlparse(uri)._replace(query=urlencode(query)).geturl()
        return uri, body, headers
Beispiel #13
0
    def _formulate_change(self, path, params, as_json=False):
        """Return URL, headers, and body for a non-GET request.

        This is similar to _formulate_get, except parameters are encoded as
        a multipart form body.

        :param path: Path to the object to issue a GET on.
        :param params: A dict of parameter values.
        :param as_json: Encode params as application/json instead of
            multipart/form-data. Only use this if you know the API already
            supports JSON requests.
        :return: A tuple: URL, headers, and body for the request.
        """
        url = self._make_url(path)
        if "op" in params:
            params = dict(params)
            op = params.pop("op")
            url += "?" + urlencode([("op", op)])
        if as_json:
            body, headers = encode_json_data(params)
        else:
            body, headers = encode_multipart_data(params, {})
        self.auth.sign_request(url, headers)
        return url, headers, body
Beispiel #14
0
    def _formulate_change(self, path, params, as_json=False):
        """Return URL, headers, and body for a non-GET request.

        This is similar to _formulate_get, except parameters are encoded as
        a multipart form body.

        :param path: Path to the object to issue a GET on.
        :param params: A dict of parameter values.
        :param as_json: Encode params as application/json instead of
            multipart/form-data. Only use this if you know the API already
            supports JSON requests.
        :return: A tuple: URL, headers, and body for the request.
        """
        url = self._make_url(path)
        if 'op' in params:
            params = dict(params)
            op = params.pop('op')
            url += '?' + urlencode([('op', op)])
        if as_json:
            body, headers = encode_json_data(params)
        else:
            body, headers = encode_multipart_data(params, {})
        self.auth.sign_request(url, headers)
        return url, headers, body