Skip to content

Commit

Permalink
[KianYee] #459 enhance file info function (#710)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
kianyee authored Apr 13, 2021
1 parent 23f319b commit 04c2a9b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
24 changes: 19 additions & 5 deletions src/provider/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -129,12 +130,9 @@ export class Provider extends Instantiable {
* @param {String | DID} url or did
* @return {Promise<File[]>} urlDetails
*/
public async fileinfo(url: string | DID): Promise<File[]> {
let args
public async fileinfo(url: string): Promise<File[]> {
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 {
Expand All @@ -149,6 +147,22 @@ export class Provider extends Instantiable {
}
}

public async isFileConsumable(did: DID, serviceIndex: number): Promise<boolean> {
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>} string
Expand Down
10 changes: 6 additions & 4 deletions test/integration/Marketplaceflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 04c2a9b

Please sign in to comment.