diff --git a/api/api.d.ts b/api/api.d.ts index ec56d7b..7e584a6 100644 --- a/api/api.d.ts +++ b/api/api.d.ts @@ -76,6 +76,8 @@ declare type HTTPOptions = { } declare interface HTTPService { + request(method: HTTPMethod, url: string): void; + request(method: HTTPMethod, url: string, options: HTTPOptions): void; request(method: HTTPMethod, url: string, cb: (response: string) => void): void; request(method: HTTPMethod, url: string, options: HTTPOptions, cb: (response: string) => void): void; } diff --git a/app/src/main/java/net/pipe01/pinepartner/scripting/api/HTTPService.kt b/app/src/main/java/net/pipe01/pinepartner/scripting/api/HTTPService.kt index 4f6a1b3..86138fe 100644 --- a/app/src/main/java/net/pipe01/pinepartner/scripting/api/HTTPService.kt +++ b/app/src/main/java/net/pipe01/pinepartner/scripting/api/HTTPService.kt @@ -2,6 +2,7 @@ package net.pipe01.pinepartner.scripting.api import fuel.Fuel import fuel.method +import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withTimeout import org.mozilla.javascript.Context import org.mozilla.javascript.Function @@ -11,39 +12,64 @@ import org.mozilla.javascript.annotations.JSFunction class HTTPService : ApiScriptableObject(HTTPService::class) { @JSFunction - fun request(method: String, url: String, arg1: Any, arg2: Any?) { - val options: NativeObject? - val cb: Function + fun request(method: String, url: String, arg1: Any, arg2: Any): Any? { + var options: NativeObject? = null + var cb: Function? = null - if (Undefined.isUndefined(arg2)) { - options = null - cb = arg1 as? Function ?: throw Context.throwAsScriptRuntimeEx(IllegalArgumentException("Invalid callback")) + if (!Undefined.isUndefined(arg1)) { + when (arg1) { + is NativeObject -> { + options = arg1 + } + is Function -> { + cb = arg1 + } + else -> { + throw Context.throwAsScriptRuntimeEx(IllegalArgumentException("Invalid argument")) + } + } + } + if (!Undefined.isUndefined(arg2)) { + if (arg2 is Function) { + cb = arg2 + } else { + throw Context.throwAsScriptRuntimeEx(IllegalArgumentException("Invalid argument")) + } + } + + return if (cb != null) { + launch { + val resp = doRequest(method, url, options) + + enterAndCall(cb) { arrayOf(resp) } + } + + Undefined.instance } else { - options = arg1 as? NativeObject ?: throw Context.throwAsScriptRuntimeEx(IllegalArgumentException("Invalid options")) - cb = arg2 as? Function ?: throw Context.throwAsScriptRuntimeEx(IllegalArgumentException("Invalid callback")) + runBlocking { doRequest(method, url, options) } } + } - launch { - val headers = options?.get("headers")?.let { - val headers = it as? NativeObject ?: throw Context.throwAsScriptRuntimeEx(IllegalArgumentException("Invalid headers")) - val map = mutableMapOf() + private suspend fun doRequest(method: String, url: String, options: NativeObject?): String { + val headers = options?.get("headers")?.let { + val headers = it as? NativeObject ?: throw Context.throwAsScriptRuntimeEx(IllegalArgumentException("Invalid headers")) + val map = mutableMapOf() - for (key in headers.keys) { - map[key as String] = headers[key] as String - } + for (key in headers.keys) { + map[key as String] = headers[key] as String + } - map - } ?: emptyMap() + map + } ?: emptyMap() - val timeout = options?.get("timeout")?.let { - if (it as? Double != null) it.toLong() else throw Context.throwAsScriptRuntimeEx(IllegalArgumentException("Invalid timeout")) - } ?: 5000 + val timeout = options?.get("timeout")?.let { + if (it as? Double != null) it.toLong() else throw Context.throwAsScriptRuntimeEx(IllegalArgumentException("Invalid timeout")) + } ?: 5000 - withTimeout(timeout) { - val resp = Fuel.method(url, method = method, headers = headers) + return withTimeout(timeout) { + val resp = Fuel.method(url, method = method, headers = headers) - enterAndCall(cb) { arrayOf(resp.body) } - } + resp.body } } } \ No newline at end of file