diff --git a/src/aquarius/Aquarius.ts b/src/aquarius/Aquarius.ts index 0ece540..8e0a971 100644 --- a/src/aquarius/Aquarius.ts +++ b/src/aquarius/Aquarius.ts @@ -1,7 +1,6 @@ import { URL } from "whatwg-url" import { DDO } from "../ddo/DDO" import DID from "../ocean/DID" -import WebServiceConnectorProvider from "../utils/WebServiceConnectorProvider" import { Instantiable, InstantiableConfig } from "../Instantiable.abstract" const apiPath = "/api/v1/aquarius/assets/ddo" @@ -36,7 +35,7 @@ export class Aquarius extends Instantiable { } public async getAccessUrl(accessToken: any, payload: any): Promise { - const accessUrl: string = await WebServiceConnectorProvider.getConnector() + const accessUrl: string = await this.ocean.utils.fetch .post(`${accessToken.service_endpoint}/${accessToken.resource_id}`, payload) .then((response: any): string => { if (response.ok) { @@ -63,7 +62,7 @@ export class Aquarius extends Instantiable { * @return {Promise} */ public async queryMetadata(query: SearchQuery): Promise { - const result: QueryResult = await WebServiceConnectorProvider.getConnector() + const result: QueryResult = await this.ocean.utils.fetch .post(`${this.url}${apiPath}/query`, JSON.stringify(query)) .then((response: any) => { if (response.ok) { @@ -94,7 +93,7 @@ export class Aquarius extends Instantiable { 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: QueryResult = await WebServiceConnectorProvider.getConnector() + const result: QueryResult = await this.ocean.utils.fetch .get(fullUrl) .then((response: any) => { if (response.ok) { @@ -121,7 +120,7 @@ export class Aquarius extends Instantiable { */ public async storeDDO(ddo: DDO): Promise { const fullUrl = `${this.url}${apiPath}` - const result: DDO = await WebServiceConnectorProvider.getConnector() + const result: DDO = await this.ocean.utils.fetch .post(fullUrl, DDO.serialize(ddo)) .then((response: any) => { if (response.ok) { @@ -148,7 +147,7 @@ export class Aquarius extends Instantiable { */ public async retrieveDDO(did: DID): Promise { const fullUrl = `${this.url}${apiPath}/${did.getDid()}` - const result = await WebServiceConnectorProvider.getConnector() + const result = await this.ocean.utils.fetch .get(fullUrl) .then((response: any) => { if (response.ok) { diff --git a/src/brizo/Brizo.ts b/src/brizo/Brizo.ts index 287be0e..30f1501 100644 --- a/src/brizo/Brizo.ts +++ b/src/brizo/Brizo.ts @@ -4,7 +4,6 @@ import save = require("save-file") import { File } from "../ddo/MetaData" import Account from "../ocean/Account" import { noZeroX } from "../utils" -import WebServiceConnectorProvider from "../utils/WebServiceConnectorProvider" import { Instantiable, InstantiableConfig } from "../Instantiable.abstract" const apiPath = "/api/v1/brizo/services" @@ -57,8 +56,7 @@ export class Brizo extends Instantiable { } try { - return await WebServiceConnectorProvider - .getConnector() + return await this.ocean.utils.fetch .post( this.getPurchaseEndpoint(), decodeURI(JSON.stringify(args)), @@ -117,8 +115,7 @@ export class Brizo extends Instantiable { } try { - const response = await WebServiceConnectorProvider - .getConnector() + const response = await this.ocean.utils.fetch .post( this.getEncryptEndpoint(), decodeURI(JSON.stringify(args)), @@ -134,8 +131,7 @@ export class Brizo extends Instantiable { } private async downloadFile(url: string, destination?: string): Promise { - const response = await WebServiceConnectorProvider - .getConnector() + const response = await this.ocean.utils.fetch .get(url) if (!response.ok) { throw new Error("Response error.") diff --git a/src/ocean/utils/OceanUtils.ts b/src/ocean/utils/OceanUtils.ts index 5b7d6df..ef48795 100644 --- a/src/ocean/utils/OceanUtils.ts +++ b/src/ocean/utils/OceanUtils.ts @@ -2,6 +2,7 @@ import { Instantiable, InstantiableConfig } from "../../Instantiable.abstract" import { ServiceAgreement } from "./ServiceAgreement" import { SignatureUtils } from "./SignatureUtils" +import { WebServiceConnector } from "./WebServiceConnector" /** * Utils internal submodule of Ocean Protocol. @@ -18,6 +19,7 @@ export class OceanUtils extends Instantiable { instance.agreements = new ServiceAgreement(config) instance.signature = new SignatureUtils(config) + instance.fetch = new WebServiceConnector(config) return instance } @@ -33,4 +35,10 @@ export class OceanUtils extends Instantiable { * @type {SignatureUtils} */ public signature: SignatureUtils + + /** + * Fetch utils. + * @type {WebServiceConnector} + */ + public fetch: WebServiceConnector } diff --git a/src/utils/WebServiceConnector.ts b/src/ocean/utils/WebServiceConnector.ts similarity index 73% rename from src/utils/WebServiceConnector.ts rename to src/ocean/utils/WebServiceConnector.ts index f56e338..3dc4a14 100644 --- a/src/utils/WebServiceConnector.ts +++ b/src/ocean/utils/WebServiceConnector.ts @@ -1,10 +1,15 @@ import fetch, { BodyInit, RequestInit, Response } from "node-fetch" -import LoggerInstance from "./Logger" +import { Instantiable, InstantiableConfig } from "../../Instantiable.abstract" /** * Provides a common interface to web services. */ -export default class WebServiceConnector { +export class WebServiceConnector extends Instantiable { + + constructor(config: InstantiableConfig) { + super() + this.setInstanceConfig(config) + } public post(url: string, payload: BodyInit): Promise { return this.fetch(url, { @@ -38,8 +43,8 @@ export default class WebServiceConnector { private async fetch(url: string, opts: RequestInit): Promise { const result = await fetch(url, opts) if (!result.ok) { - LoggerInstance.error(`Error requesting [${opts.method}] ${url}`) - LoggerInstance.error(`Response message: \n${await result.text()}`) + this.logger.error(`Error requesting [${opts.method}] ${url}`) + this.logger.error(`Response message: \n${await result.text()}`) throw result } return result diff --git a/src/squid.ts b/src/squid.ts index 4a168dd..61b909f 100644 --- a/src/squid.ts +++ b/src/squid.ts @@ -3,7 +3,6 @@ import Account from "./ocean/Account" import DID from "./ocean/DID" import { Ocean } from "./ocean/Ocean" import { LoggerInstance as Logger} from "./utils/Logger" -import WebServiceConnectorProvider from "./utils/WebServiceConnectorProvider" import Keeper from "./keeper/Keeper" import * as templates from "./keeper/contracts/templates" @@ -25,7 +24,6 @@ export { DID, Logger, Keeper, - WebServiceConnectorProvider, conditions, templates, diff --git a/src/utils/WebServiceConnectorProvider.ts b/src/utils/WebServiceConnectorProvider.ts deleted file mode 100644 index 2078d6e..0000000 --- a/src/utils/WebServiceConnectorProvider.ts +++ /dev/null @@ -1,19 +0,0 @@ -import WebServiceConnector from "./WebServiceConnector" - -export default class WebServiceConnectorProvider { - - public static setConnector(connector: WebServiceConnector) { - - WebServiceConnectorProvider.instance = connector - } - - public static getConnector(): WebServiceConnector { - - if (!WebServiceConnectorProvider.instance) { - WebServiceConnectorProvider.instance = new WebServiceConnector() - } - return WebServiceConnectorProvider.instance - } - - private static instance: WebServiceConnector = null -}