|
|
@ -34,9 +34,10 @@ suspend fun apiRequest(
|
|
|
|
) = withContext(Dispatchers.IO) {
|
|
|
|
) = withContext(Dispatchers.IO) {
|
|
|
|
val url = URL(SERVER + uri)
|
|
|
|
val url = URL(SERVER + uri)
|
|
|
|
with(url.openConnection() as HttpURLConnection) {
|
|
|
|
with(url.openConnection() as HttpURLConnection) {
|
|
|
|
|
|
|
|
connectTimeout = 3000
|
|
|
|
requestMethod = method
|
|
|
|
requestMethod = method
|
|
|
|
doInput = true
|
|
|
|
doInput = true
|
|
|
|
if (method == "POST" || method == "PUT") {
|
|
|
|
if (method == "POST" || method == "PUT" || method == "PATCH") {
|
|
|
|
setRequestProperty("Content-Type", "application/json")
|
|
|
|
setRequestProperty("Content-Type", "application/json")
|
|
|
|
doOutput = true
|
|
|
|
doOutput = true
|
|
|
|
val data = when (body) {
|
|
|
|
val data = when (body) {
|
|
|
@ -51,17 +52,22 @@ suspend fun apiRequest(
|
|
|
|
wr.write(data)
|
|
|
|
wr.write(data)
|
|
|
|
wr.flush()
|
|
|
|
wr.flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (responseCode >= 400) {
|
|
|
|
|
|
|
|
BufferedReader(InputStreamReader(errorStream)).use {
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (responseCode >= 400) {
|
|
|
|
|
|
|
|
BufferedReader(InputStreamReader(errorStream)).use {
|
|
|
|
|
|
|
|
val response = it.readText()
|
|
|
|
|
|
|
|
onFail?.invoke(response)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return@with
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//response
|
|
|
|
|
|
|
|
BufferedReader(InputStreamReader(inputStream)).use {
|
|
|
|
val response = it.readText()
|
|
|
|
val response = it.readText()
|
|
|
|
onFail?.invoke(response)
|
|
|
|
onOk?.invoke(response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return@with
|
|
|
|
} catch (e: Exception) {
|
|
|
|
}
|
|
|
|
e.message?.let { onFail?.invoke(it) }
|
|
|
|
//response
|
|
|
|
|
|
|
|
BufferedReader(InputStreamReader(inputStream)).use {
|
|
|
|
|
|
|
|
val response = it.readText()
|
|
|
|
|
|
|
|
onOk?.invoke(response)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
eventually?.invoke()
|
|
|
|
eventually?.invoke()
|
|
|
@ -77,7 +83,7 @@ object {{with .Info}}{{.Title}}{{end}}{
|
|
|
|
val {{with $item}}{{lowCamelCase .Name}}: {{parseType .Type}}{{end}}{{if ne $i (add $length -1)}},{{end}}{{end}}
|
|
|
|
val {{with $item}}{{lowCamelCase .Name}}: {{parseType .Type}}{{end}}{{if ne $i (add $length -1)}},{{end}}{{end}}
|
|
|
|
){{end}}
|
|
|
|
){{end}}
|
|
|
|
{{with .Service}}
|
|
|
|
{{with .Service}}
|
|
|
|
{{range .Routes}}suspend fun {{pathToFuncName .Path}}({{with .RequestType}}{{if ne .Name ""}}
|
|
|
|
{{range .Routes}}suspend fun {{routeToFuncName .Method .Path}}({{with .RequestType}}{{if ne .Name ""}}
|
|
|
|
req:{{.Name}},{{end}}{{end}}
|
|
|
|
req:{{.Name}},{{end}}{{end}}
|
|
|
|
onOk: (({{with .ResponseType}}{{.Name}}{{end}}) -> Unit)? = null,
|
|
|
|
onOk: (({{with .ResponseType}}{{.Name}}{{end}}) -> Unit)? = null,
|
|
|
|
onFail: ((String) -> Unit)? = null,
|
|
|
|
onFail: ((String) -> Unit)? = null,
|
|
|
|