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

Tests failling. Implemented EndpointInformation.

This commit is contained in:
mariacarmina 2021-01-26 09:45:56 +02:00
parent 8642bc5625
commit 59dc288893
2 changed files with 50 additions and 43 deletions

View File

@ -0,0 +1,4 @@
export interface EndpointInformation {
method: string
urlPath: string
}

View File

@ -3,6 +3,7 @@ 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'
@ -21,7 +22,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 servicesEndpoints: any
public get url(): string { public get url(): string {
return this.baseUrl return this.baseUrl
@ -32,12 +33,17 @@ export class Provider extends Instantiable {
this.setInstanceConfig(config) this.setInstanceConfig(config)
this.baseUrl = this.config.providerUri this.baseUrl = this.config.providerUri
this.nonce = '0' this.nonce = '0'
this.setServiceEndpoints()
} }
public setBaseUrl(url: string): void { public setBaseUrl(url: string): void {
this.baseUrl = url this.baseUrl = url
} }
public async setServiceEndpoints(): Promise<void> {
this.servicesEndpoints = await this.getServiceEndpoints()
}
public async createSignature(account: Account, agreementId: string): Promise<string> { public async createSignature(account: Account, agreementId: string): Promise<string> {
const signature = await this.ocean.utils.signature.signText( const signature = await this.ocean.utils.signature.signText(
noZeroX(agreementId), noZeroX(agreementId),
@ -65,7 +71,7 @@ export class Provider extends Instantiable {
} }
try { try {
const response = await this.ocean.utils.fetch.post( const response = await this.ocean.utils.fetch.post(
this.getEncryptEndpoint(), this.getEncryptEndpoint().urlPath,
decodeURI(JSON.stringify(args)) decodeURI(JSON.stringify(args))
) )
return (await response.json()).encryptedDocument return (await response.json()).encryptedDocument
@ -87,7 +93,7 @@ export class Provider extends Instantiable {
} else args = { url } } else args = { url }
try { try {
const response = await this.ocean.utils.fetch.post( const response = await this.ocean.utils.fetch.post(
this.getFileinfoEndpoint(), this.getFileinfoEndpoint().urlPath,
JSON.stringify(args) JSON.stringify(args)
) )
const results: File[] = await response.json() const results: File[] = await response.json()
@ -105,7 +111,7 @@ export class Provider extends Instantiable {
* @return {Promise<string>} string * @return {Promise<string>} string
*/ */
public async getNonce(consumerAddress: string): Promise<string> { public async getNonce(consumerAddress: string): Promise<string> {
let initializeUrl = this.getNonceEndpoint() let initializeUrl = this.getNonceEndpoint().urlPath
initializeUrl += `?userAddress=${consumerAddress}` initializeUrl += `?userAddress=${consumerAddress}`
try { try {
const response = await this.ocean.utils.fetch.get(initializeUrl) const response = await this.ocean.utils.fetch.get(initializeUrl)
@ -132,7 +138,7 @@ export class Provider extends Instantiable {
throw new Error('Failed to resolve DID') throw new Error('Failed to resolve DID')
} }
let initializeUrl = this.getInitializeEndpoint() let initializeUrl = this.getInitializeEndpoint().urlPath
initializeUrl += `?documentId=${did}` initializeUrl += `?documentId=${did}`
initializeUrl += `&serviceId=${serviceIndex}` initializeUrl += `&serviceId=${serviceIndex}`
initializeUrl += `&serviceType=${serviceType}` initializeUrl += `&serviceType=${serviceType}`
@ -163,7 +169,7 @@ export class Provider extends Instantiable {
const filesPromises = files const filesPromises = files
.filter((_, i) => index === -1 || i === index) .filter((_, i) => index === -1 || i === index)
.map(async ({ index: i }) => { .map(async ({ index: i }) => {
let consumeUrl = this.getDownloadEndpoint() let consumeUrl = this.getDownloadEndpoint().urlPath
consumeUrl += `?fileIndex=${i}` consumeUrl += `?fileIndex=${i}`
consumeUrl += `&documentId=${did}` consumeUrl += `&documentId=${did}`
consumeUrl += `&serviceId=${serviceIndex}` consumeUrl += `&serviceId=${serviceIndex}`
@ -188,9 +194,10 @@ export class Provider extends Instantiable {
} }
public async getServiceEndpoints() { public async getServiceEndpoints() {
const fetch = this.ocean.utils.fetch.get(this.url) const url = this.getURI()
if (this.validServiceEndpoints != null) { const fetch = this.ocean.utils.fetch.get(url)
this.validServiceEndpoints = await fetch if (this.servicesEndpoints == null) {
this.servicesEndpoints = await fetch
.then((response: Response) => { .then((response: Response) => {
if (response.ok) { if (response.ok) {
return response.json() return response.json()
@ -210,16 +217,27 @@ export class Provider extends Instantiable {
throw error throw error
}) })
} }
return this.validServiceEndpoints 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): string { public getEndpointURL(serviceName: string): EndpointInformation {
let urlEndpoint: string const methodEndpoint = this.servicesEndpoints.serviceEndpoints[serviceName][0]
this.validServiceEndpoints = this.getServiceEndpoints() console.log('Method is: ' + methodEndpoint)
const method = this.validServiceEndpoints.serviceEndpoints[serviceName][0] const urlEndpoint = this.servicesEndpoints.serviceEndpoints[serviceName][1]
urlEndpoint = this.validServiceEndpoints.serviceEndpoints[serviceName][1] const urlEndpointPath = `${this.getURI()}${urlEndpoint}`
urlEndpoint = urlEndpoint.replace(`${apiPath}`, '') console.log('URL: ' + urlEndpointPath)
return `${this.getURI()}${urlEndpoint}` const myVar: EndpointInformation = {
method: methodEndpoint,
urlPath: urlEndpointPath
}
return myVar
} }
public async compute( public async compute(
@ -322,38 +340,24 @@ export class Provider extends Instantiable {
return `${this.url}` return `${this.url}`
} }
public getInitializeEndpoint(): string { public getInitializeEndpoint(): EndpointInformation {
// return `${this.url}${apiPath}/initialize`
return this.getEndpointURL('initialize') return this.getEndpointURL('initialize')
} }
public getNonceEndpoint(): string { public getNonceEndpoint(): EndpointInformation {
// return `${this.url}${apiPath}/nonce` // Output: NONCE: [Object object]
console.log('NONCE: ' + this.getEndpointURL('nonce'))
return this.getEndpointURL('nonce') return this.getEndpointURL('nonce')
} }
public getConsumeEndpointPath(): string { public getEncryptEndpoint(): EndpointInformation {
return `${apiPath}/consume`
}
public getConsumeEndpoint(): string {
return `${this.url}` + this.getConsumeEndpointPath()
}
public getEncryptEndpoint(): string {
// return `${this.url}${apiPath}/encrypt`
return this.getEndpointURL('encrypt') return this.getEndpointURL('encrypt')
} }
public getFileinfoEndpoint(): string { public getFileinfoEndpoint(): EndpointInformation {
// return `${this.url}${apiPath}/fileinfo`
return this.getEndpointURL('fileinfo') return this.getEndpointURL('fileinfo')
} }
public getPublishEndpoint(): string {
return `${this.url}${apiPath}/publish`
}
public getComputeEndpointPath(): string { public getComputeEndpointPath(): string {
return `${apiPath}/compute` return `${apiPath}/compute`
} }
@ -362,24 +366,23 @@ export class Provider extends Instantiable {
return `${this.url}` + this.getComputeEndpointPath() return `${this.url}` + this.getComputeEndpointPath()
} }
public getComputeStatusJob(): string { public getComputeStatusJob(): EndpointInformation {
return this.getEndpointURL('computeStatus') return this.getEndpointURL('computeStatus')
} }
public getStartComputeJob(): string { public getStartComputeJob(): EndpointInformation {
return this.getEndpointURL('computeStart') return this.getEndpointURL('computeStart')
} }
public getStopComputeJob(): string { public getStopComputeJob(): EndpointInformation {
return this.getEndpointURL('computeStop') return this.getEndpointURL('computeStop')
} }
public getDeleteComputeJob(): string { public getDeleteComputeJob(): EndpointInformation {
return this.getEndpointURL('computeDelete') return this.getEndpointURL('computeDelete')
} }
public getDownloadEndpoint(): string { public getDownloadEndpoint(): EndpointInformation {
// return `${this.url}${apiPath}/download`
return this.getEndpointURL('download') return this.getEndpointURL('download')
} }