Beispiel #1
0
 def download(self, wallet: Wallet, tx_id: str, destination_folder: str):
     url = self.blob()
     download_url = (f"{url}?"
                     f"consumerAddress={wallet.address}"
                     f"&dataToken={self.address}"
                     f"&transferTxId={tx_id}")
     response = get_requests_session().get(download_url, stream=True)
     file_name = f"file-{self.address}"
     DataServiceProvider.write_file(response, destination_folder, file_name)
     return os.path.join(destination_folder, file_name)
    def download_service(
        did,
        service_endpoint,
        wallet,
        files,
        destination_folder,
        service_id,
        token_address,
        order_tx_id,
        index=None,
        userdata=None,
    ):

        indexes = range(len(files))
        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]

        req = PreparedRequest()
        params = {
            "documentId": did,
            "serviceId": service_id,
            "serviceType": ServiceTypes.ASSET_ACCESS,
            "dataToken": token_address,
            "transferTxId": order_tx_id,
            "consumerAddress": wallet.address,
        }

        if userdata:
            userdata = json.dumps(userdata)
            params["userdata"] = userdata

        req.prepare_url(service_endpoint, params)
        base_url = req.url

        provider_uri = DataProviderMock.build_download_endpoint(
            service_endpoint)[1]
        for i in indexes:
            signature = DataServiceProvider.sign_message(
                wallet, did, provider_uri=provider_uri)
            download_url = base_url + f"&signature={signature}&fileIndex={i}"
            logger.info(
                f"invoke consume endpoint with this url: {download_url}")
            http_client = get_requests_session()
            response = http_client.get(download_url, stream=True)
            file_name = DataServiceProvider._get_file_name(response)
            DataServiceProvider.write_file(response, destination_folder,
                                           file_name or f"file-{i}")
Beispiel #3
0
    def download_service(
        did,
        service_endpoint,
        wallet,
        files,
        destination_folder,
        service_id,
        token_address,
        order_tx_id,
        index=None,
    ):

        indexes = range(len(files))
        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]

        base_url = (
            f"{service_endpoint}"
            f"?documentId={did}"
            f"&serviceId={service_id}"
            f"&serviceType={ServiceTypes.ASSET_ACCESS}"
            f"&dataToken={token_address}"
            f"&transferTxId={order_tx_id}"
            f"&consumerAddress={wallet.address}"
        )
        provider_uri = DataProviderMock.build_download_endpoint(service_endpoint)[1]
        for i in indexes:
            signature = DataServiceProvider.sign_message(
                wallet, did, provider_uri=provider_uri
            )
            download_url = base_url + f"&signature={signature}&fileIndex={i}"
            logger.info(f"invoke consume endpoint with this url: {download_url}")
            http_client = get_requests_session()
            response = http_client.get(download_url, stream=True)
            file_name = DataServiceProvider._get_file_name(response)
            DataServiceProvider.write_file(
                response, destination_folder, file_name or f"file-{i}"
            )