mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
refactor
This commit is contained in:
parent
59dc288893
commit
100e5c5767
@ -113,8 +113,8 @@ export class Assets extends Instantiable {
|
|||||||
let provider: Provider
|
let provider: Provider
|
||||||
|
|
||||||
if (providerUri) {
|
if (providerUri) {
|
||||||
provider = new Provider(this.instanceConfig)
|
provider = await Provider.getInstance(this.instanceConfig)
|
||||||
provider.setBaseUrl(providerUri)
|
await provider.setBaseUrl(providerUri)
|
||||||
} else provider = this.ocean.provider
|
} else provider = this.ocean.provider
|
||||||
const encryptedFiles = await provider.encrypt(
|
const encryptedFiles = await provider.encrypt(
|
||||||
did.getDid(),
|
did.getDid(),
|
||||||
@ -406,8 +406,8 @@ export class Assets extends Instantiable {
|
|||||||
serviceIndex = -1,
|
serviceIndex = -1,
|
||||||
serviceEndpoint: string
|
serviceEndpoint: string
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const provider = new Provider(this.instanceConfig)
|
const provider = await Provider.getInstance(this.instanceConfig)
|
||||||
provider.setBaseUrl(serviceEndpoint)
|
await provider.setBaseUrl(serviceEndpoint)
|
||||||
const res = await provider.initialize(did, serviceIndex, serviceType, consumerAddress)
|
const res = await provider.initialize(did, serviceIndex, serviceType, consumerAddress)
|
||||||
if (res === null) return null
|
if (res === null) return null
|
||||||
const providerData = JSON.parse(res)
|
const providerData = JSON.parse(res)
|
||||||
@ -514,8 +514,8 @@ export class Assets extends Instantiable {
|
|||||||
destination = destination
|
destination = destination
|
||||||
? `${destination}/datafile.${ddo.shortId()}.${service.index}/`
|
? `${destination}/datafile.${ddo.shortId()}.${service.index}/`
|
||||||
: undefined
|
: undefined
|
||||||
const provider = new Provider(this.instanceConfig)
|
const provider = await Provider.getInstance(this.instanceConfig)
|
||||||
provider.setBaseUrl(serviceEndpoint)
|
await provider.setBaseUrl(serviceEndpoint)
|
||||||
await provider.download(
|
await provider.download(
|
||||||
did,
|
did,
|
||||||
txId,
|
txId,
|
||||||
|
@ -92,8 +92,8 @@ export class Compute extends Instantiable {
|
|||||||
const service = ddo.findServiceByType('compute')
|
const service = ddo.findServiceByType('compute')
|
||||||
const { serviceEndpoint } = service
|
const { serviceEndpoint } = service
|
||||||
if (did && txId) {
|
if (did && txId) {
|
||||||
const provider = new Provider(this.instanceConfig)
|
const provider = await Provider.getInstance(this.instanceConfig)
|
||||||
provider.setBaseUrl(serviceEndpoint)
|
await provider.setBaseUrl(serviceEndpoint)
|
||||||
const computeJobsList = await provider.compute(
|
const computeJobsList = await provider.compute(
|
||||||
'post',
|
'post',
|
||||||
did,
|
did,
|
||||||
@ -128,8 +128,8 @@ export class Compute extends Instantiable {
|
|||||||
const ddo = await this.ocean.assets.resolve(did)
|
const ddo = await this.ocean.assets.resolve(did)
|
||||||
const service = ddo.findServiceByType('compute')
|
const service = ddo.findServiceByType('compute')
|
||||||
const { serviceEndpoint } = service
|
const { serviceEndpoint } = service
|
||||||
const provider = new Provider(this.instanceConfig)
|
const provider = await Provider.getInstance(this.instanceConfig)
|
||||||
provider.setBaseUrl(serviceEndpoint)
|
await provider.setBaseUrl(serviceEndpoint)
|
||||||
const computeJobsList = await provider.compute(
|
const computeJobsList = await provider.compute(
|
||||||
'put',
|
'put',
|
||||||
did,
|
did,
|
||||||
@ -157,8 +157,8 @@ export class Compute extends Instantiable {
|
|||||||
const ddo = await this.ocean.assets.resolve(did)
|
const ddo = await this.ocean.assets.resolve(did)
|
||||||
const service = ddo.findServiceByType('compute')
|
const service = ddo.findServiceByType('compute')
|
||||||
const { serviceEndpoint } = service
|
const { serviceEndpoint } = service
|
||||||
const provider = new Provider(this.instanceConfig)
|
const provider = await Provider.getInstance(this.instanceConfig)
|
||||||
provider.setBaseUrl(serviceEndpoint)
|
await provider.setBaseUrl(serviceEndpoint)
|
||||||
const computeJobsList = await provider.compute(
|
const computeJobsList = await provider.compute(
|
||||||
'delete',
|
'delete',
|
||||||
did,
|
did,
|
||||||
@ -193,8 +193,8 @@ export class Compute extends Instantiable {
|
|||||||
const ddo = await this.ocean.assets.resolve(did)
|
const ddo = await this.ocean.assets.resolve(did)
|
||||||
const service = ddo.findServiceByType('compute')
|
const service = ddo.findServiceByType('compute')
|
||||||
const { serviceEndpoint } = service
|
const { serviceEndpoint } = service
|
||||||
provider = new Provider(this.instanceConfig)
|
provider = await Provider.getInstance(this.instanceConfig)
|
||||||
provider.setBaseUrl(serviceEndpoint)
|
await provider.setBaseUrl(serviceEndpoint)
|
||||||
} else {
|
} else {
|
||||||
provider = this.ocean.provider
|
provider = this.ocean.provider
|
||||||
}
|
}
|
||||||
@ -233,8 +233,8 @@ export class Compute extends Instantiable {
|
|||||||
const ddo = await this.ocean.assets.resolve(did)
|
const ddo = await this.ocean.assets.resolve(did)
|
||||||
const service = ddo.findServiceByType('compute')
|
const service = ddo.findServiceByType('compute')
|
||||||
const { serviceEndpoint } = service
|
const { serviceEndpoint } = service
|
||||||
const provider = new Provider(this.instanceConfig)
|
const provider = await Provider.getInstance(this.instanceConfig)
|
||||||
provider.setBaseUrl(serviceEndpoint)
|
await provider.setBaseUrl(serviceEndpoint)
|
||||||
const computeJobsList = await provider.compute(
|
const computeJobsList = await provider.compute(
|
||||||
'get',
|
'get',
|
||||||
did,
|
did,
|
||||||
|
@ -36,7 +36,7 @@ export class Ocean extends Instantiable {
|
|||||||
|
|
||||||
instance.utils = await OceanUtils.getInstance(instanceConfig)
|
instance.utils = await OceanUtils.getInstance(instanceConfig)
|
||||||
|
|
||||||
instance.provider = new Provider(instanceConfig)
|
instance.provider = await Provider.getInstance(instanceConfig)
|
||||||
instance.metadatacache = new MetadataCache(
|
instance.metadatacache = new MetadataCache(
|
||||||
instanceConfig.config.metadataCacheUri,
|
instanceConfig.config.metadataCacheUri,
|
||||||
instanceConfig.logger
|
instanceConfig.logger
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
export interface EndpointInformation {
|
|
||||||
method: string
|
|
||||||
urlPath: string
|
|
||||||
}
|
|
@ -3,7 +3,6 @@ import { noZeroX } from '../utils'
|
|||||||
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
|
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
|
||||||
import { File } from '../ddo/interfaces/File'
|
import { File } from '../ddo/interfaces/File'
|
||||||
import { ComputeJob } from '../ocean/interfaces/ComputeJob'
|
import { ComputeJob } from '../ocean/interfaces/ComputeJob'
|
||||||
import { EndpointInformation } from '../ocean/interfaces/EndpointInformation'
|
|
||||||
import { Output } from '../ocean/interfaces/ComputeOutput'
|
import { Output } from '../ocean/interfaces/ComputeOutput'
|
||||||
import { MetadataAlgorithm } from '../ddo/interfaces/MetadataAlgorithm'
|
import { MetadataAlgorithm } from '../ddo/interfaces/MetadataAlgorithm'
|
||||||
import { Versions } from '../ocean/Versions'
|
import { Versions } from '../ocean/Versions'
|
||||||
@ -11,7 +10,11 @@ import { Response } from 'node-fetch'
|
|||||||
import { DDO } from '../ddo/DDO'
|
import { DDO } from '../ddo/DDO'
|
||||||
import DID from '../ocean/DID'
|
import DID from '../ocean/DID'
|
||||||
|
|
||||||
const apiPath = '/api/v1/services'
|
export interface EndpointInformation {
|
||||||
|
serviceName: string
|
||||||
|
method: string
|
||||||
|
urlPath: string
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides an interface for provider service.
|
* Provides an interface for provider service.
|
||||||
@ -23,25 +26,55 @@ export class Provider extends Instantiable {
|
|||||||
public nonce: string
|
public nonce: string
|
||||||
private baseUrl: string
|
private baseUrl: string
|
||||||
public servicesEndpoints: any
|
public servicesEndpoints: any
|
||||||
|
public providerAddress: string
|
||||||
|
/**
|
||||||
|
* Returns the instance of Provider.
|
||||||
|
* @return {Promise<Assets>}
|
||||||
|
*/
|
||||||
|
public static async getInstance(config: InstantiableConfig): Promise<Provider> {
|
||||||
|
const instance = new Provider()
|
||||||
|
instance.setInstanceConfig(config)
|
||||||
|
instance.nonce = '0'
|
||||||
|
await instance.setBaseUrl(config.config.providerUri)
|
||||||
|
return instance
|
||||||
|
}
|
||||||
|
|
||||||
public get url(): string {
|
public get url(): string {
|
||||||
return this.baseUrl
|
return this.baseUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(config: InstantiableConfig) {
|
public async setBaseUrl(url: string): Promise<boolean> {
|
||||||
super()
|
|
||||||
this.setInstanceConfig(config)
|
|
||||||
this.baseUrl = this.config.providerUri
|
|
||||||
this.nonce = '0'
|
|
||||||
this.setServiceEndpoints()
|
|
||||||
}
|
|
||||||
|
|
||||||
public setBaseUrl(url: string): void {
|
|
||||||
this.baseUrl = url
|
this.baseUrl = url
|
||||||
|
this.servicesEndpoints = await this.getServiceEndpoints()
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
public async setServiceEndpoints(): Promise<void> {
|
public async getServiceEndpoints(): Promise<EndpointInformation[]> {
|
||||||
this.servicesEndpoints = await this.getServiceEndpoints()
|
const serviceEndpoints: EndpointInformation[] = []
|
||||||
|
try {
|
||||||
|
const result = await (await this.ocean.utils.fetch.get(this.url)).json()
|
||||||
|
this.providerAddress = result['provider-address']
|
||||||
|
for (const i in result.serviceEndpoints) {
|
||||||
|
const endpoint: EndpointInformation = {
|
||||||
|
serviceName: i,
|
||||||
|
method: result.serviceEndpoints[i][0],
|
||||||
|
urlPath: this.url + result.serviceEndpoints[i][1]
|
||||||
|
}
|
||||||
|
serviceEndpoints.push(endpoint)
|
||||||
|
}
|
||||||
|
return serviceEndpoints
|
||||||
|
} catch (e) {
|
||||||
|
this.logger.error('Finding the service endpoints failed:', e)
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getEndpointURL(serviceName: string): EndpointInformation {
|
||||||
|
if (!this.servicesEndpoints) return null
|
||||||
|
return this.servicesEndpoints.find(
|
||||||
|
(s) => s.serviceName === serviceName
|
||||||
|
) as EndpointInformation
|
||||||
}
|
}
|
||||||
|
|
||||||
public async createSignature(account: Account, agreementId: string): Promise<string> {
|
public async createSignature(account: Account, agreementId: string): Promise<string> {
|
||||||
@ -193,53 +226,6 @@ export class Provider extends Instantiable {
|
|||||||
return destination
|
return destination
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getServiceEndpoints() {
|
|
||||||
const url = this.getURI()
|
|
||||||
const fetch = this.ocean.utils.fetch.get(url)
|
|
||||||
if (this.servicesEndpoints == null) {
|
|
||||||
this.servicesEndpoints = await fetch
|
|
||||||
.then((response: Response) => {
|
|
||||||
if (response.ok) {
|
|
||||||
return response.json()
|
|
||||||
}
|
|
||||||
|
|
||||||
this.logger.error(
|
|
||||||
'Finding the service endpoints failed:',
|
|
||||||
response.status,
|
|
||||||
response.statusText
|
|
||||||
)
|
|
||||||
|
|
||||||
return null
|
|
||||||
})
|
|
||||||
.catch((error: Error) => {
|
|
||||||
this.logger.error('Error with service endpoints')
|
|
||||||
this.logger.error(error.message)
|
|
||||||
throw error
|
|
||||||
})
|
|
||||||
}
|
|
||||||
console.log('JSON format: ' + JSON.stringify(this.servicesEndpoints))
|
|
||||||
const myJSON = JSON.parse(
|
|
||||||
JSON.stringify(this.servicesEndpoints),
|
|
||||||
function (key, value) {
|
|
||||||
if (key === 'serviceEndpoints') console.log('eeee: ' + JSON.parse(value))
|
|
||||||
return JSON.parse(value)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
public getEndpointURL(serviceName: string): EndpointInformation {
|
|
||||||
const methodEndpoint = this.servicesEndpoints.serviceEndpoints[serviceName][0]
|
|
||||||
console.log('Method is: ' + methodEndpoint)
|
|
||||||
const urlEndpoint = this.servicesEndpoints.serviceEndpoints[serviceName][1]
|
|
||||||
const urlEndpointPath = `${this.getURI()}${urlEndpoint}`
|
|
||||||
console.log('URL: ' + urlEndpointPath)
|
|
||||||
const myVar: EndpointInformation = {
|
|
||||||
method: methodEndpoint,
|
|
||||||
urlPath: urlEndpointPath
|
|
||||||
}
|
|
||||||
return myVar
|
|
||||||
}
|
|
||||||
|
|
||||||
public async compute(
|
public async compute(
|
||||||
method: string,
|
method: string,
|
||||||
did: string,
|
did: string,
|
||||||
@ -258,7 +244,21 @@ export class Provider extends Instantiable {
|
|||||||
): Promise<ComputeJob | ComputeJob[]> {
|
): Promise<ComputeJob | ComputeJob[]> {
|
||||||
const address = consumerAccount.getId()
|
const address = consumerAccount.getId()
|
||||||
await this.getNonce(consumerAccount.getId())
|
await this.getNonce(consumerAccount.getId())
|
||||||
let url = this.getComputeEndpoint()
|
let url
|
||||||
|
switch (method) {
|
||||||
|
case 'get':
|
||||||
|
url = this.getComputeStatusEndpoint().urlPath
|
||||||
|
break
|
||||||
|
case 'post':
|
||||||
|
url = this.getComputeStartEndpoint().urlPath
|
||||||
|
break
|
||||||
|
case 'put':
|
||||||
|
url = this.getComputeStopEndpoint().urlPath
|
||||||
|
break
|
||||||
|
case 'delete':
|
||||||
|
url = this.getComputeDeleteEndpoint().urlPath
|
||||||
|
break
|
||||||
|
}
|
||||||
url += `?documentId=${noZeroX(did)}`
|
url += `?documentId=${noZeroX(did)}`
|
||||||
if (sign) {
|
if (sign) {
|
||||||
let signatureMessage = address
|
let signatureMessage = address
|
||||||
@ -346,7 +346,6 @@ export class Provider extends Instantiable {
|
|||||||
|
|
||||||
public getNonceEndpoint(): EndpointInformation {
|
public getNonceEndpoint(): EndpointInformation {
|
||||||
// Output: NONCE: [Object object]
|
// Output: NONCE: [Object object]
|
||||||
console.log('NONCE: ' + this.getEndpointURL('nonce'))
|
|
||||||
return this.getEndpointURL('nonce')
|
return this.getEndpointURL('nonce')
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -358,27 +357,19 @@ export class Provider extends Instantiable {
|
|||||||
return this.getEndpointURL('fileinfo')
|
return this.getEndpointURL('fileinfo')
|
||||||
}
|
}
|
||||||
|
|
||||||
public getComputeEndpointPath(): string {
|
public getComputeStatusEndpoint(): EndpointInformation {
|
||||||
return `${apiPath}/compute`
|
|
||||||
}
|
|
||||||
|
|
||||||
public getComputeEndpoint(): string {
|
|
||||||
return `${this.url}` + this.getComputeEndpointPath()
|
|
||||||
}
|
|
||||||
|
|
||||||
public getComputeStatusJob(): EndpointInformation {
|
|
||||||
return this.getEndpointURL('computeStatus')
|
return this.getEndpointURL('computeStatus')
|
||||||
}
|
}
|
||||||
|
|
||||||
public getStartComputeJob(): EndpointInformation {
|
public getComputeStartEndpoint(): EndpointInformation {
|
||||||
return this.getEndpointURL('computeStart')
|
return this.getEndpointURL('computeStart')
|
||||||
}
|
}
|
||||||
|
|
||||||
public getStopComputeJob(): EndpointInformation {
|
public getComputeStopEndpoint(): EndpointInformation {
|
||||||
return this.getEndpointURL('computeStop')
|
return this.getEndpointURL('computeStop')
|
||||||
}
|
}
|
||||||
|
|
||||||
public getDeleteComputeJob(): EndpointInformation {
|
public getComputeDeleteEndpoint(): EndpointInformation {
|
||||||
return this.getEndpointURL('computeDelete')
|
return this.getEndpointURL('computeDelete')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user