Create Request wrapper
This commit is contained in:
@@ -49,6 +49,7 @@ class SonosWorker(
|
||||
device.stop()
|
||||
device.volume = data.volume
|
||||
|
||||
|
||||
if (preferences.getBoolean(PreferenceKey.ENABLE_GONG, false)) {
|
||||
playUri(device, gongUrl)
|
||||
}
|
||||
|
||||
@@ -30,9 +30,9 @@ class DefaultEndpoint(
|
||||
return null
|
||||
}
|
||||
|
||||
val request = Request.of(extractBody(session), matchingResult.variables)
|
||||
val request = Request.of(session, matchingResult)
|
||||
|
||||
val response = consumer.call(request.body, request.params).checktable()
|
||||
val response = consumer.call(request.luaTable).checktable()
|
||||
return parseResponse(response)
|
||||
}
|
||||
|
||||
@@ -81,10 +81,4 @@ class DefaultEndpoint(
|
||||
|
||||
private fun getData(response: LuaTable) = response.get("data").checkstring().tojstring()
|
||||
|
||||
private fun extractBody(session: IHTTPSession): String {
|
||||
return mutableMapOf<String, String>().let {
|
||||
session.parseBody(it)
|
||||
it["postData"] ?: ""
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,10 @@
|
||||
package com.bartlomiejpluta.ttsserver.core.web.endpoint
|
||||
|
||||
import com.bartlomiejpluta.ttsserver.core.sonos.worker.SonosWorker
|
||||
import com.bartlomiejpluta.ttsserver.core.web.uri.UriTemplate
|
||||
import fi.iki.elonen.NanoHTTPD
|
||||
import fi.iki.elonen.NanoHTTPD.newFixedLengthResponse
|
||||
import org.luaj.vm2.LuaClosure
|
||||
import java.util.concurrent.BlockingQueue
|
||||
import java.util.concurrent.Executors
|
||||
import java.util.concurrent.LinkedBlockingQueue
|
||||
import java.util.concurrent.ThreadPoolExecutor
|
||||
|
||||
class QueuedEndpoint(
|
||||
private val uri: UriTemplate,
|
||||
@@ -33,7 +29,7 @@ class QueuedEndpoint(
|
||||
return null
|
||||
}
|
||||
|
||||
val request = Request.of(extractBody(session), matchingResult.variables)
|
||||
val request = Request.of(session, matchingResult)
|
||||
|
||||
queue.add(request)
|
||||
|
||||
|
||||
@@ -1,16 +1,40 @@
|
||||
package com.bartlomiejpluta.ttsserver.core.web.endpoint
|
||||
|
||||
import com.bartlomiejpluta.ttsserver.core.web.uri.UriTemplate
|
||||
import fi.iki.elonen.NanoHTTPD
|
||||
import org.luaj.vm2.LuaValue
|
||||
|
||||
class Request private constructor(rawBody: String, paramsMap: Map<String, String>) {
|
||||
val body = LuaValue.valueOf(rawBody)
|
||||
val params = LuaValue.tableOf().also { params ->
|
||||
paramsMap
|
||||
.map { LuaValue.valueOf(it.key) to LuaValue.valueOf(it.value) }
|
||||
.forEach { params.set(it.first, it.second) }
|
||||
class Request private constructor(
|
||||
request: NanoHTTPD.IHTTPSession,
|
||||
matchingResult: UriTemplate.MatchingResult
|
||||
) {
|
||||
val luaTable = LuaValue.tableOf()
|
||||
|
||||
init {
|
||||
val body = extractBody(request)
|
||||
val pathParams = extractPathParams(matchingResult)
|
||||
|
||||
luaTable.set("body", body)
|
||||
luaTable.set("params", pathParams)
|
||||
}
|
||||
|
||||
|
||||
private fun extractPathParams(matchingResult: UriTemplate.MatchingResult) =
|
||||
LuaValue.tableOf().also { params ->
|
||||
matchingResult.variables
|
||||
.map { LuaValue.valueOf(it.key) to LuaValue.valueOf(it.value) }
|
||||
.forEach { params.set(it.first, it.second) }
|
||||
|
||||
}
|
||||
|
||||
private fun extractBody(request: NanoHTTPD.IHTTPSession) =
|
||||
mutableMapOf<String, String>().let {
|
||||
request.parseBody(it)
|
||||
it["postData"] ?: ""
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun of(body: String, params: Map<String, String>) = Request(body, params)
|
||||
fun of(request: NanoHTTPD.IHTTPSession, matchingResult: UriTemplate.MatchingResult) =
|
||||
Request(request, matchingResult)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,9 @@ package com.bartlomiejpluta.ttsserver.core.web.endpoint
|
||||
import com.bartlomiejpluta.ttsserver.service.foreground.ForegroundService
|
||||
import com.bartlomiejpluta.ttsserver.service.state.ServiceState
|
||||
import org.luaj.vm2.LuaClosure
|
||||
import org.luaj.vm2.LuaInteger
|
||||
import org.luaj.vm2.LuaValue
|
||||
import org.luaj.vm2.lib.ZeroArgFunction
|
||||
import java.util.concurrent.BlockingQueue
|
||||
|
||||
class Worker(
|
||||
@@ -18,6 +21,10 @@ class Worker(
|
||||
}
|
||||
|
||||
private fun consume(request: Request) {
|
||||
consumer.call(request.body, request.params)
|
||||
consumer.call(request.luaTable, QueueSizeFunction(queue))
|
||||
}
|
||||
|
||||
class QueueSizeFunction(private val queue: BlockingQueue<Request>) : ZeroArgFunction() {
|
||||
override fun call(): LuaInteger = LuaValue.valueOf(queue.size)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user