Beispiel #1
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 #2
0
    def prepare_initial_payload(self, data):
        """Return the body and headers for the initial request.

        This is method is only used for the first request to MAAS. It
        removes the passed content, and replaces it with the sha256 and size
        of that content.

        :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.
        """
        data = dict(data)
        if 'content' not in data:
            print('Missing content.')
            raise CommandError(2)

        content = data.pop('content')
        size, sha256 = self.calculate_size_and_sha256(content)
        data['size'] = '%s' % size
        data['sha256'] = sha256

        data = sorted((key, value) for key, value in data.items())
        message = build_multipart_message(data)
        headers, body = encode_multipart_message(message)
        return body, headers
Beispiel #3
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 #4
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