2020-09-17 09:44:48 +00:00
|
|
|
import * as querystring from 'querystring'
|
|
|
|
|
2020-02-29 14:28:43 +00:00
|
|
|
const URL = Java.type('java.net.URL')
|
2020-09-17 09:44:48 +00:00
|
|
|
const Paths = Java.type('java.nio.file.Paths')
|
|
|
|
const Files = Java.type('java.nio.file.Files')
|
|
|
|
const StandardCopyOption = Java.type('java.nio.file.StandardCopyOption')
|
2020-02-29 14:28:43 +00:00
|
|
|
|
2019-11-04 12:19:50 +00:00
|
|
|
export type Method =
|
|
|
|
| 'get' | 'GET'
|
|
|
|
| 'delete' | 'DELETE'
|
|
|
|
| 'head' | 'HEAD'
|
|
|
|
| 'options' | 'OPTIONS'
|
|
|
|
| 'post' | 'POST'
|
|
|
|
| 'put' | 'PUT'
|
|
|
|
| 'patch' | 'PATCH'
|
|
|
|
|
|
|
|
interface RequestConfig {
|
2020-09-17 09:44:48 +00:00
|
|
|
url?: string
|
|
|
|
method?: Method
|
|
|
|
headers?: { [key: string]: string }
|
|
|
|
params?: { [key: string]: string }
|
|
|
|
data?: any
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 12:19:50 +00:00
|
|
|
function request(config: RequestConfig) {
|
2020-02-01 17:58:07 +00:00
|
|
|
// @ts-ignore
|
2020-09-17 09:44:48 +00:00
|
|
|
let xhr = new XMLHttpRequest()
|
|
|
|
xhr.open(config.method, config.url, false)
|
2019-11-04 12:19:50 +00:00
|
|
|
for (const header in config.headers) {
|
2020-09-17 09:44:48 +00:00
|
|
|
xhr.setRequestHeader(header, config.headers[header])
|
|
|
|
}
|
|
|
|
let body = config.data
|
|
|
|
if (body && typeof body !== "string") {
|
|
|
|
switch (config.headers['Content-Type']) {
|
|
|
|
case "application/json":
|
|
|
|
body = JSON.stringify(body)
|
|
|
|
break
|
|
|
|
case "application/x-www-form-urlencoded":
|
|
|
|
body = querystring.encode(body)
|
|
|
|
break
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
2020-09-17 09:44:48 +00:00
|
|
|
xhr.send(body)
|
|
|
|
if (xhr.getResponseHeader("Content-Type").indexOf('application/json') != -1) {
|
2020-02-23 14:33:51 +00:00
|
|
|
xhr.responseType = "json"
|
|
|
|
}
|
2020-09-17 09:44:48 +00:00
|
|
|
return xhr.get()
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 14:28:43 +00:00
|
|
|
function download(url: string, target: string) {
|
2020-03-01 12:28:16 +00:00
|
|
|
console.debug(`Start Download file ${target} from ${url}....`)
|
2020-09-17 09:44:48 +00:00
|
|
|
Files.copy(new URL(url).openStream(), Paths.get(target), StandardCopyOption.REPLACE_EXISTING)
|
2020-03-01 12:28:16 +00:00
|
|
|
console.debug(`File ${target} Download Complate...`)
|
2020-02-29 14:28:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 12:19:50 +00:00
|
|
|
function _proxy(method: Method) {
|
2020-09-17 09:44:48 +00:00
|
|
|
return function (url: string, data?: any, config: RequestConfig = {}) {
|
|
|
|
if (!config.headers) { config.headers = {} }
|
|
|
|
config.headers['Content-Type'] = config.headers['Content-Type'] ?? 'application/json'
|
|
|
|
return request({ url, method, data, ...config })
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 12:19:50 +00:00
|
|
|
export default {
|
|
|
|
get: _proxy('GET'),
|
|
|
|
post: _proxy('POST'),
|
2020-02-29 14:28:43 +00:00
|
|
|
request,
|
|
|
|
download
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|