mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
Decoupled Aquarius class from Ocean class
and removed inheritance from Instantiable Signed-off-by: Pablo F. Mescher <pfmescher@gmail.com>
This commit is contained in:
parent
ca67de1254
commit
550ec76a35
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@oceanprotocol/squid",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.2",
|
||||
"description": "JavaScript client library for Ocean Protocol",
|
||||
"main": "./dist/node/squid.js",
|
||||
"typings": "./dist/node/squid.d.ts",
|
||||
|
@ -1,7 +1,8 @@
|
||||
import { URL } from 'whatwg-url'
|
||||
import { DDO } from '../ddo/DDO'
|
||||
import DID from '../ocean/DID'
|
||||
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
|
||||
import { Logger } from '../utils'
|
||||
import { WebServiceConnector } from '../ocean/utils/WebServiceConnector'
|
||||
|
||||
const apiPath = '/api/v1/aquarius/assets/ddo'
|
||||
|
||||
@ -24,22 +25,27 @@ export interface SearchQuery {
|
||||
* Provides a interface with Aquarius.
|
||||
* Aquarius provides an off-chain database store for metadata about data assets.
|
||||
*/
|
||||
export class Aquarius extends Instantiable {
|
||||
export class Aquarius {
|
||||
private fetch: WebServiceConnector
|
||||
private logger: Logger
|
||||
private aquariusUri: string
|
||||
|
||||
private get url() {
|
||||
return this.config.aquariusUri
|
||||
return this.aquariusUri
|
||||
}
|
||||
|
||||
constructor(config: InstantiableConfig) {
|
||||
super()
|
||||
this.setInstanceConfig(config)
|
||||
constructor(aquariusUri: string, logger?: Logger) {
|
||||
this.fetch = new WebServiceConnector(logger)
|
||||
this.logger = logger
|
||||
this.aquariusUri = aquariusUri
|
||||
}
|
||||
|
||||
public async getVersionInfo() {
|
||||
return (await this.ocean.utils.fetch.get(this.url)).json()
|
||||
return (await this.fetch.get(this.url)).json()
|
||||
}
|
||||
|
||||
public async getAccessUrl(accessToken: any, payload: any): Promise<string> {
|
||||
const accessUrl: string = await this.ocean.utils.fetch
|
||||
const accessUrl: string = await this.fetch
|
||||
.post(`${accessToken.service_endpoint}/${accessToken.resource_id}`, payload)
|
||||
.then((response: any): string => {
|
||||
if (response.ok) {
|
||||
@ -69,7 +75,7 @@ export class Aquarius extends Instantiable {
|
||||
* @return {Promise<QueryResult>}
|
||||
*/
|
||||
public async queryMetadata(query: SearchQuery): Promise<QueryResult> {
|
||||
const result: QueryResult = await this.ocean.utils.fetch
|
||||
const result: QueryResult = await this.fetch
|
||||
.post(`${this.url}${apiPath}/query`, JSON.stringify(query))
|
||||
.then((response: any) => {
|
||||
if (response.ok) {
|
||||
@ -107,7 +113,8 @@ export class Aquarius extends Instantiable {
|
||||
)
|
||||
fullUrl.searchParams.append('offset', query.offset.toString())
|
||||
fullUrl.searchParams.append('page', query.page.toString())
|
||||
const result: QueryResult = await this.ocean.utils.fetch
|
||||
|
||||
const result: QueryResult = await this.fetch
|
||||
.get(fullUrl)
|
||||
.then((response: any) => {
|
||||
if (response.ok) {
|
||||
@ -138,7 +145,7 @@ export class Aquarius extends Instantiable {
|
||||
*/
|
||||
public async storeDDO(ddo: DDO): Promise<DDO> {
|
||||
const fullUrl = `${this.url}${apiPath}`
|
||||
const result: DDO = await this.ocean.utils.fetch
|
||||
const result: DDO = await this.fetch
|
||||
.post(fullUrl, DDO.serialize(ddo))
|
||||
.then((response: any) => {
|
||||
if (response.ok) {
|
||||
@ -174,7 +181,7 @@ export class Aquarius extends Instantiable {
|
||||
): Promise<DDO> {
|
||||
did = did && DID.parse(did)
|
||||
const fullUrl = metadataServiceEndpoint || `${this.url}${apiPath}/${did.getDid()}`
|
||||
const result = await this.ocean.utils.fetch
|
||||
const result = await this.fetch
|
||||
.get(fullUrl)
|
||||
.then((response: any) => {
|
||||
if (response.ok) {
|
||||
|
@ -37,10 +37,11 @@ export class Ocean extends Instantiable {
|
||||
}
|
||||
instance.setInstanceConfig(instanceConfig)
|
||||
|
||||
instance.utils = await OceanUtils.getInstance(instanceConfig)
|
||||
instance.keeper = await Keeper.getInstance(instanceConfig)
|
||||
|
||||
instance.brizo = new Brizo(instanceConfig)
|
||||
instance.aquarius = new Aquarius(instanceConfig)
|
||||
instance.aquarius = new Aquarius(instanceConfig.config.aquariusUri, instanceConfig.logger)
|
||||
|
||||
instance.accounts = await OceanAccounts.getInstance(instanceConfig)
|
||||
instance.auth = await OceanAuth.getInstance(instanceConfig)
|
||||
@ -50,8 +51,6 @@ export class Ocean extends Instantiable {
|
||||
instance.tokens = await OceanTokens.getInstance(instanceConfig)
|
||||
instance.versions = await OceanVersions.getInstance(instanceConfig)
|
||||
|
||||
instance.utils = await OceanUtils.getInstance(instanceConfig)
|
||||
|
||||
return instance
|
||||
}
|
||||
|
||||
|
@ -16,9 +16,9 @@ export class OceanUtils extends Instantiable {
|
||||
const instance = new OceanUtils()
|
||||
instance.setInstanceConfig(config)
|
||||
|
||||
instance.agreements = new ServiceAgreement(config)
|
||||
instance.signature = new SignatureUtils(config)
|
||||
instance.fetch = new WebServiceConnector(config)
|
||||
instance.agreements = new ServiceAgreement(config.ocean, config.logger, config.web3)
|
||||
instance.signature = new SignatureUtils(config.web3, config.logger)
|
||||
instance.fetch = new WebServiceConnector(config.logger)
|
||||
|
||||
return instance
|
||||
}
|
||||
|
@ -2,13 +2,18 @@ import { ServiceAgreementTemplateCondition } from '../../ddo/ServiceAgreementTem
|
||||
import { DDO } from '../../ddo/DDO'
|
||||
import { ServiceAccess } from '../../ddo/Service'
|
||||
import Account from '../Account'
|
||||
import { zeroX } from '../../utils'
|
||||
import { Instantiable, InstantiableConfig } from '../../Instantiable.abstract'
|
||||
import { zeroX, Logger } from '../../utils'
|
||||
import { Ocean } from '../../squid'
|
||||
import Web3 from 'web3'
|
||||
|
||||
export class ServiceAgreement extends Instantiable {
|
||||
constructor(config: InstantiableConfig) {
|
||||
super()
|
||||
this.setInstanceConfig(config)
|
||||
export class ServiceAgreement {
|
||||
private ocean: Ocean
|
||||
private logger: Logger
|
||||
private web3: Web3
|
||||
constructor(ocean: Ocean, logger: Logger, web3: Web3) {
|
||||
this.ocean = ocean
|
||||
this.logger = logger
|
||||
this.web3 = web3
|
||||
}
|
||||
|
||||
public async signServiceAgreement(
|
||||
|
@ -1,9 +1,13 @@
|
||||
import { Instantiable, InstantiableConfig } from '../../Instantiable.abstract'
|
||||
import Web3 from 'web3'
|
||||
import { Logger } from '../../utils'
|
||||
|
||||
export class SignatureUtils extends Instantiable {
|
||||
constructor(config: InstantiableConfig) {
|
||||
super()
|
||||
this.setInstanceConfig(config)
|
||||
export class SignatureUtils {
|
||||
private web3: Web3
|
||||
private logger: Logger;
|
||||
|
||||
constructor(web3: Web3, logger: Logger) {
|
||||
this.web3 = web3
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public async signText(
|
||||
|
@ -1,17 +1,17 @@
|
||||
import { BodyInit, RequestInit, Response } from 'node-fetch'
|
||||
import fs from 'fs'
|
||||
import { Instantiable, InstantiableConfig } from '../../Instantiable.abstract'
|
||||
|
||||
const fetch = require('node-fetch')
|
||||
import save = require('save-file')
|
||||
import { Logger } from '../../utils'
|
||||
|
||||
/**
|
||||
* Provides a common interface to web services.
|
||||
*/
|
||||
export class WebServiceConnector extends Instantiable {
|
||||
constructor(config: InstantiableConfig) {
|
||||
super()
|
||||
this.setInstanceConfig(config)
|
||||
export class WebServiceConnector {
|
||||
public logger: Logger;
|
||||
constructor(logger: Logger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public post(url: string, payload: BodyInit): Promise<Response> {
|
||||
|
@ -4,6 +4,7 @@ import DID from './ocean/DID'
|
||||
import { Ocean } from './ocean/Ocean'
|
||||
import { LoggerInstance as Logger } from './utils/Logger'
|
||||
import Keeper from './keeper/Keeper'
|
||||
import { Aquarius } from './aquarius/Aquarius'
|
||||
|
||||
import * as templates from './keeper/contracts/templates'
|
||||
import * as conditions from './keeper/contracts/conditions'
|
||||
@ -24,4 +25,4 @@ export {
|
||||
export { AgreementTemplate } from './keeper/contracts/templates'
|
||||
export { Condition, ConditionState } from './keeper/contracts/conditions'
|
||||
|
||||
export { Ocean, Account, Config, DID, Logger, Keeper, conditions, templates, utils }
|
||||
export { Ocean, Account, Config, DID, Logger, Keeper, Aquarius, conditions, templates, utils }
|
||||
|
@ -97,6 +97,18 @@ describe('Aquarius', () => {
|
||||
assert.equal(result.totalResults, 1)
|
||||
})
|
||||
|
||||
it('should query metadata by text with a new instance', async () => {
|
||||
spy.on(ocean.utils.fetch, 'get', () => reponsify(getResults([new DDO()])))
|
||||
|
||||
const aquariusNew = new Aquarius(config.aquariusUri, ocean.utils, null)
|
||||
const result = await aquariusNew.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 () => {
|
||||
spy.on(ocean.utils.fetch, 'get', () => reponsify(getResults([new DDO()])))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user