1
0
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:
Sebastian Gerske 2018-11-01 12:47:48 +01:00
parent 787ed9dcf3
commit 093a6ea4af
9 changed files with 182 additions and 39 deletions

View File

@ -1,6 +1,9 @@
import {URL} from "url"
import DDO from "../ddo/DDO"
import Config from "../models/Config"
import Logger from "../utils/Logger"
import AquariusConnectorProvider from "./AquariusConnectorProvider"
import SearchQuery from "./query/SearchQuery"
export default class Aquarius {
@ -11,7 +14,7 @@ export default class Aquarius {
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(
`${accessToken.service_endpoint}/${accessToken.resource_id}`,
@ -33,11 +36,10 @@ export default class Aquarius {
return accessUrl
}
public async queryMetadata(query): Promise<any[]> {
public async queryMetadata(query: SearchQuery): Promise<any[]> {
const result = await AquariusConnectorProvider.getConnector().post(
this.url + "/api/v1/aquarius/assets/ddo/query",
JSON.stringify(query))
const result = await AquariusConnectorProvider.getConnector()
.post(this.url + "/api/v1/aquarius/assets/ddo/query", JSON.stringify(query))
.then((response: any) => {
if (response.ok) {
return response.json()
@ -51,11 +53,15 @@ export default class Aquarius {
return result
}
public async queryMetadataByText(query): Promise<any[]> {
public async queryMetadataByText(query: SearchQuery): Promise<any[]> {
const result = await AquariusConnectorProvider.getConnector().get(
this.url + "/api/v1/aquarius/assets/ddo/query",
JSON.stringify(query))
const fullUrl = new URL(this.url + "/api/v1/aquarius/assets/ddo/query")
fullUrl.searchParams.append("text", query.text)
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) => {
if (response.ok) {
return response.json()
@ -68,4 +74,38 @@ export default class Aquarius {
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
}
}

View File

@ -1,10 +1,9 @@
import fetch from "node-fetch"
import { URL } from "url"
export default class AquariusConnector {
public post(url, payload) {
return fetch(url, {
public async post(url, payload): Promise<any> {
return this.fetch(url, {
method: "POST",
body: payload,
headers: {
@ -13,17 +12,26 @@ export default class AquariusConnector {
})
}
public get(url, payload) {
const fullUrl = new URL(url)
for (const key of Object.keys(payload)) {
fullUrl.searchParams.append(key, payload[key])
}
return fetch(fullUrl, {
public async get(url): Promise<any> {
return this.fetch(url, {
method: "GET",
body: null,
headers: {
"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)
}
}

View File

@ -1,12 +1,9 @@
import Logger from "../utils/Logger"
import AquariusConnector from "./AquariusConnector"
export default class AquariusConnectorProvider {
public static setConnector(connector: AquariusConnector) {
Logger.log("setting", typeof connector.constructor.name)
AquariusConnectorProvider.connector = connector
}

View File

@ -0,0 +1,3 @@
export default class Query {
public value: number = 1
}

View 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"
}

View File

@ -0,0 +1,3 @@
export default class Sort {
public value: number = 1
}

View File

@ -24,10 +24,12 @@ export default class DDO {
private assa: string
public constructor(ddo?: {
id?: string,
publicKey?: PublicKey[],
authentication?: Authentication[],
service?: Service[],
}) {
this.id = ddo ? ddo.id ? ddo.id : null : null
this.publicKey = ddo ? ddo.publicKey ? ddo.publicKey : [] : []
this.authentication = ddo ? ddo.authentication ? ddo.authentication : [] : []
this.service = ddo ? ddo.service ? ddo.service : [] : []

View File

@ -1,21 +1,20 @@
import * as assert from "assert"
import Aquarius from "../../src/aquarius/Aquarius"
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 AquariusConnectorMock from "../mocks/AquariusConnector.mock"
// import * as jsonDDO from "../testdata/ddo.json"
describe("Aquarius", () => {
before(() => {
AquariusConnectorProvider.setConnector(new AquariusConnectorMock())
})
const aquarius: Aquarius = new Aquarius(config)
describe("#queryMetadata()", () => {
it("should query metadata", async () => {
const aquarius: Aquarius = new Aquarius(config)
const query = {
offset: 100,
page: 0,
@ -26,12 +25,84 @@ describe("Aquarius", () => {
value: 1,
},
text: "Office",
}
} as SearchQuery
// @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock())
const result: any[] = await aquarius.queryMetadata(query)
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)
})
})
})

View File

@ -1,17 +1,22 @@
import AquariusConnector from "../../src/aquarius/AquariusConnector"
// @ts-ignore
export default class AquariusConnectorMock extends AquariusConnector {
public async post(url: string, payload: any) {
constructor(private returnData: any) {
super()
}
return {
ok: true,
json: () => {
return []
},
text: () => {
return ""
},
}
// @ts-ignore
private async fetch(url, opts): Promise<any> {
return new Promise((resolve, reject) => {
resolve({
ok: true,
json: () => {
return this.returnData ? this.returnData : []
},
})
})
}
}