Exemple #1
0
 def get_metadata(self, dataset_identifier, content_type="json"):
     """
     Retrieve the metadata for a particular dataset.
     """
     resource = utils.format_old_api_request(dataid=dataset_identifier,
                                             content_type=content_type)
     return self._perform_request("get", resource)
Exemple #2
0
    def create(self, name, **kwargs):
        """
        Create a dataset, including the field types. Optionally, specify args such as:
            description : description of the dataset
            columns : list of columns (see docs/tests for list structure)
            category : must exist in /admin/metadata
            tags : list of tag strings
            row_identifier : field name of primary key
            new_backend : whether to create the dataset in the new backend

        WARNING: This api endpoint might be deprecated.
        """
        new_backend = kwargs.pop("new_backend", False)
        resource = utils.format_old_api_request(content_type="json")
        if new_backend:
            resource += "?nbe=true"

        payload = {"name": name}

        if "row_identifier" in kwargs:
            payload["metadata"] = {
                "rowIdentifier": kwargs.pop("row_identifier", None)
            }

        payload.update(kwargs)
        payload = utils.clear_empty_values(payload)

        return self._perform_update("post", resource, payload)
Exemple #3
0
    def publish(self, dataset_identifier, content_type="json"):
        """
        The create() method creates a dataset in a "working copy" state.
        This method publishes it.
        """
        base = utils.format_old_api_request(dataid=dataset_identifier)
        resource = "{0}/publication.{1}".format(base, content_type)

        return self._perform_request("post", resource)
Exemple #4
0
    def update_metadata(self,
                        dataset_identifier,
                        update_fields,
                        content_type="json"):
        """
        Update the metadata for a particular dataset.
            update_fields is a dictionary containing [metadata key:new value] pairs.

        This method performs a full replace for the key:value pairs listed in `update_fields`, and
        returns all of the metadata with the updates applied.
        """
        resource = utils.format_old_api_request(dataid=dataset_identifier,
                                                content_type=content_type)
        return self._perform_update("put", resource, update_fields)
Exemple #5
0
    def delete(self, dataset_identifier, row_id=None, content_type="json"):
        """
        Delete the entire dataset, e.g.
            client.delete("nimj-3ivp")
        or a single row, e.g.
            client.delete("nimj-3ivp", row_id=4)
        """
        if row_id:
            resource = utils.format_new_api_request(dataid=dataset_identifier,
                                                    row_id=row_id,
                                                    content_type=content_type)
        else:
            resource = utils.format_old_api_request(dataid=dataset_identifier,
                                                    content_type=content_type)

        return self._perform_request("delete", resource)
Exemple #6
0
    def set_permission(self,
                       dataset_identifier,
                       permission="private",
                       content_type="json"):
        """
        Set a dataset's permissions to private or public
        Options are private, public

        """
        resource = utils.format_old_api_request(dataid=dataset_identifier,
                                                content_type=content_type)

        params = {
            "method": "setPermission",
            "value": "public.read" if permission == "public" else permission,
        }

        return self._perform_request("put", resource, params=params)
Exemple #7
0
    def replace_non_data_file(self, dataset_identifier, params, file_data):
        """
        Same as create_non_data_file, but replaces a file that already exists in a
        file-based dataset.

        WARNING: a table-based dataset cannot be replaced by a file-based dataset.
                 Use create_non_data_file in order to replace.
        """
        resource = utils.format_old_api_request(dataid=dataset_identifier,
                                                content_type="txt")

        if not params.get("method", None):
            params["method"] = "replaceBlob"

        params["id"] = dataset_identifier

        return self._perform_request("post",
                                     resource,
                                     params=params,
                                     files=file_data)
Exemple #8
0
    def download_attachments(self,
                             dataset_identifier,
                             content_type="json",
                             download_dir="~/sodapy_downloads"):
        """
        Download all of the attachments associated with a dataset. Return the paths of downloaded
        files.
        """
        metadata = self.get_metadata(dataset_identifier,
                                     content_type=content_type)
        files = []
        attachments = metadata["metadata"].get("attachments")
        if not attachments:
            logging.info("No attachments were found or downloaded.")
            return files

        download_dir = os.path.join(os.path.expanduser(download_dir),
                                    dataset_identifier)
        if not os.path.exists(download_dir):
            os.makedirs(download_dir)

        for attachment in attachments:
            file_path = os.path.join(download_dir, attachment["filename"])
            has_assetid = attachment.get("assetId", False)
            if has_assetid:
                base = utils.format_old_api_request(dataid=dataset_identifier)
                assetid = attachment["assetId"]
                resource = "{0}/files/{1}?download=true&filename={2}".format(
                    base, assetid, attachment["filename"])
            else:
                base = "/api/assets"
                assetid = attachment["blobId"]
                resource = "{0}/{1}?download=true".format(base, assetid)

            uri = "{0}{1}{2}".format(self.uri_prefix, self.domain, resource)
            utils.download_file(uri, file_path)
            files.append(file_path)

        logging.info("The following files were downloaded:\n\t{0}".format(
            "\n\t".join(files)))
        return files
Exemple #9
0
def test_format_old_api_request(dataid, content_type, path):
    assert (
        utils.format_old_api_request(dataid=dataid, content_type=content_type) == path
    )
Exemple #10
0
def test_format_old_api_request_exception():
    with pytest.raises(Exception):
        utils.format_old_api_request()