Skip to content

Commit

Permalink
Overload HTTP request method
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Mar 23, 2024
1 parent f221aae commit ba8116c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
2 changes: 2 additions & 0 deletions api/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String, String>()
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<String, String>()

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
}
}
}

0 comments on commit ba8116c

Please sign in to comment.