From 04c2a9b09a28c4ebf688d9ee21568077d8fc619b Mon Sep 17 00:00:00 2001 From: TSS-LauKY <71874741+TSS-LauKY@users.noreply.github.com> Date: Tue, 13 Apr 2021 19:39:11 +0800 Subject: [PATCH] [KianYee] #459 enhance file info function (#710) * [KianYee] #459 enhance file info function * [KianYee] #459 enhance file info function * [KianYee] Fix issue * [KianYee] #459 fix codeclimate * [KianYee] #459 add intergration test * [KianYee] Add integration test * [KianYee] #459 Fix bug * [KianYee] #459 Slipt function into 2 * [KianYee] removed unused import * [KianYee] update test case description --- src/provider/Provider.ts | 24 +++++++++++++++++++----- test/integration/Marketplaceflow.test.ts | 10 ++++++---- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/provider/Provider.ts b/src/provider/Provider.ts index af4896db..0b301d92 100644 --- a/src/provider/Provider.ts +++ b/src/provider/Provider.ts @@ -9,6 +9,7 @@ import { MetadataAlgorithm } from '../ddo/interfaces/MetadataAlgorithm' import { Versions } from '../ocean/Versions' import { DDO } from '../ddo/DDO' import DID from '../ocean/DID' +import { Service } from '../ddo/interfaces' export interface ServiceEndpoint { serviceName: string @@ -129,12 +130,9 @@ export class Provider extends Instantiable { * @param {String | DID} url or did * @return {Promise} urlDetails */ - public async fileinfo(url: string | DID): Promise { - let args + public async fileinfo(url: string): Promise { + const args = { url } const files: File[] = [] - if (url instanceof DID) { - args = { did: url.getDid() } - } else args = { url } const path = this.getFileinfoEndpoint() ? this.getFileinfoEndpoint().urlPath : null if (!path) return null try { @@ -149,6 +147,22 @@ export class Provider extends Instantiable { } } + public async isFileConsumable(did: DID, serviceIndex: number): Promise { + const args = { did: did.getDid() } + const ddo = await this.ocean.metadataCache.retrieveDDO(did) + if (!ddo) return false + const service: Service = ddo.findServiceById(serviceIndex) + if (!service) return false + const path = service.serviceEndpoint + '/api/v1/services/fileinfo' + try { + const response = await this.ocean.utils.fetch.post(path, JSON.stringify(args)) + const results = await response.json() + return results[0].valid + } catch (e) { + return false + } + } + /** Get nonce from provider * @param {String} consumerAddress * @return {Promise} string diff --git a/test/integration/Marketplaceflow.test.ts b/test/integration/Marketplaceflow.test.ts index bbca40d4..05bbbd4e 100644 --- a/test/integration/Marketplaceflow.test.ts +++ b/test/integration/Marketplaceflow.test.ts @@ -448,11 +448,13 @@ describe('Marketplace flow', () => { assert(parseInt(metaData.attributes.main.timeout) === parseInt(newTimeout.toFixed())) }) - it('Alice should check if her asset has valid url(s)', async () => { + it('Alice should check if her asset is consumable', async () => { + const service = ddo.findServiceByType('access') + assert(service !== null) + const serviceIndex = service.index const did: DID = DID.generate(noDidPrefixed(ddo.id)) - const response = await ocean.provider.fileinfo(did) - assert(response[0].contentLength === '1161') - assert(response[0].contentType === 'application/json') + const response = await ocean.provider.isFileConsumable(did, serviceIndex) + assert(response === true) }) it('Alice should create a FRE pricing for her asset', async () => {