Beispiel #1
0
    def call_action(self, action, data_dict=None, context=None, apikey=None,
            files=None, requests_kwargs=None):
        """
        :param action: the action name, e.g. 'package_create'
        :param data_dict: the dict to pass to the action as JSON,
                          defaults to {}
        :param context: always set to None for RemoteCKAN
        :param apikey: API key for authentication
        :param files: None or {field-name: file-to-be-sent, ...}

        This function parses the response from the server as JSON and
        returns the decoded value.  When an error is returned this
        function will convert it back to an exception that matches the
        one the action function itself raised.
        """
        if context:
            raise CKANAPIError("RemoteCKAN.call_action does not support "
                "use of context parameter, use apikey instead")
        if files and self.get_only:
            raise CKANAPIError("RemoteCKAN: files may not be sent when "
                "get_only is True")
        url, data, headers = prepare_action(
            action, data_dict, apikey or self.apikey, files)
        headers['User-Agent'] = self.user_agent
        url = self.address.rstrip('/') + '/' + url
        requests_kwargs = requests_kwargs or {}
        if not self.session:
            self.session = requests.Session()
        if self.get_only:
            status, response = self._request_fn_get(url, data_dict, headers, requests_kwargs)
        else:
            status, response = self._request_fn(url, data, headers, files, requests_kwargs)
        return reverse_apicontroller_action(url, status, response)
Beispiel #2
0
    def call_action(self,
                    action,
                    data_dict=None,
                    context=None,
                    apikey=None,
                    files=None,
                    requests_kwargs=None,
                    progress=None):
        """
        :param action: the action name, e.g. 'package_create'
        :param data_dict: the dict to pass to the action as JSON,
                          defaults to {}
        :param context: always set to None for RemoteCKAN
        :param apikey: API key for authentication
        :param files: None or {field-name: file-to-be-sent, ...}
        :param progress: A callable that takes an instance of
            :class:`requests_toolbelt.MultipartEncoder` as parameter and returns
            a callback funtion. The callback function will be called every time
            data is read from the file-to-be-sent and it will be passed the
            instance of :class:`requests_toolbelt.MultipartEncoderMonitor`. This
            monitor has the attribute `bytes_read` that can be used to display
            a progress bar. An example is implemented in ckanapi.cli.

        This function parses the response from the server as JSON and
        returns the decoded value.  When an error is returned this
        function will convert it back to an exception that matches the
        one the action function itself raised.
        """
        if context:
            raise CKANAPIError("RemoteCKAN.call_action does not support "
                               "use of context parameter, use apikey instead")
        if files and self.get_only:
            raise CKANAPIError("RemoteCKAN: files may not be sent when "
                               "get_only is True")
        url, data, headers = prepare_action(action, data_dict, apikey
                                            or self.apikey, files)
        headers['User-Agent'] = self.user_agent
        url = self.address.rstrip('/') + '/' + url
        requests_kwargs = requests_kwargs or {}
        if not self.session:
            self.session = requests.Session()
        if self.get_only:
            status, response = self._request_fn_get(url, data_dict, headers,
                                                    requests_kwargs)
        else:
            status, response = self._request_fn(url, data, headers, files,
                                                requests_kwargs, progress)
        return reverse_apicontroller_action(url, status, response)
Beispiel #3
0
    def call_action(self,
                    action,
                    data_dict=None,
                    context=None,
                    apikey=None,
                    files=None,
                    requests_kwargs=None):
        """
        :param action: the action name, e.g. 'package_create'
        :param data_dict: the dict to pass to the action as JSON,
                          defaults to {}
        :param context: always set to None for RemoteCKAN
        :param apikey: API key for authentication
        :param files: None or {field-name: file-to-be-sent, ...}

        This function parses the response from the server as JSON and
        returns the decoded value.  When an error is returned this
        function will convert it back to an exception that matches the
        one the action function itself raised.
        """
        if context:
            raise CKANAPIError("RemoteCKAN.call_action does not support "
                               "use of context parameter, use apikey instead")
        if files and self.get_only:
            raise CKANAPIError("RemoteCKAN: files may not be sent when "
                               "get_only is True")
        url, data, headers = prepare_action(action,
                                            data_dict,
                                            apikey or self.apikey,
                                            files,
                                            base_url=self.base_url)
        headers['User-Agent'] = self.user_agent
        url = self.address.rstrip('/') + '/' + url
        requests_kwargs = requests_kwargs or {}
        if not self.session:
            self.session = requests.Session()
        if self.get_only:
            status, response = self._request_fn_get(url, data_dict, headers,
                                                    requests_kwargs)
        else:
            status, response = self._request_fn(url, data, headers, files,
                                                requests_kwargs)
        return reverse_apicontroller_action(url, status, response)
Beispiel #4
0
    def call_action(self,
                    action,
                    data_dict=None,
                    context=None,
                    apikey=None,
                    files=None):
        """
        :param action: the action name, e.g. 'package_create'
        :param data_dict: the dict to pass to the action as JSON,
                          defaults to {}
        :param context: not supported
        :param files: None or {field-name: file-to-be-sent, ...}

        This function parses the response from the server as JSON and
        returns the decoded value.  When an error is returned this
        function will convert it back to an exception that matches the
        one the action function itself raised.
        """
        if context:
            raise CKANAPIError("TestAppCKAN.call_action does not support "
                               "use of context parameter, use apikey instead")
        url, data, headers = prepare_action(action, data_dict, apikey
                                            or self.apikey, files)

        kwargs = {}
        if files:
            # Convert the list of (fieldname, file_object) tuples into the
            # (fieldname, filename, file_contents) tuples that webtests needs.
            upload_files = []
            for fieldname, file_ in files.items():
                if hasattr(file_, 'name'):
                    filename = os.path.split(file_.name)[1]
                else:
                    filename = fieldname
                upload_files.append((fieldname, filename, file_.read()))
            kwargs['upload_files'] = upload_files

        r = self.test_app.post('/' + url,
                               params=data,
                               headers=headers,
                               expect_errors=True,
                               **kwargs)
        return reverse_apicontroller_action(url, r.status, r.body)
    def call_action(self, action, data_dict=None, context=None, apikey=None,
            files=None):
        """
        :param action: the action name, e.g. 'package_create'
        :param data_dict: the dict to pass to the action as JSON,
                          defaults to {}
        :param context: not supported
        :param files: None or {field-name: file-to-be-sent, ...}

        This function parses the response from the server as JSON and
        returns the decoded value.  When an error is returned this
        function will convert it back to an exception that matches the
        one the action function itself raised.
        """
        if context:
            raise CKANAPIError("TestAppCKAN.call_action does not support "
                "use of context parameter, use apikey instead")
        url, data, headers = prepare_action(action, data_dict,
                                            apikey or self.apikey, files)

        kwargs = {}
        if files:
            # Convert the list of (fieldname, file_object) tuples into the
            # (fieldname, filename, file_contents) tuples that webtests needs.
            upload_files = []
            for fieldname, file_ in files.items():
                if hasattr(file_, 'name'):
                    filename = os.path.split(file_.name)[1]
                else:
                    filename = fieldname
                upload_files.append( (fieldname, filename, file_.read()) )
            kwargs['upload_files'] = upload_files

        r = self.test_app.post('/' + url, data, headers, expect_errors=True,
                               **kwargs)
        return reverse_apicontroller_action(url, r.status, r.body)