1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

add download method for provider

This commit is contained in:
Ahmed Ali 2020-06-25 13:29:32 +02:00
parent 799c2e98f2
commit dd64062f8c

View File

@ -1,7 +1,7 @@
import Account from '../ocean/Account'
import { noZeroX } from '../utils'
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
import { DDO } from '../ddo/DDO'
import { File } from '../ddo/interfaces/File'
const apiPath = '/api/v1/services/'
@ -102,6 +102,43 @@ export class Provider extends Instantiable {
}
}
public async download(
did: string,
txId: string,
tokenAddress: string,
serviceType: string,
serviceIndex: string,
destination: string,
account: Account,
files: File[],
index: number = -1
): Promise<any> {
const signature = await this.createSignature(account, did)
const filesPromises = files
.filter((_, i) => index === -1 || i === index)
.map(async ({ index: i }) => {
let consumeUrl = this.getDownloadEndpoint()
consumeUrl += `?index=${i}`
consumeUrl += `&documentId=${did}`
consumeUrl += `&serviceId=${serviceIndex}`
consumeUrl += `&serviceType=${serviceType}`
consumeUrl += `tokenAddress=${tokenAddress}`
consumeUrl += `&transferTxId=${txId}`
consumeUrl += `&consumerAddress=${account.getId()}`
consumeUrl += `&signature=${signature}`
try {
await this.ocean.utils.fetch.downloadFile(consumeUrl, destination, i)
} catch (e) {
this.logger.error('Error consuming assets')
this.logger.error(e)
throw e
}
})
await Promise.all(filesPromises)
return destination
}
public async getVersionInfo() {
return (await this.ocean.utils.fetch.get(this.url)).json()
}