Enable returning short status' messages instead of full DTO content
This commit is contained in:
@@ -5,9 +5,6 @@ import io.bartek.ttsserver.core.web.exception.WebException
|
|||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
|
|
||||||
abstract class DTO(json: String) : JSONObject(json) {
|
abstract class DTO(json: String) : JSONObject(json) {
|
||||||
val json: String
|
|
||||||
get() = toString()
|
|
||||||
|
|
||||||
protected fun requiredString(key: String) = this.optString(key)
|
protected fun requiredString(key: String) = this.optString(key)
|
||||||
.takeIf { it.isNotBlank() }
|
.takeIf { it.isNotBlank() }
|
||||||
?: throw WebException(Response.Status.BAD_REQUEST, "The '$key' field is required")
|
?: throw WebException(Response.Status.BAD_REQUEST, "The '$key' field is required")
|
||||||
|
|||||||
@@ -6,7 +6,10 @@ import org.json.JSONObject
|
|||||||
|
|
||||||
class WebException(val status: Response.Status, message: String? = null) : Exception(message) {
|
class WebException(val status: Response.Status, message: String? = null) : Exception(message) {
|
||||||
val json: String
|
val json: String
|
||||||
get() = message?.takeIf { it.isNotBlank() }
|
get() = JSONObject().let { json ->
|
||||||
?.let { JSONObject().put("message", it).toString() }
|
json.put("status", status.requestStatus)
|
||||||
?: ""
|
json.put("description", status.description)
|
||||||
|
message?.let { json.put("message", it) }
|
||||||
|
json.toString()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -18,6 +18,7 @@ import io.bartek.ttsserver.core.web.exception.WebException
|
|||||||
import io.bartek.ttsserver.service.foreground.ForegroundService
|
import io.bartek.ttsserver.service.foreground.ForegroundService
|
||||||
import io.bartek.ttsserver.service.state.ServiceState
|
import io.bartek.ttsserver.service.state.ServiceState
|
||||||
import io.bartek.ttsserver.ui.preference.PreferenceKey
|
import io.bartek.ttsserver.ui.preference.PreferenceKey
|
||||||
|
import org.json.JSONObject
|
||||||
import java.io.BufferedInputStream
|
import java.io.BufferedInputStream
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileInputStream
|
import java.io.FileInputStream
|
||||||
@@ -52,7 +53,7 @@ class WebServer(
|
|||||||
Endpoint.WAVE -> wave(it)
|
Endpoint.WAVE -> wave(it)
|
||||||
Endpoint.SONOS -> sonos(it)
|
Endpoint.SONOS -> sonos(it)
|
||||||
Endpoint.SONOS_CACHE -> sonosCache(it)
|
Endpoint.SONOS_CACHE -> sonosCache(it)
|
||||||
Endpoint.UNKNOWN -> throw WebException(NOT_FOUND, "")
|
Endpoint.UNKNOWN -> throw WebException(NOT_FOUND)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +65,7 @@ class WebServer(
|
|||||||
|
|
||||||
private fun say(session: IHTTPSession): Response {
|
private fun say(session: IHTTPSession): Response {
|
||||||
if (!preferences.getBoolean(PreferenceKey.ENABLE_SAY_ENDPOINT, true)) {
|
if (!preferences.getBoolean(PreferenceKey.ENABLE_SAY_ENDPOINT, true)) {
|
||||||
throw WebException(NOT_FOUND, "")
|
throw WebException(NOT_FOUND)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (session.method != Method.POST) {
|
if (session.method != Method.POST) {
|
||||||
@@ -72,13 +73,13 @@ class WebServer(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (session.headers[CONTENT_TYPE]?.let { it != MIME_JSON } != false) {
|
if (session.headers[CONTENT_TYPE]?.let { it != MIME_JSON } != false) {
|
||||||
throw WebException(BAD_REQUEST, "The only accepted format is JSON")
|
throw WebException(BAD_REQUEST, "Only JSON data is accepted")
|
||||||
}
|
}
|
||||||
|
|
||||||
val dto = extractBody(session) { BaseDTO(it) }
|
val dto = extractBody(session) { BaseDTO(it) }
|
||||||
|
|
||||||
tts.performTTS(dto.text, dto.language)
|
tts.performTTS(dto.text, dto.language)
|
||||||
return newFixedLengthResponse(OK, MIME_JSON, dto.json)
|
return newFixedLengthResponse(OK, MIME_JSON, SUCCESS_RESPONSE)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun <T> extractBody(session: IHTTPSession, provider: (String) -> T): T {
|
private fun <T> extractBody(session: IHTTPSession, provider: (String) -> T): T {
|
||||||
@@ -90,7 +91,7 @@ class WebServer(
|
|||||||
|
|
||||||
private fun wave(session: IHTTPSession): Response {
|
private fun wave(session: IHTTPSession): Response {
|
||||||
if (!preferences.getBoolean(PreferenceKey.ENABLE_WAVE_ENDPOINT, true)) {
|
if (!preferences.getBoolean(PreferenceKey.ENABLE_WAVE_ENDPOINT, true)) {
|
||||||
throw WebException(NOT_FOUND, "")
|
throw WebException(NOT_FOUND)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (session.method != Method.POST) {
|
if (session.method != Method.POST) {
|
||||||
@@ -98,10 +99,10 @@ class WebServer(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (session.headers[CONTENT_TYPE]?.let { it != MIME_JSON } != false) {
|
if (session.headers[CONTENT_TYPE]?.let { it != MIME_JSON } != false) {
|
||||||
throw WebException(BAD_REQUEST, "The only accepted format is JSON")
|
throw WebException(BAD_REQUEST, "Only JSON data is accepted")
|
||||||
}
|
}
|
||||||
|
|
||||||
val dto = extractBody(session) {BaseDTO(it) }
|
val dto = extractBody(session) { BaseDTO(it) }
|
||||||
|
|
||||||
val (stream, size) = tts.fetchTTSStream(dto.text, dto.language)
|
val (stream, size) = tts.fetchTTSStream(dto.text, dto.language)
|
||||||
return newFixedLengthResponse(OK, MIME_WAVE, stream, size)
|
return newFixedLengthResponse(OK, MIME_WAVE, stream, size)
|
||||||
@@ -109,7 +110,7 @@ class WebServer(
|
|||||||
|
|
||||||
private fun sonos(session: IHTTPSession): Response {
|
private fun sonos(session: IHTTPSession): Response {
|
||||||
if (!preferences.getBoolean(PreferenceKey.ENABLE_SONOS_ENDPOINT, true)) {
|
if (!preferences.getBoolean(PreferenceKey.ENABLE_SONOS_ENDPOINT, true)) {
|
||||||
throw WebException(NOT_FOUND, "")
|
throw WebException(NOT_FOUND)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (session.method != Method.POST) {
|
if (session.method != Method.POST) {
|
||||||
@@ -117,30 +118,30 @@ class WebServer(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (session.headers[CONTENT_TYPE]?.let { it != MIME_JSON } != false) {
|
if (session.headers[CONTENT_TYPE]?.let { it != MIME_JSON } != false) {
|
||||||
throw WebException(BAD_REQUEST, "The only accepted format is JSON")
|
throw WebException(BAD_REQUEST, "Only JSON data is accepted")
|
||||||
}
|
}
|
||||||
|
|
||||||
val dto = extractBody(session) { SonosDTO(it) }
|
val dto = extractBody(session) { SonosDTO(it) }
|
||||||
|
|
||||||
sonos.push(dto)
|
sonos.push(dto)
|
||||||
|
|
||||||
return newFixedLengthResponse(ACCEPTED, MIME_JSON, dto.json)
|
return newFixedLengthResponse(ACCEPTED, MIME_JSON, QUEUED_RESPONSE)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun sonosCache(session: IHTTPSession): Response {
|
private fun sonosCache(session: IHTTPSession): Response {
|
||||||
if (!preferences.getBoolean(PreferenceKey.ENABLE_SONOS_ENDPOINT, true)) {
|
if (!preferences.getBoolean(PreferenceKey.ENABLE_SONOS_ENDPOINT, true)) {
|
||||||
throw WebException(NOT_FOUND, "")
|
throw WebException(NOT_FOUND)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (session.method != Method.GET) {
|
if (session.method != Method.GET) {
|
||||||
throw WebException(METHOD_NOT_ALLOWED, "Only GET methods are allowed")
|
throw WebException(METHOD_NOT_ALLOWED, "Only GET methods are allowed")
|
||||||
}
|
}
|
||||||
|
|
||||||
val filename = Uri.parse(session.uri).lastPathSegment ?: throw WebException(BAD_REQUEST, "")
|
val filename = Uri.parse(session.uri).lastPathSegment ?: throw WebException(BAD_REQUEST)
|
||||||
val file = File(context.cacheDir, filename)
|
val file = File(context.cacheDir, filename)
|
||||||
|
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
throw WebException(NOT_FOUND, "")
|
throw WebException(NOT_FOUND)
|
||||||
}
|
}
|
||||||
|
|
||||||
val stream = BufferedInputStream(FileInputStream(file))
|
val stream = BufferedInputStream(FileInputStream(file))
|
||||||
@@ -172,5 +173,9 @@ class WebServer(
|
|||||||
private const val MIME_JSON = "application/json"
|
private const val MIME_JSON = "application/json"
|
||||||
private const val MIME_WAVE = "audio/x-wav"
|
private const val MIME_WAVE = "audio/x-wav"
|
||||||
private const val CONTENT_TYPE = "content-type"
|
private const val CONTENT_TYPE = "content-type"
|
||||||
|
private val SUCCESS_RESPONSE = response("Request has been completed")
|
||||||
|
private val QUEUED_RESPONSE = response("Request has been queued")
|
||||||
|
|
||||||
|
private fun response(status: String) = JSONObject().put("message", status).toString()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user