コード例 #1
0
    def download(
        self,
        did: str,
        service_index: int,
        consumer_wallet: Wallet,
        order_tx_id: str,
        destination: Union[str, Path],
        index: Optional[int] = None,
        userdata: Optional[dict] = None,
    ) -> str:
        """
        Consume the asset data.

        Using the service endpoint defined in the ddo's service pointed to by service_definition_id.
        Consumer's permissions is checked implicitly by the secret-store during decryption
        of the contentUrls.
        The service endpoint is expected to also verify the consumer's permissions to consume this
        asset.
        This method downloads and saves the asset datafiles to disk.

        :param did: DID, str
        :param service_index: identifier of the service inside the asset DDO, str
        :param consumer_wallet: Wallet instance of the consumer
        :param order_tx_id: hex str id of the token transfer transaction
        :param destination: str path
        :param index: Index of the document that is going to be downloaded, int
        :return: str path to saved files
        """
        asset = self.resolve(did)
        if index is not None:
            assert isinstance(index, int), logger.error("index has to be an integer.")
            assert index >= 0, logger.error("index has to be 0 or a positive integer.")

        service = asset.get_service_by_index(service_index)
        assert (
            service and service.type == ServiceTypes.ASSET_ACCESS
        ), f"Service with index {service_index} and type {ServiceTypes.ASSET_ACCESS} is not found."

        consumable_result = asset.is_consumable(
            {"type": "address", "value": consumer_wallet.address},
            provider_uri=service.service_endpoint,
        )
        if consumable_result != ConsumableCodes.OK:
            raise AssetNotConsumable(consumable_result)

        return download_asset_files(
            service_index,
            asset,
            consumer_wallet,
            destination,
            asset.data_token_address,
            order_tx_id,
            self._data_provider,
            index,
            userdata,
        )
コード例 #2
0
def test_ocean_assets_download_failure():
    """Tests that downloading from an empty service raises an AssertionError."""
    publisher = get_publisher_wallet()
    data_provider = DataServiceProvider

    ddo = get_sample_ddo()
    sa = ddo.get_service(ServiceTypes.ASSET_ACCESS)
    sa.service_endpoint = None
    ddo.services[1] = sa

    with pytest.raises(AssertionError):
        download_asset_files(
            sa.index,
            ddo,
            publisher,
            "test_destination",
            "",
            "test_order_tx_id",
            data_provider,
        )
コード例 #3
0
def ocean_assets_download_destination_file_helper(tmpdir):
    """Tests downloading to an existing directory."""
    publisher = get_publisher_wallet()
    data_provider = DataServiceProvider

    ddo = get_sample_ddo()
    sa = ddo.get_service(ServiceTypes.ASSET_ACCESS)

    written_path = download_asset_files(
        sa.index, ddo, publisher, tmpdir, "0x1", "test_order_tx_id", data_provider
    )
    assert os.path.exists(written_path)
コード例 #4
0
def test_ocean_assets_download_failure(publisher_ocean_instance, metadata):
    """Tests that downloading from an empty service raises an AssertionError."""
    publisher = get_publisher_wallet()
    metadata_copy = metadata.copy()
    data_provider = DataServiceProvider

    ddo = publisher_ocean_instance.assets.create(metadata_copy, publisher)
    wait_for_ddo(publisher_ocean_instance, ddo.did)
    sa = ServiceAgreement.from_ddo(ServiceTypes.ASSET_ACCESS, ddo)
    sa.__dict__["_service_endpoint"] = None
    ddo.__dict__["_services"][1] = sa

    with pytest.raises(AssertionError):
        download_asset_files(
            sa.index,
            ddo,
            publisher,
            "test_destination",
            ddo.data_token_address,
            "test_order_tx_id",
            data_provider,
        )
コード例 #5
0
def ocean_assets_download_destination_file_helper(publisher_ocean_instance,
                                                  metadata, tmpdir):
    """Tests downloading to an existing directory."""
    publisher = get_publisher_wallet()
    metadata_copy = metadata.copy()
    data_provider = DataServiceProvider

    ddo = publisher_ocean_instance.assets.create(metadata_copy, publisher)
    wait_for_ddo(publisher_ocean_instance, ddo.did)
    sa = ServiceAgreement.from_ddo(ServiceTypes.ASSET_ACCESS, ddo)

    written_path = download_asset_files(
        sa.index,
        ddo,
        publisher,
        tmpdir,
        ddo.data_token_address,
        "test_order_tx_id",
        data_provider,
    )
    assert os.path.exists(written_path)
