1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

Use the new Aquarius response type.

This commit is contained in:
Pedro Gutiérrez 2019-04-05 12:20:42 +02:00 committed by Pedro Gutiérrez
parent 9fe42ecf3c
commit b8fec2d084
7 changed files with 84 additions and 51 deletions

View File

@ -21,6 +21,7 @@ before_script:
- ganache-cli --port 18545 > ganache-cli.log & - ganache-cli --port 18545 > ganache-cli.log &
- git clone https://github.com/oceanprotocol/barge - git clone https://github.com/oceanprotocol/barge
- cd barge - cd barge
- export AQUARIUS_VERSION=v0.2.1
- export KEEPER_VERSION=v0.9.0 - export KEEPER_VERSION=v0.9.0
- export KEEPER_OWNER_ROLE_ADDRESS="0xe2DD09d719Da89e5a3D0F2549c7E24566e947260" - export KEEPER_OWNER_ROLE_ADDRESS="0xe2DD09d719Da89e5a3D0F2549c7E24566e947260"
- bash -x start_ocean.sh --latest --no-pleuston --local-spree-node 2>&1 > start_ocean.log & - bash -x start_ocean.sh --latest --no-pleuston --local-spree-node 2>&1 > start_ocean.log &

View File

@ -27,13 +27,13 @@ describe("Search Asset", () => {
}) })
it("should be able to search the assets", async () => { 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") assert.isArray(ddos, "A search should return an array")
test1length = ddos.length test1length = ddos.length
test2length = (await ocean.assets.search(`Test2${testHash}`)).length test2length = (await ocean.assets.search(`Test2${testHash}`)).results.length
test3length = (await ocean.assets.search(`Test3${testHash}`)).length test3length = (await ocean.assets.search(`Test3${testHash}`)).results.length
}) })
it("should regiester some a asset", async () => { 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 () => { 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(
assert.equal((await ocean.assets.search(`Test3${testHash}`)).length - test3length, 1, "Something was wrong searching the assets") (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 () => { 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") 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")) 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 () => { 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, page: 0,
offset: 1, offset: 1,
query: { query: {

View File

@ -6,6 +6,13 @@ import { Instantiable, InstantiableConfig } from "../Instantiable.abstract"
const apiPath = "/api/v1/aquarius/assets/ddo" const apiPath = "/api/v1/aquarius/assets/ddo"
export interface QueryResult {
results: DDO[]
page: number
totalPages: number
totalResults: number
}
export interface SearchQuery { export interface SearchQuery {
text?: string text?: string
offset: number offset: number
@ -53,26 +60,24 @@ export class Aquarius extends Instantiable {
/** /**
* Search over the DDOs using a query. * Search over the DDOs using a query.
* @param {SearchQuery} query Query to filter the DDOs. * @param {SearchQuery} query Query to filter the DDOs.
* @return {Promise<DDO[]>} * @return {Promise<QueryResult>}
*/ */
public async queryMetadata(query: SearchQuery): Promise<DDO[]> { public async queryMetadata(query: SearchQuery): Promise<QueryResult> {
const result: DDO[] = await WebServiceConnectorProvider.getConnector() const result: QueryResult = await WebServiceConnectorProvider.getConnector()
.post(`${this.url}${apiPath}/query`, JSON.stringify(query)) .post(`${this.url}${apiPath}/query`, JSON.stringify(query))
.then((response: any) => { .then((response: any) => {
if (response.ok) { if (response.ok) {
return response.json() as DDO[] return response.json() as DDO[]
} }
this.logger.error("queryMetadata failed:", response.status, response.statusText) this.logger.error("queryMetadata failed:", response.status, response.statusText)
return [] as DDO[] return this.transformResult()
}) })
.then((ddos) => { .then((results) => {
return ddos.map((ddo): DDO => { return this.transformResult(results)
return new DDO(ddo as DDO)
})
}) })
.catch((error) => { .catch((error) => {
this.logger.error("Error fetching querying metadata: ", error) this.logger.error("Error fetching querying metadata: ", error)
return [] as DDO[] return this.transformResult()
}) })
return result return result
@ -81,31 +86,29 @@ export class Aquarius extends Instantiable {
/** /**
* Search over the DDOs using a query. * Search over the DDOs using a query.
* @param {SearchQuery} query Query to filter the DDOs. * @param {SearchQuery} query Query to filter the DDOs.
* @return {Promise<DDO[]>} * @return {Promise<QueryResult>}
*/ */
public async queryMetadataByText(query: SearchQuery): Promise<DDO[]> { public async queryMetadataByText(query: SearchQuery): Promise<QueryResult> {
const fullUrl = new URL(`${this.url}${apiPath}/query`) const fullUrl = new URL(`${this.url}${apiPath}/query`)
fullUrl.searchParams.append("text", query.text) fullUrl.searchParams.append("text", query.text)
fullUrl.searchParams.append("sort", decodeURIComponent(JSON.stringify(query.sort))) fullUrl.searchParams.append("sort", decodeURIComponent(JSON.stringify(query.sort)))
fullUrl.searchParams.append("offset", query.offset.toString()) fullUrl.searchParams.append("offset", query.offset.toString())
fullUrl.searchParams.append("page", query.page.toString()) fullUrl.searchParams.append("page", query.page.toString())
const result: DDO[] = await WebServiceConnectorProvider.getConnector() const result: QueryResult = await WebServiceConnectorProvider.getConnector()
.get(fullUrl) .get(fullUrl)
.then((response: any) => { .then((response: any) => {
if (response.ok) { if (response.ok) {
return response.json() as DDO[] return response.json() as DDO[]
} }
this.logger.log("queryMetadataByText failed:", response.status, response.statusText) this.logger.log("queryMetadataByText failed:", response.status, response.statusText)
return [] as DDO[] return this.transformResult()
}) })
.then((ddos) => { .then((results) => {
return ddos.map((ddo): DDO => { return this.transformResult(results)
return new DDO(ddo as DDO)
})
}) })
.catch((error) => { .catch((error) => {
this.logger.error("Error fetching querying metadata by text: ", error) this.logger.error("Error fetching querying metadata by text: ", error)
return [] as DDO[] return this.transformResult()
}) })
return result return result
@ -168,4 +171,16 @@ export class Aquarius extends Instantiable {
public getServiceEndpoint(did: DID) { public getServiceEndpoint(did: DID) {
return `${this.url}/api/v1/aquarius/assets/metadata/${did.getId()}` 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,
}
}
} }

View File

@ -77,9 +77,9 @@ export class Brizo extends Instantiable {
const agreementIdSignature = await this.ocean.utils.signature.signText(agreementId, account.getId()) const agreementIdSignature = await this.ocean.utils.signature.signText(agreementId, account.getId())
const filesPromises = files const filesPromises = files
.filter(({}, i) => index === -1 || i === index) .filter(({}, i) => index === -1 || i === index)
.map(async ({index}) => { .map(async ({index: i}) => {
let consumeUrl = serviceEndpoint let consumeUrl = serviceEndpoint
consumeUrl += `?index=${index}` consumeUrl += `?index=${i}`
consumeUrl += `&serviceAgreementId=${agreementId}` consumeUrl += `&serviceAgreementId=${agreementId}`
consumeUrl += `&consumerAddress=${account.getId()}` consumeUrl += `&consumerAddress=${account.getId()}`
consumeUrl += `&signature=${agreementIdSignature}` consumeUrl += `&signature=${agreementIdSignature}`
@ -87,7 +87,7 @@ export class Brizo extends Instantiable {
try { try {
await this.downloadFile( await this.downloadFile(
consumeUrl, consumeUrl,
`file-${index}`, `file-${i}`,
destination, destination,
) )
} catch (e) { } catch (e) {

View File

@ -262,7 +262,7 @@ export class OceanAssets extends Instantiable {
* @param {SearchQuery} query Query to filter the assets. * @param {SearchQuery} query Query to filter the assets.
* @return {Promise<DDO[]>} * @return {Promise<DDO[]>}
*/ */
public async query(query: SearchQuery): Promise<DDO[]> { public async query(query: SearchQuery) {
return this.ocean.aquarius.queryMetadata(query) return this.ocean.aquarius.queryMetadata(query)
} }
@ -271,7 +271,7 @@ export class OceanAssets extends Instantiable {
* @param {SearchQuery} text Text to filter the assets. * @param {SearchQuery} text Text to filter the assets.
* @return {Promise<DDO[]>} * @return {Promise<DDO[]>}
*/ */
public async search(text: string): Promise<DDO[]> { public async search(text: string) {
return this.ocean.aquarius.queryMetadataByText({ return this.ocean.aquarius.queryMetadataByText({
text, text,
page: 0, page: 0,

View File

@ -1,4 +1,4 @@
import * as assert from "assert" import { assert } from "chai"
import { Aquarius } from "../../src/aquarius/Aquarius" import { Aquarius } from "../../src/aquarius/Aquarius"
import { SearchQuery } from "../../src/aquarius/Aquarius" import { SearchQuery } from "../../src/aquarius/Aquarius"
import { DDO } from "../../src/ddo/DDO" import { DDO } from "../../src/ddo/DDO"
@ -10,6 +10,9 @@ import WebServiceConnectorMock from "../mocks/WebServiceConnector.mock"
describe("Aquarius", () => { describe("Aquarius", () => {
const aquarius: Aquarius = new Aquarius({config} as any) 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()", () => { describe("#queryMetadata()", () => {
@ -26,24 +29,26 @@ describe("Aquarius", () => {
} as SearchQuery } as SearchQuery
it("should query metadata", async () => { it("should query metadata", async () => {
// @ts-ignore // @ts-ignore
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock([new DDO()])) WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(getResults([new DDO()])))
const result: DDO[] = await aquarius.queryMetadata(query) const result = await aquarius.queryMetadata(query)
assert(result) assert.typeOf(result.results, "array")
assert(result.length !== null) 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 () => { it("should query metadata and return real ddo", async () => {
// @ts-ignore // @ts-ignore
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock([new DDO()])) WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(getResults([new DDO()])))
const result: DDO[] = await aquarius.queryMetadata(query) const result = await aquarius.queryMetadata(query)
assert.typeOf(result.results, "array")
assert(result) assert.lengthOf(result.results, 1)
assert(result[0].findServiceById) assert.isDefined(result.results[0].findServiceById)
}) })
}) })
@ -64,21 +69,25 @@ describe("Aquarius", () => {
it("should query metadata by text", async () => { it("should query metadata by text", async () => {
// @ts-ignore // @ts-ignore
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock([new DDO()])) WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(getResults([new DDO()])))
const result: DDO[] = await aquarius.queryMetadataByText(query) const result = await aquarius.queryMetadataByText(query)
assert(result) assert.typeOf(result.results, "array")
assert(result.length !== null) 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 () => { it("should query metadata and return real ddo", async () => {
// @ts-ignore // @ts-ignore
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock([new DDO()])) WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(getResults([new DDO()])))
const result: DDO[] = await aquarius.queryMetadataByText(query) const result = await aquarius.queryMetadataByText(query)
assert(result) assert.typeOf(result.results, "array")
assert(result[0].findServiceById) assert.lengthOf(result.results, 1)
assert.isDefined(result.results[0].findServiceById)
}) })
}) })

View File

@ -59,7 +59,7 @@ describe("Ocean", () => {
text: "Office", text: "Office",
} as SearchQuery } as SearchQuery
const assets: any[] = await ocean.assets.query(query) const assets = await ocean.assets.query(query)
assert(assets) assert(assets)
}) })
@ -68,7 +68,7 @@ describe("Ocean", () => {
describe("#searchAssetsByText()", () => { describe("#searchAssetsByText()", () => {
it("should search for assets", async () => { it("should search for assets", async () => {
const text = "office" const text = "office"
const assets: any[] = await ocean.assets.search(text) const assets = await ocean.assets.search(text)
assert(assets) assert(assets)
}) })