squid-js/src/brizo/Brizo.ts

174 lines
4.9 KiB
TypeScript
Raw Normal View History

2019-06-20 00:20:09 +02:00
import * as fs from 'fs'
2019-06-20 00:20:09 +02:00
import { File } from '../ddo/MetaData'
import Account from '../ocean/Account'
import { noZeroX } from '../utils'
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
import save = require('save-file')
2018-11-09 10:43:29 +01:00
2019-06-20 00:20:09 +02:00
const apiPath = '/api/v1/brizo/services'
2018-11-26 15:24:59 +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-06-20 00:20:09 +02:00
export class Brizo extends Instantiable {
private get url() {
return this.config.brizoUri
}
2018-11-09 10:43:29 +01:00
constructor(config: InstantiableConfig) {
super()
this.setInstanceConfig(config)
2018-11-09 10:43:29 +01:00
}
2019-06-14 12:07:35 +02:00
public async getVersionInfo() {
2019-06-14 00:34:53 +02:00
return await (await this.ocean.utils.fetch.get(this.url)).json()
}
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
}
public getConsumeEndpoint() {
return `${this.url}${apiPath}/consume`
2018-11-09 10:43:29 +01:00
}
public getEncryptEndpoint() {
return `${this.url}${apiPath}/publish`
}
2019-06-20 00:20:09 +02: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-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-06-20 00:20:09 +02:00
consumerAddress: string
2019-02-21 17:58:54 +01:00
): Promise<any> {
2018-11-29 08:45:10 +01:00
const args = {
did,
serviceAgreementId,
serviceDefinitionId,
signature,
2019-06-20 00:20:09 +02:00
consumerAddress
2018-11-29 08:45:10 +01:00
}
try {
2019-06-20 00:20:09 +02:00
return await this.ocean.utils.fetch.post(
this.getPurchaseEndpoint(),
decodeURI(JSON.stringify(args))
)
2019-01-24 10:58:05 +01:00
} catch (e) {
this.logger.error(e)
2019-06-20 00:20:09 +02: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,
2019-06-20 00:20:09 +02:00
index: number = -1
2019-02-21 17:58:54 +01:00
): Promise<string> {
2019-06-20 00:20:09 +02:00
const signature =
(await account.getToken()) ||
(await this.ocean.utils.signature.signText(
noZeroX(agreementId),
account.getId()
))
2019-02-21 17:58:54 +01:00
const filesPromises = files
.filter(({}, i) => index === -1 || i === index)
2019-06-20 00:20:09 +02:00
.map(async ({ index: i }) => {
2019-02-21 17:58:54 +01:00
let consumeUrl = serviceEndpoint
2019-04-05 12:20:42 +02:00
consumeUrl += `?index=${i}`
2019-04-15 17:45:06 +02:00
consumeUrl += `&serviceAgreementId=${noZeroX(agreementId)}`
2019-02-21 17:58:54 +01:00
consumeUrl += `&consumerAddress=${account.getId()}`
consumeUrl += `&signature=${signature}`
2019-02-21 17:58:54 +01:00
try {
2019-06-20 00:20:09 +02:00
await this.downloadFile(consumeUrl, destination)
2019-02-21 17:58:54 +01:00
} catch (e) {
2019-06-20 00:20:09 +02:00
this.logger.error('Error consuming assets')
this.logger.error(e)
throw e
2019-02-21 17:58:54 +01:00
}
})
await Promise.all(filesPromises)
return destination
}
public async encrypt(
did: string,
signature: string,
document: any,
2019-06-20 00:20:09 +02:00
publisher: string
): Promise<string> {
const args = {
documentId: did,
signature,
document: JSON.stringify(document),
2019-06-20 00:20:09 +02:00
publisherAddress: publisher
}
try {
2019-06-20 00:20:09 +02:00
const response = await this.ocean.utils.fetch.post(
this.getEncryptEndpoint(),
decodeURI(JSON.stringify(args))
)
if (!response.ok) {
2019-06-20 00:20:09 +02:00
throw new Error('HTTP request failed')
}
return await response.text()
} catch (e) {
this.logger.error(e)
2019-06-20 00:20:09 +02:00
throw new Error('HTTP request failed')
}
}
2019-04-01 17:26:42 +02:00
2019-06-20 00:20:09 +02:00
private async downloadFile(
url: string,
destination?: string
): Promise<string> {
const response = await this.ocean.utils.fetch.get(url)
if (!response.ok) {
2019-06-20 00:20:09 +02:00
throw new Error('Response error.')
}
let filename
try {
2019-06-20 00:20:09 +02:00
filename = response.headers
.get('content-disposition')
.match(/attachment;filename=(.+)/)[1]
} catch {
2019-06-20 00:20:09 +02:00
throw new Error('Response is not containing file name.')
}
if (destination) {
2019-04-08 16:09:35 +02:00
await new Promise(async (resolve, reject) => {
2019-06-20 00:20:09 +02:00
fs.mkdirSync(destination, { recursive: true })
const fileStream = fs.createWriteStream(
`${destination}${filename}`
)
2019-04-09 13:16:39 +02:00
response.body.pipe(fileStream)
2019-06-20 00:20:09 +02:00
response.body.on('error', reject)
fileStream.on('finish', resolve)
2019-04-09 13:16:39 +02:00
})
2019-04-08 16:09:35 +02:00
return destination
} else {
save(await response.arrayBuffer(), filename)
2019-04-08 16:09:35 +02:00
}
2019-04-01 17:26:42 +02:00
}
2018-11-09 10:43:29 +01:00
}