def download_asset_files(service_index: int,
                         asset: Asset,
                         consumer_wallet: Wallet,
                         destination: str,
                         token_address: str,
                         order_tx_id: str,
                         data_provider: DataServiceProvider,
                         index: [int, None] = None):
    """
    Download asset data files or result files from a compute job.

    :param service_index: identifier of the service inside the asset DDO, str
    :param asset: Asset instance
    :param consumer_wallet: Wallet instance of the consumer
    :param destination: Path, str
    :param token_address: hex str the address of the DataToken smart contract
    :param order_tx_id: hex str the transaction hash of the startOrder tx
    :param data_provider: DataServiceProvider instance
    :param index: Index of the document that is going to be downloaded, int
    :return: Asset folder path, str
    """
    _files = asset.metadata['main']['files']
    sa = ServiceAgreement.from_ddo(ServiceTypes.ASSET_ACCESS, asset)
    service_endpoint = sa.service_endpoint
    if not service_endpoint:
        logger.error(
            'Consume asset failed, service definition is missing the "serviceEndpoint".'
        )
        raise AssertionError(
            'Consume asset failed, service definition is missing the "serviceEndpoint".'
        )

    service_endpoint = data_provider.build_download_endpoint(service_endpoint)
    if not os.path.isabs(destination):
        destination = os.path.abspath(destination)
    if not os.path.exists(destination):
        os.mkdir(destination)

    asset_folder = os.path.join(destination,
                                f'datafile.{asset.asset_id}.{service_index}')
    if not os.path.exists(asset_folder):
        os.mkdir(asset_folder)

    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.')
        assert index < len(_files), logger.error(
            'index can not be bigger than the number of files')
        indexes = [index]
    else:
        indexes = range(len(_files))

    for i in indexes:
        data_provider.download_service(asset.did, service_endpoint,
                                       consumer_wallet, _files, asset_folder,
                                       service_index, token_address,
                                       order_tx_id, i)
    return asset_folder
Beispiel #2
0
def test_build_specific_endpoints():
    """Tests that a specific list of agreed endpoints is supported on the DataServiceProvider."""
    config = ConfigProvider.get_config()
    endpoints = TEST_SERVICE_ENDPOINTS

    def get_service_endpoints(_provider_uri=None):
        return TEST_SERVICE_ENDPOINTS.copy()

    original_func = DataSP.get_service_endpoints
    DataSP.get_service_endpoints = get_service_endpoints

    base_uri = DataSP.get_root_uri(config.provider_url)
    assert DataSP.build_download_endpoint()[1] == urljoin(
        base_uri, endpoints["download"][1])
    assert DataSP.build_initialize_endpoint()[1] == urljoin(
        base_uri, endpoints["initialize"][1])
    assert DataSP.build_encrypt_endpoint()[1] == urljoin(
        base_uri, endpoints["encrypt"][1])
    assert DataSP.build_fileinfo()[1] == urljoin(base_uri,
                                                 endpoints["fileinfo"][1])
    assert DataSP.build_compute_endpoint()[1] == urljoin(
        base_uri, endpoints["computeStatus"][1])
    assert DataSP.build_compute_endpoint()[1] == urljoin(
        base_uri, endpoints["computeStart"][1])
    assert DataSP.build_compute_endpoint()[1] == urljoin(
        base_uri, endpoints["computeStop"][1])
    assert DataSP.build_compute_endpoint()[1] == urljoin(
        base_uri, endpoints["computeDelete"][1])

    DataSP.get_service_endpoints = original_func
Beispiel #3
0
def get_access_service_descriptor(
    ocean_instance, address, date_created, provider_uri=None, timeout=3600
):
    if not provider_uri:
        provider_uri = DataServiceProvider.get_url(ocean_instance.config)

    return ServiceDescriptor.access_service_descriptor(
        ocean_instance.assets.build_access_service(date_created, 1.0, address, timeout),
        DataServiceProvider.build_download_endpoint(provider_uri)[1],
    )