diff --git a/src/aquarius/Aquarius.ts b/src/aquarius/Aquarius.ts index e3a0dc48..1d76913b 100644 --- a/src/aquarius/Aquarius.ts +++ b/src/aquarius/Aquarius.ts @@ -1,4 +1,4 @@ -import { LoggerInstance } from '../utils' +import { LoggerInstance, crossFetchGeneric } from '../utils' import { Asset, DDO, Metadata, ValidateMetadata } from '../@types/' import { json } from 'stream/consumers' @@ -17,12 +17,15 @@ export class Aquarius { * @param {string} fetchMethod fetch client instance * @return {Promise} DDO */ - public async resolve(did: string, fetchMethod: any): Promise { + public async resolve(did: string, fetchMethod?: any): Promise { + const preferedFetch = fetchMethod || crossFetchGeneric const path = this.aquariusURL + '/api/aquarius/assets/ddo/' + did try { - const response = await fetchMethod('GET', path) + const response = await preferedFetch('GET', path, null, { + 'Content-Type': 'application/json' + }) if (response.ok) { - const raw = await response.json() + const raw = response.data ? response.data : await response.json() return raw as DDO } else { throw new Error('HTTP request failed with status ' + response.status) @@ -44,19 +47,27 @@ export class Aquarius { /** * Blocks until Aqua will cache the did (or the update for that did) or timeouts + + * @param {string} did DID of the asset. + * @param {string} txid used when the did exists and we expect an update with that txid. * @param {string} fetchMethod fetch client instance - * @param {string} did DID of the asset. - * @param {string} txid used when the did exists and we expect an update with that txid. * @return {Promise} DDO of the asset. */ - public async waitForAqua(fetchMethod: any, did: string, txid?: string): Promise { + public async waitForAqua( + did: string, + txid?: string, + fetchMethod?: any + ): Promise { let tries = 0 do { try { + const preferedFetch = fetchMethod || crossFetchGeneric const path = this.aquariusURL + '/api/aquarius/assets/ddo/' + did - const response = await fetchMethod('GET', path) + const response = await preferedFetch('GET', path, null, { + 'Content-Type': 'application/json' + }) if (response.ok) { - const ddo = await response.json() + const ddo = response.data ? response.data : await response.json() if (txid) { // check tx if (ddo.event && ddo.event.txid === txid) return ddo as Asset @@ -73,21 +84,22 @@ export class Aquarius { /** * Validate DDO content + * @param {DDO} ddo DID Descriptor Object content. * @param {string} fetchMethod fetch client instance - * @param {DDO} ddo DID Descriptor Object content. * @return {Promise}. */ - public async validate(fetchMethod: any, ddo: DDO): Promise { + public async validate(ddo: DDO, fetchMethod: any): Promise { + const preferedFetch = fetchMethod || crossFetchGeneric const status: ValidateMetadata = { valid: false } let jsonResponse try { const path = this.aquariusURL + '/api/aquarius/assets/ddo/validate' - const response = await fetchMethod('POST', path, JSON.stringify(ddo), { + const response = await preferedFetch('POST', path, JSON.stringify(ddo), { 'Content-Type': 'application/octet-stream' }) - jsonResponse = await response.json() + jsonResponse = response.data ? response.data : await response.json() if (response.status === 200) { status.valid = true status.hash = jsonResponse.hash diff --git a/test/integration/ComputeFlow.test.ts b/test/integration/ComputeFlow.test.ts index fdf4bca4..21d44a7a 100644 --- a/test/integration/ComputeFlow.test.ts +++ b/test/integration/ComputeFlow.test.ts @@ -241,9 +241,13 @@ describe('Simple compute tests', async () => { '0x' + metadataHash ) // let's wait - const resolvedDDOAsset = await aquarius.waitForAqua(crossFetchGeneric, ddo.id) + const resolvedDDOAsset = await aquarius.waitForAqua(ddo.id, null, crossFetchGeneric) assert(resolvedDDOAsset, 'Cannot fetch DDO from Aquarius') - const resolvedDDOAlgo = await aquarius.waitForAqua(crossFetchGeneric, algoDdo.id) + const resolvedDDOAlgo = await aquarius.waitForAqua( + algoDdo.id, + null, + crossFetchGeneric + ) assert(resolvedDDOAlgo, 'Cannot fetch DDO from Aquarius') // mint 1 ERC20 and send it to the consumer await datatoken.mint(datatokenAddressAsset, publisherAccount, '1', consumerAccount) diff --git a/test/integration/PublishFlows.test.ts b/test/integration/PublishFlows.test.ts index d81e111c..64a6c380 100644 --- a/test/integration/PublishFlows.test.ts +++ b/test/integration/PublishFlows.test.ts @@ -147,8 +147,8 @@ describe('Publish tests', async () => { 'did:op:' + SHA256(web3.utils.toChecksumAddress(nftAddress) + chain.toString(10)) const AssetValidation: ValidateMetadata = await aquarius.validate( - crossFetchGeneric, - poolDdo + poolDdo, + crossFetchGeneric ) assert(AssetValidation.valid === true, 'Published asset is not valid') @@ -173,7 +173,7 @@ describe('Publish tests', async () => { [AssetValidation.proof] ) - const resolvedDDO = await aquarius.waitForAqua(crossFetchGeneric, poolDdo.id) + const resolvedDDO = await aquarius.waitForAqua(poolDdo.id, null, crossFetchGeneric) assert(resolvedDDO, 'Cannot fetch DDO from Aquarius') }) @@ -236,8 +236,8 @@ describe('Publish tests', async () => { 'did:op:' + SHA256(web3.utils.toChecksumAddress(nftAddress) + chain.toString(10)) const isAssetValid: ValidateMetadata = await aquarius.validate( - crossFetchGeneric, - fixedPriceDdo + fixedPriceDdo, + crossFetchGeneric ) assert(isAssetValid.valid === true, 'Published asset is not valid') @@ -260,7 +260,11 @@ describe('Publish tests', async () => { '0x' + metadataHash, [] ) - const resolvedDDO = await aquarius.waitForAqua(crossFetchGeneric, fixedPriceDdo.id) + const resolvedDDO = await aquarius.waitForAqua( + fixedPriceDdo.id, + null, + crossFetchGeneric + ) assert(resolvedDDO, 'Cannot fetch DDO from Aquarius') }) @@ -317,8 +321,8 @@ describe('Publish tests', async () => { 'did:op:' + SHA256(web3.utils.toChecksumAddress(nftAddress) + chain.toString(10)) const isAssetValid: ValidateMetadata = await aquarius.validate( - crossFetchGeneric, - dispenserDdo + dispenserDdo, + crossFetchGeneric ) assert(isAssetValid.valid === true, 'Published asset is not valid') @@ -340,7 +344,11 @@ describe('Publish tests', async () => { encryptedResponse, '0x' + metadataHash ) - const resolvedDDO = await aquarius.waitForAqua(crossFetchGeneric, dispenserDdo.id) + const resolvedDDO = await aquarius.waitForAqua( + dispenserDdo.id, + null, + crossFetchGeneric + ) assert(resolvedDDO, 'Cannot fetch DDO from Aquarius') }) }) diff --git a/test/integration/SimplePublishConsumeFlow.test.ts b/test/integration/SimplePublishConsumeFlow.test.ts index 43512f8b..329042ca 100644 --- a/test/integration/SimplePublishConsumeFlow.test.ts +++ b/test/integration/SimplePublishConsumeFlow.test.ts @@ -122,7 +122,7 @@ describe('Simple Publish & consume test', async () => { encryptedResponse, '0x' + metadataHash ) - const resolvedDDO = await aquarius.waitForAqua(crossFetchGeneric, ddo.id) + const resolvedDDO = await aquarius.waitForAqua(ddo.id, null, crossFetchGeneric) assert(resolvedDDO, 'Cannot fetch DDO from Aquarius') // mint 1 ERC20 and send it to the consumer await datatoken.mint(datatokenAddress, publisherAccount, '1', consumerAccount)