2019-02-21 18:14:07 +01:00
|
|
|
import save = require("save-file")
|
2019-02-21 17:58:54 +01:00
|
|
|
import { File } from "../ddo/MetaData"
|
|
|
|
import Account from "../ocean/Account"
|
2018-11-19 12:16:11 +01:00
|
|
|
import WebServiceConnectorProvider from "../utils/WebServiceConnectorProvider"
|
2019-03-21 02:56:58 +01:00
|
|
|
import { Instantiable, InstantiableConfig } from "../Instantiable.abstract"
|
2018-11-09 10:43:29 +01:00
|
|
|
|
2018-11-26 15:24:59 +01:00
|
|
|
const apiPath = "/api/v1/brizo/services"
|
|
|
|
|
2019-01-09 16:15:32 +01:00
|
|
|
/**
|
|
|
|
* Provides a interface with Brizo.
|
|
|
|
* Brizo is the technical component executed by the Publishers allowing to them to provide extended data services.
|
|
|
|
*/
|
2019-03-21 02:56:58 +01:00
|
|
|
export class Brizo extends Instantiable {
|
|
|
|
private get url() {
|
|
|
|
return this.config.brizoUri
|
|
|
|
}
|
2018-11-09 10:43:29 +01:00
|
|
|
|
2019-03-21 02:56:58 +01:00
|
|
|
constructor(config: InstantiableConfig) {
|
|
|
|
super()
|
|
|
|
this.setInstanceConfig(config)
|
2018-11-09 10:43:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public getPurchaseEndpoint() {
|
2018-12-12 20:52:11 +01:00
|
|
|
return `${this.url}${apiPath}/access/initialize`
|
2018-11-09 10:43:29 +01:00
|
|
|
}
|
|
|
|
|
2018-12-08 00:46:18 +01:00
|
|
|
public getConsumeEndpoint() {
|
|
|
|
return `${this.url}${apiPath}/consume`
|
2018-11-09 10:43:29 +01:00
|
|
|
}
|
2018-11-09 11:16:22 +01:00
|
|
|
|
|
|
|
public getComputeEndpoint(pubKey: string, serviceId: string, algo: string, container: string) {
|
|
|
|
// tslint:disable-next-line
|
2018-12-12 20:52:11 +01:00
|
|
|
return `${this.url}${apiPath}/compute`
|
2018-11-09 11:16:22 +01:00
|
|
|
}
|
2018-11-19 12:16:11 +01:00
|
|
|
|
2018-11-21 14:59:22 +01:00
|
|
|
public async initializeServiceAgreement(
|
|
|
|
did: string,
|
|
|
|
serviceAgreementId: string,
|
|
|
|
serviceDefinitionId: string,
|
|
|
|
signature: string,
|
2019-02-21 17:58:54 +01:00
|
|
|
consumerAddress: string,
|
|
|
|
): Promise<any> {
|
2018-11-26 15:24:59 +01:00
|
|
|
|
2018-11-29 08:45:10 +01:00
|
|
|
const args = {
|
|
|
|
did,
|
|
|
|
serviceAgreementId,
|
|
|
|
serviceDefinitionId,
|
|
|
|
signature,
|
|
|
|
consumerAddress,
|
|
|
|
}
|
|
|
|
|
2019-01-23 12:29:13 +01:00
|
|
|
try {
|
|
|
|
return await WebServiceConnectorProvider
|
|
|
|
.getConnector()
|
|
|
|
.post(
|
|
|
|
this.getPurchaseEndpoint(),
|
|
|
|
decodeURI(JSON.stringify(args)),
|
|
|
|
)
|
2019-01-24 10:58:05 +01:00
|
|
|
} catch (e) {
|
2019-03-21 02:56:58 +01:00
|
|
|
this.logger.error(e)
|
2019-01-23 12:29:13 +01:00
|
|
|
throw new Error("HTTP request failed")
|
|
|
|
}
|
2018-11-19 12:16:11 +01:00
|
|
|
}
|
2019-02-21 17:58:54 +01:00
|
|
|
|
|
|
|
public async consumeService(
|
|
|
|
agreementId: string,
|
|
|
|
serviceEndpoint: string,
|
|
|
|
account: Account,
|
|
|
|
files: File[],
|
|
|
|
destination: string,
|
|
|
|
): Promise<string> {
|
|
|
|
const filesPromises = files
|
|
|
|
.map(async ({url}, i) => {
|
|
|
|
let consumeUrl = serviceEndpoint
|
|
|
|
consumeUrl += `?url=${url}`
|
|
|
|
consumeUrl += `&serviceAgreementId=${agreementId}`
|
|
|
|
consumeUrl += `&consumerAddress=${account.getId()}`
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.downloadFile(
|
|
|
|
consumeUrl,
|
2019-02-21 18:14:07 +01:00
|
|
|
url.split("/").pop() || `file-${i}`,
|
2019-02-21 17:58:54 +01:00
|
|
|
destination,
|
|
|
|
)
|
|
|
|
} catch (e) {
|
2019-03-21 02:56:58 +01:00
|
|
|
this.logger.error("Error consuming assets")
|
|
|
|
this.logger.error(e)
|
2019-02-21 17:58:54 +01:00
|
|
|
throw new Error("Error consuming assets")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
await Promise.all(filesPromises)
|
|
|
|
return destination
|
|
|
|
}
|
|
|
|
|
|
|
|
private async downloadFile(url: string, filename: string, destination?: string): Promise<string> {
|
|
|
|
const path = `${destination}${filename}`
|
|
|
|
const response = await WebServiceConnectorProvider
|
|
|
|
.getConnector()
|
|
|
|
.get(url)
|
2019-03-26 16:13:24 +01:00
|
|
|
await save(await response.blob(), path)
|
2019-02-21 17:58:54 +01:00
|
|
|
return path
|
|
|
|
}
|
2018-11-09 10:43:29 +01:00
|
|
|
}
|