From b8fec2d0844009944f6492aa62049e806a4286a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Fri, 5 Apr 2019 12:20:42 +0200 Subject: [PATCH] Use the new Aquarius response type. --- .travis.yml | 1 + integration/ocean/SearchAsset.test.ts | 22 ++++++++---- src/aquarius/Aquarius.ts | 51 +++++++++++++++++---------- src/brizo/Brizo.ts | 6 ++-- src/ocean/OceanAssets.ts | 4 +-- test/aquarius/Aquarius.test.ts | 47 ++++++++++++++---------- test/ocean/Ocean.test.ts | 4 +-- 7 files changed, 84 insertions(+), 51 deletions(-) diff --git a/.travis.yml b/.travis.yml index b7200a2..e5fdfa1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,6 +21,7 @@ before_script: - ganache-cli --port 18545 > ganache-cli.log & - git clone https://github.com/oceanprotocol/barge - cd barge + - export AQUARIUS_VERSION=v0.2.1 - export KEEPER_VERSION=v0.9.0 - export KEEPER_OWNER_ROLE_ADDRESS="0xe2DD09d719Da89e5a3D0F2549c7E24566e947260" - bash -x start_ocean.sh --latest --no-pleuston --local-spree-node 2>&1 > start_ocean.log & diff --git a/integration/ocean/SearchAsset.test.ts b/integration/ocean/SearchAsset.test.ts index 0098718..3809267 100644 --- a/integration/ocean/SearchAsset.test.ts +++ b/integration/ocean/SearchAsset.test.ts @@ -27,13 +27,13 @@ describe("Search Asset", () => { }) it("should be able to search the assets", async () => { - const ddos: DDO[] = await ocean.assets.search(`Test1${testHash}`) + const {results: ddos} = await ocean.assets.search(`Test1${testHash}`) assert.isArray(ddos, "A search should return an array") test1length = ddos.length - test2length = (await ocean.assets.search(`Test2${testHash}`)).length - test3length = (await ocean.assets.search(`Test3${testHash}`)).length + test2length = (await ocean.assets.search(`Test2${testHash}`)).results.length + test3length = (await ocean.assets.search(`Test3${testHash}`)).results.length }) it("should regiester some a asset", async () => { @@ -44,19 +44,27 @@ describe("Search Asset", () => { }) it("should search by text and see the increment of DDOs", async () => { - assert.equal((await ocean.assets.search(`Test2${testHash}`)).length - test2length, 2, "Something was wrong searching the assets") - assert.equal((await ocean.assets.search(`Test3${testHash}`)).length - test3length, 1, "Something was wrong searching the assets") + assert.equal( + (await ocean.assets.search(`Test2${testHash}`)).results.length - test2length, + 2, + "Something was wrong searching the assets", + ) + assert.equal( + (await ocean.assets.search(`Test3${testHash}`)).results.length - test3length, + 1, + "Something was wrong searching the assets", + ) }) it("should return a list of DDOs", async () => { - const ddos: DDO[] = await ocean.assets.search(`Test1${testHash}`) + const {results: ddos} = await ocean.assets.search(`Test1${testHash}`) assert.equal(ddos.length - test1length, 1, "Something was wrong searching the assets") ddos.map((ddo) => assert.instanceOf(ddo, DDO, "The DDO is not an instance of a DDO")) }) it("should be able to do a query to get a list of DDOs", async () => { - const ddos: DDO[] = await ocean.assets.query({ + const {results: ddos} = await ocean.assets.query({ page: 0, offset: 1, query: { diff --git a/src/aquarius/Aquarius.ts b/src/aquarius/Aquarius.ts index 602e99c..1718322 100644 --- a/src/aquarius/Aquarius.ts +++ b/src/aquarius/Aquarius.ts @@ -6,6 +6,13 @@ import { Instantiable, InstantiableConfig } from "../Instantiable.abstract" const apiPath = "/api/v1/aquarius/assets/ddo" +export interface QueryResult { + results: DDO[] + page: number + totalPages: number + totalResults: number +} + export interface SearchQuery { text?: string offset: number @@ -53,26 +60,24 @@ export class Aquarius extends Instantiable { /** * Search over the DDOs using a query. * @param {SearchQuery} query Query to filter the DDOs. - * @return {Promise} + * @return {Promise} */ - public async queryMetadata(query: SearchQuery): Promise { - const result: DDO[] = await WebServiceConnectorProvider.getConnector() + public async queryMetadata(query: SearchQuery): Promise { + const result: QueryResult = await WebServiceConnectorProvider.getConnector() .post(`${this.url}${apiPath}/query`, JSON.stringify(query)) .then((response: any) => { if (response.ok) { return response.json() as DDO[] } this.logger.error("queryMetadata failed:", response.status, response.statusText) - return [] as DDO[] + return this.transformResult() }) - .then((ddos) => { - return ddos.map((ddo): DDO => { - return new DDO(ddo as DDO) - }) + .then((results) => { + return this.transformResult(results) }) .catch((error) => { this.logger.error("Error fetching querying metadata: ", error) - return [] as DDO[] + return this.transformResult() }) return result @@ -81,31 +86,29 @@ export class Aquarius extends Instantiable { /** * Search over the DDOs using a query. * @param {SearchQuery} query Query to filter the DDOs. - * @return {Promise} + * @return {Promise} */ - public async queryMetadataByText(query: SearchQuery): Promise { + public async queryMetadataByText(query: SearchQuery): Promise { const fullUrl = new URL(`${this.url}${apiPath}/query`) fullUrl.searchParams.append("text", query.text) fullUrl.searchParams.append("sort", decodeURIComponent(JSON.stringify(query.sort))) fullUrl.searchParams.append("offset", query.offset.toString()) fullUrl.searchParams.append("page", query.page.toString()) - const result: DDO[] = await WebServiceConnectorProvider.getConnector() + const result: QueryResult = await WebServiceConnectorProvider.getConnector() .get(fullUrl) .then((response: any) => { if (response.ok) { return response.json() as DDO[] } this.logger.log("queryMetadataByText failed:", response.status, response.statusText) - return [] as DDO[] + return this.transformResult() }) - .then((ddos) => { - return ddos.map((ddo): DDO => { - return new DDO(ddo as DDO) - }) + .then((results) => { + return this.transformResult(results) }) .catch((error) => { this.logger.error("Error fetching querying metadata by text: ", error) - return [] as DDO[] + return this.transformResult() }) return result @@ -168,4 +171,16 @@ export class Aquarius extends Instantiable { public getServiceEndpoint(did: DID) { return `${this.url}/api/v1/aquarius/assets/metadata/${did.getId()}` } + + private transformResult( + {results, page, total_pages, total_results}: any = {result: [], page: 0, total_pages: 0, total_results: 0}, + ): QueryResult { + + return { + results: (results || []).map((ddo) => new DDO(ddo as DDO)), + page, + totalPages: total_pages, + totalResults: total_results, + } + } } diff --git a/src/brizo/Brizo.ts b/src/brizo/Brizo.ts index e894c91..234be1b 100644 --- a/src/brizo/Brizo.ts +++ b/src/brizo/Brizo.ts @@ -77,9 +77,9 @@ export class Brizo extends Instantiable { const agreementIdSignature = await this.ocean.utils.signature.signText(agreementId, account.getId()) const filesPromises = files .filter(({}, i) => index === -1 || i === index) - .map(async ({index}) => { + .map(async ({index: i}) => { let consumeUrl = serviceEndpoint - consumeUrl += `?index=${index}` + consumeUrl += `?index=${i}` consumeUrl += `&serviceAgreementId=${agreementId}` consumeUrl += `&consumerAddress=${account.getId()}` consumeUrl += `&signature=${agreementIdSignature}` @@ -87,7 +87,7 @@ export class Brizo extends Instantiable { try { await this.downloadFile( consumeUrl, - `file-${index}`, + `file-${i}`, destination, ) } catch (e) { diff --git a/src/ocean/OceanAssets.ts b/src/ocean/OceanAssets.ts index a642166..203797d 100644 --- a/src/ocean/OceanAssets.ts +++ b/src/ocean/OceanAssets.ts @@ -262,7 +262,7 @@ export class OceanAssets extends Instantiable { * @param {SearchQuery} query Query to filter the assets. * @return {Promise} */ - public async query(query: SearchQuery): Promise { + public async query(query: SearchQuery) { return this.ocean.aquarius.queryMetadata(query) } @@ -271,7 +271,7 @@ export class OceanAssets extends Instantiable { * @param {SearchQuery} text Text to filter the assets. * @return {Promise} */ - public async search(text: string): Promise { + public async search(text: string) { return this.ocean.aquarius.queryMetadataByText({ text, page: 0, diff --git a/test/aquarius/Aquarius.test.ts b/test/aquarius/Aquarius.test.ts index c35c172..e1f5da7 100644 --- a/test/aquarius/Aquarius.test.ts +++ b/test/aquarius/Aquarius.test.ts @@ -1,4 +1,4 @@ -import * as assert from "assert" +import { assert } from "chai" import { Aquarius } from "../../src/aquarius/Aquarius" import { SearchQuery } from "../../src/aquarius/Aquarius" import { DDO } from "../../src/ddo/DDO" @@ -10,6 +10,9 @@ import WebServiceConnectorMock from "../mocks/WebServiceConnector.mock" describe("Aquarius", () => { const aquarius: Aquarius = new Aquarius({config} as any) + // tslint:disable-next-line + const getResults = (results: DDO[], page = 0, total_pages = 1, total_results = 1) => + ({results, page, total_pages, total_results}) describe("#queryMetadata()", () => { @@ -26,24 +29,26 @@ describe("Aquarius", () => { } as SearchQuery it("should query metadata", async () => { - // @ts-ignore - WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock([new DDO()])) + WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(getResults([new DDO()]))) - const result: DDO[] = await aquarius.queryMetadata(query) - assert(result) - assert(result.length !== null) + const result = await aquarius.queryMetadata(query) + assert.typeOf(result.results, "array") + assert.lengthOf(result.results, 1) + assert.equal(result.page, 0) + assert.equal(result.totalPages, 1) + assert.equal(result.totalResults, 1) }) it("should query metadata and return real ddo", async () => { // @ts-ignore - WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock([new DDO()])) + WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(getResults([new DDO()]))) - const result: DDO[] = await aquarius.queryMetadata(query) - - assert(result) - assert(result[0].findServiceById) + const result = await aquarius.queryMetadata(query) + assert.typeOf(result.results, "array") + assert.lengthOf(result.results, 1) + assert.isDefined(result.results[0].findServiceById) }) }) @@ -64,21 +69,25 @@ describe("Aquarius", () => { it("should query metadata by text", async () => { // @ts-ignore - WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock([new DDO()])) + WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(getResults([new DDO()]))) - const result: DDO[] = await aquarius.queryMetadataByText(query) - assert(result) - assert(result.length !== null) + const result = await aquarius.queryMetadataByText(query) + assert.typeOf(result.results, "array") + assert.lengthOf(result.results, 1) + assert.equal(result.page, 0) + assert.equal(result.totalPages, 1) + assert.equal(result.totalResults, 1) }) it("should query metadata and return real ddo", async () => { // @ts-ignore - WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock([new DDO()])) + WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(getResults([new DDO()]))) - const result: DDO[] = await aquarius.queryMetadataByText(query) - assert(result) - assert(result[0].findServiceById) + const result = await aquarius.queryMetadataByText(query) + assert.typeOf(result.results, "array") + assert.lengthOf(result.results, 1) + assert.isDefined(result.results[0].findServiceById) }) }) diff --git a/test/ocean/Ocean.test.ts b/test/ocean/Ocean.test.ts index 96767f5..52932ba 100644 --- a/test/ocean/Ocean.test.ts +++ b/test/ocean/Ocean.test.ts @@ -59,7 +59,7 @@ describe("Ocean", () => { text: "Office", } as SearchQuery - const assets: any[] = await ocean.assets.query(query) + const assets = await ocean.assets.query(query) assert(assets) }) @@ -68,7 +68,7 @@ describe("Ocean", () => { describe("#searchAssetsByText()", () => { it("should search for assets", async () => { const text = "office" - const assets: any[] = await ocean.assets.search(text) + const assets = await ocean.assets.search(text) assert(assets) })