1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
squid-js/src/ocean/utils/WebServiceConnector.ts

88 lines
2.6 KiB
TypeScript
Raw Normal View History

2019-11-11 12:27:18 +01:00
import { BodyInit, RequestInit, Response } from 'node-fetch'
import fs from 'fs'
2019-06-20 00:20:09 +02:00
import { Instantiable, InstantiableConfig } from '../../Instantiable.abstract'
2018-10-26 11:57:26 +02:00
2019-11-11 12:27:18 +01:00
const fetch = require('node-fetch')
2019-07-12 16:56:01 +02:00
import save = require('save-file')
/**
* Provides a common interface to web services.
*/
export class WebServiceConnector extends Instantiable {
constructor(config: InstantiableConfig) {
super()
this.setInstanceConfig(config)
}
2018-10-26 11:57:26 +02:00
public post(url: string, payload: BodyInit): Promise<Response> {
2018-11-01 12:47:48 +01:00
return this.fetch(url, {
2019-06-20 00:20:09 +02:00
method: 'POST',
2018-10-26 11:57:26 +02:00
body: payload,
2018-11-16 10:03:16 +01:00
headers: {
2019-06-20 00:20:09 +02:00
'Content-type': 'application/json'
}
2018-10-26 11:57:26 +02:00
})
}
2018-10-29 16:38:23 +01:00
public get(url: string): Promise<Response> {
2018-11-01 12:47:48 +01:00
return this.fetch(url, {
2019-06-20 00:20:09 +02:00
method: 'GET',
2018-11-16 10:03:16 +01:00
headers: {
2019-06-20 00:20:09 +02:00
'Content-type': 'application/json'
}
2018-10-29 16:38:23 +01:00
})
}
2018-11-01 12:47:48 +01:00
public put(url: string, payload: BodyInit): Promise<Response> {
2018-11-01 12:47:48 +01:00
return this.fetch(url, {
2019-06-20 00:20:09 +02:00
method: 'PUT',
2018-11-01 12:47:48 +01:00
body: payload,
2018-11-16 10:03:16 +01:00
headers: {
2019-06-20 00:20:09 +02:00
'Content-type': 'application/json'
}
2018-11-01 12:47:48 +01:00
})
}
2019-09-09 12:18:54 +02:00
public async downloadFile(url: string, destination?: string, index?: number): Promise<string> {
2019-07-11 15:54:21 +02:00
const response = await this.get(url)
if (!response.ok) {
throw new Error('Response error.')
}
2019-11-11 12:27:18 +01:00
let filename: string
2019-07-11 15:54:21 +02:00
try {
2019-09-09 12:18:54 +02:00
filename = response.headers.get('content-disposition').match(/attachment;filename=(.+)/)[1]
2019-07-11 15:54:21 +02:00
} catch {
try {
filename = url.split('/').pop()
} catch {
filename = `file${index}`
}
}
if (destination) {
2019-07-31 13:02:04 +02:00
// eslint-disable-next-line no-async-promise-executor
2019-07-11 15:54:21 +02:00
await new Promise(async (resolve, reject) => {
fs.mkdirSync(destination, { recursive: true })
2019-09-09 12:18:54 +02:00
const fileStream = fs.createWriteStream(`${destination}${filename}`)
2019-07-11 15:54:21 +02:00
response.body.pipe(fileStream)
response.body.on('error', reject)
fileStream.on('finish', resolve)
})
return destination
} else {
save(await response.arrayBuffer(), filename)
}
}
private async fetch(url: string, opts: RequestInit): Promise<Response> {
2019-01-24 10:58:05 +01:00
const result = await fetch(url, opts)
if (!result.ok) {
this.logger.error(`Error requesting [${opts.method}] ${url}`)
this.logger.error(`Response message: \n${await result.text()}`)
throw result
}
return result
2018-11-01 12:47:48 +01:00
}
2018-10-26 11:57:26 +02:00
}