-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
234 additions
and
395 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
package io.github.legion2.open_cue_cli | ||
|
||
import io.github.legion2.open_cue_cli.client.CueSdkHttpServer | ||
import io.github.legion2.open_cue_cli.client.LocalFileClient | ||
|
||
class CliContext( | ||
val sdkClient: CueSdkHttpServer, | ||
val localFileClient: LocalFileClient) { | ||
val sdkClient: CueSdkHttpServer) { | ||
companion object { | ||
fun createCliContext(host: String, port: Int): CliContext { | ||
return CliContext(CueSdkHttpServer(host, port), LocalFileClient()) | ||
return CliContext(CueSdkHttpServer(host, port)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 37 additions & 49 deletions
86
src/main/kotlin/io/github/legion2/open_cue_cli/client/CueSdkHttpServer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,69 @@ | ||
package io.github.legion2.open_cue_cli.client | ||
|
||
import io.github.legion2.open_cue_cli.model.Game | ||
import com.google.gson.Gson | ||
import io.github.legion2.open_cue_cli.model.Profile | ||
import io.github.legion2.open_cue_cli.model.SdkDetails | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.call.typeInfo | ||
import io.ktor.client.features.ClientRequestException | ||
import io.ktor.client.features.HttpResponseValidator | ||
import io.ktor.client.features.ResponseException | ||
import io.ktor.client.features.addDefaultResponseValidation | ||
import io.ktor.client.features.json.GsonSerializer | ||
import io.ktor.client.features.json.JsonFeature | ||
import io.ktor.client.features.json.defaultSerializer | ||
import io.ktor.client.request.get | ||
import io.ktor.client.request.post | ||
import io.ktor.client.statement.readText | ||
import io.ktor.http.ParametersBuilder | ||
import io.ktor.http.URLBuilder | ||
import io.ktor.http.Url | ||
import io.ktor.utils.io.core.Input | ||
import java.net.ConnectException | ||
|
||
class CueSdkHttpServer(private val host: String = "localhost", private val port: Int = 25555) { | ||
val client = HttpClient { | ||
private val client = HttpClient { | ||
install(JsonFeature) { | ||
serializer = GsonSerializer() | ||
} | ||
addDefaultResponseValidation() | ||
HttpResponseValidator { | ||
handleResponseException { exception -> | ||
|
||
when (exception) { | ||
is ClientRequestException -> throw GameSdkError(Gson().fromJson(exception.response.readText(), String::class.java)) | ||
is ResponseException -> throw SdkHttpServerException("Server can not process request: ${exception.response.call.request.url}", exception) | ||
is ConnectException -> throw SdkHttpServerException("Can not send request to Open CUE Service\n" | ||
+ "Make sure the Open CUE Service is running.", exception) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun createURL(function: String, parameters: Map<String, String> = emptyMap()): Url { | ||
private fun url(path: String, parameters: Map<String, String> = emptyMap()): Url { | ||
val httpParameters = parameters.entries.fold(ParametersBuilder()) { builder, (key, value) -> | ||
builder.append(key, value) | ||
builder | ||
} | ||
httpParameters.append("func", function) | ||
return URLBuilder(host = host, port = port, parameters = httpParameters).path("icue").build() | ||
return URLBuilder(host = host, port = port, parameters = httpParameters).path(path).build() | ||
} | ||
|
||
suspend fun requestString(function: String, parameters: Map<String, String> = emptyMap()): String { | ||
val url = createURL(function, parameters) | ||
val response = try { | ||
client.get<String>(url) | ||
} catch (e: Throwable) { | ||
throw SdkHttpServerException("Can not send request to SDK HTTP Server: $url\n" | ||
+ "Make sure the SDK HTTP Server is running and this url is correct.", e) | ||
} | ||
return translateResponse(response) | ||
} | ||
suspend fun currentGame(): String = client.get(url("api/sdk/game")) | ||
|
||
suspend inline fun <reified T> requestJson(function: String, parameters: Map<String, String> = emptyMap()): T { | ||
val url = createURL(function, parameters) | ||
val input = try { | ||
client.get<Input>(url) | ||
} catch (e: Throwable) { | ||
throw SdkHttpServerException("Can not send request to SDK HTTP Server: $url\n" | ||
+ "Make sure the SDK HTTP Server is running and this url is correct.", e) | ||
} | ||
return defaultSerializer().read(typeInfo<T>(), input) as T | ||
} | ||
suspend fun sdkInfo(): SdkDetails = client.get(url("api/sdk/details")) | ||
|
||
suspend fun getControl(): Boolean = client.get(url("api/sdk/control")) | ||
|
||
private fun translateResponse(response: String): String { | ||
return when (response) { | ||
"CE_Success" -> "Success" | ||
"CE_MissingPrioritiesFile" -> throw GameSdkError( | ||
"game or profile not found, check the GameSdkEffects directory if the game and profiles exists.\n" + | ||
"Also make sure the priorities.cfg exist and contains all the profiles.") | ||
else -> throw GameSdkError("Unknown Error, check your input and the logs of the SDK HTTP Server") | ||
} | ||
} | ||
} | ||
|
||
suspend fun CueSdkHttpServer.currentGame(): Game = requestJson("getgame") | ||
suspend fun setControl(value: Boolean): Boolean = client.post(url("api/sdk/control/${value}")) | ||
|
||
suspend fun CueSdkHttpServer.listGames(): Map<String, Game> = requestJson("getallgames") | ||
suspend fun clearAllEvents(): Unit = client.post(url("api/sdk/stop-events")) | ||
|
||
suspend fun CueSdkHttpServer.setGame(game: String): String = requestString("setgame", mapOf("game" to game)) | ||
suspend fun deactivateAll(): Unit = client.post(url("api/sdk/deactivate-profiles")) | ||
|
||
suspend fun CueSdkHttpServer.resetGame(game: String): String = requestString("reset", mapOf("game" to game)) | ||
suspend fun listProfiles(): List<Profile> = client.get(url("api/profiles")) | ||
|
||
suspend fun CueSdkHttpServer.clearAllEvents(game: String): String = requestString("clearallevents", mapOf("game" to game)) | ||
suspend fun getProfile(profile: String): Profile = client.get(url("api/profiles/${profile}")) | ||
|
||
suspend fun CueSdkHttpServer.setEvent(game: String, event: String): String = requestString("setevent", mapOf("game" to game, "event" to event)) | ||
suspend fun trigger(profile: String): Profile = client.post(url("api/profiles/${profile}/trigger")) | ||
|
||
suspend fun CueSdkHttpServer.clearAllStates(game: String): String = requestString("clearallstates", mapOf("game" to game)) | ||
suspend fun activate(profile: String): Profile = client.post(url("api/profiles/${profile}/state/true")) | ||
|
||
suspend fun CueSdkHttpServer.setState(game: String, state: String): String = requestString("setstate", mapOf("game" to game, "state" to state)) | ||
|
||
suspend fun CueSdkHttpServer.clearState(game: String, state: String): String = requestString("clearstate", mapOf("game" to game, "state" to state)) | ||
suspend fun deactivate(profile: String): Profile = client.post(url("api/profiles/${profile}/state/false")) | ||
} |
38 changes: 0 additions & 38 deletions
38
src/main/kotlin/io/github/legion2/open_cue_cli/client/LocalFileClient.kt
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
src/main/kotlin/io/github/legion2/open_cue_cli/client/ProfilesConfigFileMissingException.kt
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
src/main/kotlin/io/github/legion2/open_cue_cli/client/ProfilesConfigParseException.kt
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
src/main/kotlin/io/github/legion2/open_cue_cli/event/ClearAllEvent.kt
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
src/main/kotlin/io/github/legion2/open_cue_cli/event/EventCommand.kt
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
src/main/kotlin/io/github/legion2/open_cue_cli/event/SetEvent.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.