From 03ed3f6200ae7b3e599f48dbf561fa741a288312 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Thu, 21 Jan 2021 12:42:30 +0200 Subject: [PATCH] Draft for Dynamic provider endpoints. Need a feedback about the function modifications in Provider.ts. Need to do unit tests. --- src/provider/Provider.ts | 77 ++++++++++++++++++++++++++++++++++------ 1 file changed, 67 insertions(+), 10 deletions(-) diff --git a/src/provider/Provider.ts b/src/provider/Provider.ts index 0a575b37..629bbbc9 100644 --- a/src/provider/Provider.ts +++ b/src/provider/Provider.ts @@ -21,6 +21,7 @@ const apiPath = '/api/v1/services' export class Provider extends Instantiable { public nonce: string private baseUrl: string + public validServiceEndpoints: any public get url(): string { return this.baseUrl @@ -186,6 +187,42 @@ export class Provider extends Instantiable { 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( method: string, did: string, @@ -242,7 +279,6 @@ export class Provider extends Instantiable { // 'algorithmDid': alg_ddo.did, // 'algorithmMeta': {}, // 'algorithmDataToken': alg_data_token - // switch fetch method let fetch switch (method) { @@ -288,11 +324,13 @@ export class Provider extends Instantiable { } public getInitializeEndpoint(): string { - return `${this.url}${apiPath}/initialize` + //return `${this.url}${apiPath}/initialize` + return this.getEndpointURL('initialize') } public getNonceEndpoint(): string { - return `${this.url}${apiPath}/nonce` + //return `${this.url}${apiPath}/nonce` + return this.getEndpointURL('nonce') } public getConsumeEndpointPath(): string { @@ -304,27 +342,46 @@ export class Provider extends Instantiable { } public getEncryptEndpoint(): string { - return `${this.url}${apiPath}/encrypt` + //return `${this.url}${apiPath}/encrypt` + return this.getEndpointURL('encrypt') } public getFileinfoEndpoint(): string { - return `${this.url}${apiPath}/fileinfo` + //return `${this.url}${apiPath}/fileinfo` + return this.getEndpointURL('fileinfo') } public getPublishEndpoint(): string { return `${this.url}${apiPath}/publish` } - public getComputeEndpointPath(): string { - return `${apiPath}/compute` + // public getComputeEndpointPath(): string { + // return `${apiPath}/compute` + // } + + // public getComputeEndpoint(): string { + // return `${this.url}` + this.getComputeEndpointPath() + // } + + public getComputeStatusJob(): string { + return this.getEndpointURL('computeStatus') } - public getComputeEndpoint(): string { - return `${this.url}` + this.getComputeEndpointPath() + public getStartComputeJob(): string { + return this.getEndpointURL('computeStart') + } + + public getStopComputeJob(): string { + return this.getEndpointURL('computeStop') + } + + public getDeleteComputeJob(): string { + return this.getEndpointURL('computeDelete') } public getDownloadEndpoint(): string { - return `${this.url}${apiPath}/download` + //return `${this.url}${apiPath}/download` + return this.getEndpointURL('download') } /** Check for a valid provider at URL