mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
Move WebServiceConnector to utils and make it instantiable.
This commit is contained in:
parent
f8606bb9aa
commit
d779adc0a3
@ -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<string> {
|
||||
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<QueryResult>}
|
||||
*/
|
||||
public async queryMetadata(query: SearchQuery): Promise<QueryResult> {
|
||||
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<DDO> {
|
||||
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<DDO> {
|
||||
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) {
|
||||
|
@ -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<string> {
|
||||
const response = await WebServiceConnectorProvider
|
||||
.getConnector()
|
||||
const response = await this.ocean.utils.fetch
|
||||
.get(url)
|
||||
if (!response.ok) {
|
||||
throw new Error("Response error.")
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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<Response> {
|
||||
return this.fetch(url, {
|
||||
@ -38,8 +43,8 @@ export default class WebServiceConnector {
|
||||
private async fetch(url: string, opts: RequestInit): Promise<Response> {
|
||||
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
|
@ -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,
|
||||
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user