mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
added ddo handling
This commit is contained in:
parent
787ed9dcf3
commit
093a6ea4af
@ -1,6 +1,9 @@
|
|||||||
|
import {URL} from "url"
|
||||||
|
import DDO from "../ddo/DDO"
|
||||||
import Config from "../models/Config"
|
import Config from "../models/Config"
|
||||||
import Logger from "../utils/Logger"
|
import Logger from "../utils/Logger"
|
||||||
import AquariusConnectorProvider from "./AquariusConnectorProvider"
|
import AquariusConnectorProvider from "./AquariusConnectorProvider"
|
||||||
|
import SearchQuery from "./query/SearchQuery"
|
||||||
|
|
||||||
export default class Aquarius {
|
export default class Aquarius {
|
||||||
|
|
||||||
@ -11,7 +14,7 @@ export default class Aquarius {
|
|||||||
this.url = config.aquariusUri
|
this.url = config.aquariusUri
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getAccessUrl(accessToken: any, payload: any): Promise<string> {
|
public async getAccessUrl(accessToken: any, payload: any): Promise<string | void> {
|
||||||
|
|
||||||
const accessUrl = await AquariusConnectorProvider.getConnector().post(
|
const accessUrl = await AquariusConnectorProvider.getConnector().post(
|
||||||
`${accessToken.service_endpoint}/${accessToken.resource_id}`,
|
`${accessToken.service_endpoint}/${accessToken.resource_id}`,
|
||||||
@ -33,11 +36,10 @@ export default class Aquarius {
|
|||||||
return accessUrl
|
return accessUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
public async queryMetadata(query): Promise<any[]> {
|
public async queryMetadata(query: SearchQuery): Promise<any[]> {
|
||||||
|
|
||||||
const result = await AquariusConnectorProvider.getConnector().post(
|
const result = await AquariusConnectorProvider.getConnector()
|
||||||
this.url + "/api/v1/aquarius/assets/ddo/query",
|
.post(this.url + "/api/v1/aquarius/assets/ddo/query", JSON.stringify(query))
|
||||||
JSON.stringify(query))
|
|
||||||
.then((response: any) => {
|
.then((response: any) => {
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return response.json()
|
return response.json()
|
||||||
@ -51,11 +53,15 @@ export default class Aquarius {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
public async queryMetadataByText(query): Promise<any[]> {
|
public async queryMetadataByText(query: SearchQuery): Promise<any[]> {
|
||||||
|
|
||||||
const result = await AquariusConnectorProvider.getConnector().get(
|
const fullUrl = new URL(this.url + "/api/v1/aquarius/assets/ddo/query")
|
||||||
this.url + "/api/v1/aquarius/assets/ddo/query",
|
fullUrl.searchParams.append("text", query.text)
|
||||||
JSON.stringify(query))
|
fullUrl.searchParams.append("sort", JSON.stringify(query.sort))
|
||||||
|
fullUrl.searchParams.append("offset", query.offset.toString())
|
||||||
|
fullUrl.searchParams.append("page", query.page.toString())
|
||||||
|
const result = await AquariusConnectorProvider.getConnector()
|
||||||
|
.get(fullUrl)
|
||||||
.then((response: any) => {
|
.then((response: any) => {
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return response.json()
|
return response.json()
|
||||||
@ -68,4 +74,38 @@ export default class Aquarius {
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async storeDDO(ddo: DDO): Promise<DDO> {
|
||||||
|
const fullUrl = this.url + `/api/v1/aquarius/assets/ddo`
|
||||||
|
const result = await AquariusConnectorProvider.getConnector()
|
||||||
|
.post(fullUrl, DDO.serialize(ddo))
|
||||||
|
.then((response: any) => {
|
||||||
|
if (response.ok) {
|
||||||
|
return response.json()
|
||||||
|
}
|
||||||
|
Logger.log("Failed:", response.status, response.statusText)
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
Logger.error("Error fetching querying metadata: ", error)
|
||||||
|
})
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
public async retrieveDDO(did: string): Promise<DDO> {
|
||||||
|
const fullUrl = this.url + `/api/v1/aquarius/assets/ddo/${did}`
|
||||||
|
const result = await AquariusConnectorProvider.getConnector()
|
||||||
|
.get(fullUrl)
|
||||||
|
.then((response: any) => {
|
||||||
|
if (response.ok) {
|
||||||
|
return response.json()
|
||||||
|
}
|
||||||
|
Logger.log("Failed:", response.status, response.statusText)
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
Logger.error("Error fetching querying metadata: ", error)
|
||||||
|
})
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
import fetch from "node-fetch"
|
import fetch from "node-fetch"
|
||||||
import { URL } from "url"
|
|
||||||
|
|
||||||
export default class AquariusConnector {
|
export default class AquariusConnector {
|
||||||
|
|
||||||
public post(url, payload) {
|
public async post(url, payload): Promise<any> {
|
||||||
return fetch(url, {
|
return this.fetch(url, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: payload,
|
body: payload,
|
||||||
headers: {
|
headers: {
|
||||||
@ -13,17 +12,26 @@ export default class AquariusConnector {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public get(url, payload) {
|
public async get(url): Promise<any> {
|
||||||
const fullUrl = new URL(url)
|
return this.fetch(url, {
|
||||||
for (const key of Object.keys(payload)) {
|
|
||||||
fullUrl.searchParams.append(key, payload[key])
|
|
||||||
}
|
|
||||||
return fetch(fullUrl, {
|
|
||||||
method: "GET",
|
method: "GET",
|
||||||
body: null,
|
|
||||||
headers: {
|
headers: {
|
||||||
"Content-type": "application/json",
|
"Content-type": "application/json",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async put(url, payload): Promise<any> {
|
||||||
|
return this.fetch(url, {
|
||||||
|
method: "PUT",
|
||||||
|
body: payload,
|
||||||
|
headers: {
|
||||||
|
"Content-type": "application/json",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
private async fetch(url, opts): Promise<any> {
|
||||||
|
return fetch(url, opts)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,9 @@
|
|||||||
import Logger from "../utils/Logger"
|
|
||||||
import AquariusConnector from "./AquariusConnector"
|
import AquariusConnector from "./AquariusConnector"
|
||||||
|
|
||||||
export default class AquariusConnectorProvider {
|
export default class AquariusConnectorProvider {
|
||||||
|
|
||||||
public static setConnector(connector: AquariusConnector) {
|
public static setConnector(connector: AquariusConnector) {
|
||||||
|
|
||||||
Logger.log("setting", typeof connector.constructor.name)
|
|
||||||
|
|
||||||
AquariusConnectorProvider.connector = connector
|
AquariusConnectorProvider.connector = connector
|
||||||
}
|
}
|
||||||
|
|
||||||
|
3
src/aquarius/query/Query.ts
Normal file
3
src/aquarius/query/Query.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export default class Query {
|
||||||
|
public value: number = 1
|
||||||
|
}
|
14
src/aquarius/query/SearchQuery.ts
Normal file
14
src/aquarius/query/SearchQuery.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import Query from "./Query"
|
||||||
|
import Sort from "./Sort"
|
||||||
|
|
||||||
|
export default class SearchQuery {
|
||||||
|
public offset: number = 100
|
||||||
|
public page: number = 0
|
||||||
|
public query: Query = {
|
||||||
|
value: 1,
|
||||||
|
} as Query
|
||||||
|
public sort: Sort = {
|
||||||
|
value: 1,
|
||||||
|
} as Sort
|
||||||
|
public text: string = "Office"
|
||||||
|
}
|
3
src/aquarius/query/Sort.ts
Normal file
3
src/aquarius/query/Sort.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export default class Sort {
|
||||||
|
public value: number = 1
|
||||||
|
}
|
@ -24,10 +24,12 @@ export default class DDO {
|
|||||||
private assa: string
|
private assa: string
|
||||||
|
|
||||||
public constructor(ddo?: {
|
public constructor(ddo?: {
|
||||||
|
id?: string,
|
||||||
publicKey?: PublicKey[],
|
publicKey?: PublicKey[],
|
||||||
authentication?: Authentication[],
|
authentication?: Authentication[],
|
||||||
service?: Service[],
|
service?: Service[],
|
||||||
}) {
|
}) {
|
||||||
|
this.id = ddo ? ddo.id ? ddo.id : null : null
|
||||||
this.publicKey = ddo ? ddo.publicKey ? ddo.publicKey : [] : []
|
this.publicKey = ddo ? ddo.publicKey ? ddo.publicKey : [] : []
|
||||||
this.authentication = ddo ? ddo.authentication ? ddo.authentication : [] : []
|
this.authentication = ddo ? ddo.authentication ? ddo.authentication : [] : []
|
||||||
this.service = ddo ? ddo.service ? ddo.service : [] : []
|
this.service = ddo ? ddo.service ? ddo.service : [] : []
|
||||||
|
@ -1,21 +1,20 @@
|
|||||||
import * as assert from "assert"
|
import * as assert from "assert"
|
||||||
import Aquarius from "../../src/aquarius/Aquarius"
|
import Aquarius from "../../src/aquarius/Aquarius"
|
||||||
import AquariusConnectorProvider from "../../src/aquarius/AquariusConnectorProvider"
|
import AquariusConnectorProvider from "../../src/aquarius/AquariusConnectorProvider"
|
||||||
|
import SearchQuery from "../../src/aquarius/query/SearchQuery"
|
||||||
|
import DDO from "../../src/ddo/DDO"
|
||||||
|
import IdGenerator from "../../src/ocean/IdGenerator"
|
||||||
import config from "../config"
|
import config from "../config"
|
||||||
import AquariusConnectorMock from "../mocks/AquariusConnector.mock"
|
import AquariusConnectorMock from "../mocks/AquariusConnector.mock"
|
||||||
|
// import * as jsonDDO from "../testdata/ddo.json"
|
||||||
|
|
||||||
describe("Aquarius", () => {
|
describe("Aquarius", () => {
|
||||||
|
|
||||||
before(() => {
|
const aquarius: Aquarius = new Aquarius(config)
|
||||||
AquariusConnectorProvider.setConnector(new AquariusConnectorMock())
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("#queryMetadata()", () => {
|
describe("#queryMetadata()", () => {
|
||||||
|
|
||||||
it("should query metadata", async () => {
|
it("should query metadata", async () => {
|
||||||
|
|
||||||
const aquarius: Aquarius = new Aquarius(config)
|
|
||||||
|
|
||||||
const query = {
|
const query = {
|
||||||
offset: 100,
|
offset: 100,
|
||||||
page: 0,
|
page: 0,
|
||||||
@ -26,12 +25,84 @@ describe("Aquarius", () => {
|
|||||||
value: 1,
|
value: 1,
|
||||||
},
|
},
|
||||||
text: "Office",
|
text: "Office",
|
||||||
}
|
} as SearchQuery
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
AquariusConnectorProvider.setConnector(new AquariusConnectorMock())
|
||||||
|
|
||||||
const result: any[] = await aquarius.queryMetadata(query)
|
const result: any[] = await aquarius.queryMetadata(query)
|
||||||
assert(result)
|
assert(result)
|
||||||
|
assert(result.length !== null)
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("#queryMetadataByText()", () => {
|
||||||
|
|
||||||
|
it("should query metadata by text", async () => {
|
||||||
|
|
||||||
|
const query = {
|
||||||
|
offset: 100,
|
||||||
|
page: 0,
|
||||||
|
query: {
|
||||||
|
value: 1,
|
||||||
|
},
|
||||||
|
sort: {
|
||||||
|
value: 1,
|
||||||
|
},
|
||||||
|
text: "Office",
|
||||||
|
} as SearchQuery
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
AquariusConnectorProvider.setConnector(new AquariusConnectorMock())
|
||||||
|
|
||||||
|
const result: any[] = await aquarius.queryMetadataByText(query)
|
||||||
|
assert(result)
|
||||||
|
assert(result.length !== null)
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("#storeDDO()", () => {
|
||||||
|
|
||||||
|
it("should store a ddo", async () => {
|
||||||
|
|
||||||
|
const did: string = `did:op:${IdGenerator.generateId()}`
|
||||||
|
const ddo: DDO = new DDO({
|
||||||
|
id: did,
|
||||||
|
})
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
|
||||||
|
|
||||||
|
const result: DDO = await aquarius.storeDDO(ddo)
|
||||||
|
assert(result)
|
||||||
|
assert(result.id === ddo.id)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("#retrieveDDO()", () => {
|
||||||
|
|
||||||
|
it("should store a ddo", async () => {
|
||||||
|
|
||||||
|
const did: string = `did:op:${IdGenerator.generateId()}`
|
||||||
|
const ddo: DDO = new DDO({
|
||||||
|
id: did,
|
||||||
|
})
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
|
||||||
|
|
||||||
|
const storageResult: DDO = await aquarius.storeDDO(ddo)
|
||||||
|
assert(storageResult)
|
||||||
|
|
||||||
|
assert(storageResult.id === did)
|
||||||
|
|
||||||
|
const restrieveResult: DDO = await aquarius.retrieveDDO(did)
|
||||||
|
assert(restrieveResult)
|
||||||
|
|
||||||
|
assert(restrieveResult.id === did)
|
||||||
|
assert(restrieveResult.id === storageResult.id)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,17 +1,22 @@
|
|||||||
import AquariusConnector from "../../src/aquarius/AquariusConnector"
|
import AquariusConnector from "../../src/aquarius/AquariusConnector"
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
export default class AquariusConnectorMock extends AquariusConnector {
|
export default class AquariusConnectorMock extends AquariusConnector {
|
||||||
|
|
||||||
public async post(url: string, payload: any) {
|
constructor(private returnData: any) {
|
||||||
|
super()
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
// @ts-ignore
|
||||||
ok: true,
|
private async fetch(url, opts): Promise<any> {
|
||||||
json: () => {
|
|
||||||
return []
|
return new Promise((resolve, reject) => {
|
||||||
},
|
resolve({
|
||||||
text: () => {
|
ok: true,
|
||||||
return ""
|
json: () => {
|
||||||
},
|
return this.returnData ? this.returnData : []
|
||||||
}
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user