コード例 #6
0
ファイル: ocean_assets.py プロジェクト: dmtree2/ocean.py
    def download(self,
                 did: str,
                 service_index: int,
                 consumer_wallet: Wallet,
                 order_tx_id: str,
                 destination: str,
                 index: [int, None] = None) -> str:
        """
        Consume the asset data.

        Using the service endpoint defined in the ddo's service pointed to by service_definition_id.
        Consumer's permissions is checked implicitly by the secret-store during decryption
        of the contentUrls.
        The service endpoint is expected to also verify the consumer's permissions to consume this
        asset.
        This method downloads and saves the asset datafiles to disk.

        :param did: DID, str
        :param service_index: identifier of the service inside the asset DDO, str
        :param consumer_wallet: Wallet instance of the consumer
        :param order_tx_id: hex str id of the token transfer transaction
        :param destination: str path
        :param index: Index of the document that is going to be downloaded, int
        :return: str path to saved files
        """
        asset = self.resolve(did)
        if index is not None:
            assert isinstance(index,
                              int), logger.error('index has to be an integer.')
            assert index >= 0, logger.error(
                'index has to be 0 or a positive integer.')

        service = asset.get_service_by_index(service_index)
        assert service and service.type == ServiceTypes.ASSET_ACCESS, \
            f'Service with index {service_index} and type {ServiceTypes.ASSET_ACCESS} is not found.'

        return download_asset_files(service_index, asset, consumer_wallet,
                                    destination, asset.data_token_address,
                                    order_tx_id, self._data_provider, index)
コード例 #7
0
def test_ocean_assets_download_indexes():
    """Tests different values of indexes that raise AssertionError."""
    publisher = get_publisher_wallet()
    data_provider = DataServiceProvider

    ddo = get_sample_ddo()
    sa = ddo.get_service(ServiceTypes.ASSET_ACCESS)

    index = range(3)
    with pytest.raises(TypeError):
        download_asset_files(
            sa.index,
            ddo,
            publisher,
            "test_destination",
            "",
            "test_order_tx_id",
            data_provider,
            index,
        )

    index = -1
    with pytest.raises(AssertionError):
        download_asset_files(
            sa.index,
            ddo,
            publisher,
            "test_destination",
            "",
            "test_order_tx_id",
            data_provider,
            index,
        )
    index = 4
    with pytest.raises(AssertionError):
        download_asset_files(
            sa.index,
            ddo,
            publisher,
            "test_destination",
            "",
            "test_order_tx_id",
            data_provider,
            index,
        )
コード例 #8
0
def test_ocean_assets_download_indexes(publisher_ocean_instance, metadata):
    """Tests different values of indexes that raise AssertionError."""
    publisher = get_publisher_wallet()
    metadata_copy = metadata.copy()
    data_provider = DataServiceProvider

    ddo = publisher_ocean_instance.assets.create(metadata_copy, publisher)
    wait_for_ddo(publisher_ocean_instance, ddo.did)
    sa = ServiceAgreement.from_ddo(ServiceTypes.ASSET_ACCESS, ddo)

    config = Config(os.getenv(ENV_CONFIG_FILE))

    index = range(3)
    if config["util"].getboolean("typecheck"):
        with pytest.raises(TypeError):
            download_asset_files(
                sa.index,
                ddo,
                publisher,
                "test_destination",
                ddo.data_token_address,
                "test_order_tx_id",
                data_provider,
                index,
            )
    else:
        with pytest.raises(AssertionError):
            download_asset_files(
                sa.index,
                ddo,
                publisher,
                "test_destination",
                ddo.data_token_address,
                "test_order_tx_id",
                data_provider,
                index,
            )

    index = -1
    with pytest.raises(AssertionError):
        download_asset_files(
            sa.index,
            ddo,
            publisher,
            "test_destination",
            ddo.data_token_address,
            "test_order_tx_id",
            data_provider,
            index,
        )
    index = 4
    with pytest.raises(AssertionError):
        download_asset_files(
            sa.index,
            ddo,
            publisher,
            "test_destination",
            ddo.data_token_address,
            "test_order_tx_id",
            data_provider,
            index,
        )