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

Draft for Dynamic provider endpoints. Need a feedback about the function modifications in Provider.ts. Need to do unit tests.

This commit is contained in:
mariacarmina 2021-01-21 12:42:30 +02:00
parent 16c21e1ecb
commit 03ed3f6200

View File

@ -21,6 +21,7 @@ const apiPath = '/api/v1/services'
export class Provider extends Instantiable { export class Provider extends Instantiable {
public nonce: string public nonce: string
private baseUrl: string private baseUrl: string
public validServiceEndpoints: any
public get url(): string { public get url(): string {
return this.baseUrl return this.baseUrl
@ -186,6 +187,42 @@ export class Provider extends Instantiable {
return destination return destination
} }
public async getServiceEndpoints() {
let fetch: any
fetch = this.ocean.utils.fetch.get(this.url)
if (this.validServiceEndpoints != null) {
this.validServiceEndpoints = 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
})
}
return this.validServiceEndpoints;
}
public getEndpointURL(
serviceName : string
) : string {
var method: string
var urlEndpoint: string
this.validServiceEndpoints = this.getServiceEndpoints()
method = this.validServiceEndpoints['serviceEndpoints'][0]
urlEndpoint = this.validServiceEndpoints['serviceEndpoints'][1]
urlEndpoint = urlEndpoint.replace(`${apiPath}`, '')
return `${this.getURI()}${urlEndpoint}`
}
public async compute( public async compute(
method: string, method: string,
did: string, did: string,
@ -242,7 +279,6 @@ export class Provider extends Instantiable {
// 'algorithmDid': alg_ddo.did, // 'algorithmDid': alg_ddo.did,
// 'algorithmMeta': {}, // 'algorithmMeta': {},
// 'algorithmDataToken': alg_data_token // 'algorithmDataToken': alg_data_token
// switch fetch method // switch fetch method
let fetch let fetch
switch (method) { switch (method) {
@ -288,11 +324,13 @@ export class Provider extends Instantiable {
} }
public getInitializeEndpoint(): string { public getInitializeEndpoint(): string {
return `${this.url}${apiPath}/initialize` //return `${this.url}${apiPath}/initialize`
return this.getEndpointURL('initialize')
} }
public getNonceEndpoint(): string { public getNonceEndpoint(): string {
return `${this.url}${apiPath}/nonce` //return `${this.url}${apiPath}/nonce`
return this.getEndpointURL('nonce')
} }
public getConsumeEndpointPath(): string { public getConsumeEndpointPath(): string {
@ -304,27 +342,46 @@ export class Provider extends Instantiable {
} }
public getEncryptEndpoint(): string { public getEncryptEndpoint(): string {
return `${this.url}${apiPath}/encrypt` //return `${this.url}${apiPath}/encrypt`
return this.getEndpointURL('encrypt')
} }
public getFileinfoEndpoint(): string { public getFileinfoEndpoint(): string {
return `${this.url}${apiPath}/fileinfo` //return `${this.url}${apiPath}/fileinfo`
return this.getEndpointURL('fileinfo')
} }
public getPublishEndpoint(): string { public getPublishEndpoint(): string {
return `${this.url}${apiPath}/publish` return `${this.url}${apiPath}/publish`
} }
public getComputeEndpointPath(): string { // public getComputeEndpointPath(): string {
return `${apiPath}/compute` // return `${apiPath}/compute`
// }
// public getComputeEndpoint(): string {
// return `${this.url}` + this.getComputeEndpointPath()
// }
public getComputeStatusJob(): string {
return this.getEndpointURL('computeStatus')
} }
public getComputeEndpoint(): string { public getStartComputeJob(): string {
return `${this.url}` + this.getComputeEndpointPath() return this.getEndpointURL('computeStart')
}
public getStopComputeJob(): string {
return this.getEndpointURL('computeStop')
}
public getDeleteComputeJob(): string {
return this.getEndpointURL('computeDelete')
} }
public getDownloadEndpoint(): string { public getDownloadEndpoint(): string {
return `${this.url}${apiPath}/download` //return `${this.url}${apiPath}/download`
return this.getEndpointURL('download')
} }
/** Check for a valid provider at URL /** Check for a valid provider at URL