Skip to content

Commit

Permalink
feat: introduce http POST
Browse files Browse the repository at this point in the history
  • Loading branch information
jimirocks committed Nov 1, 2024
1 parent 07cbeb4 commit d17f4af
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 20 deletions.
2 changes: 1 addition & 1 deletion examples/java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repositories {
}

dependencies {
implementation("cz.smarteon.loxone:loxone-client-kotlin:0.1.0")
implementation("cz.smarteon.loxone:loxone-client-kotlin")

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.7.1")
}
2 changes: 1 addition & 1 deletion examples/java/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//includeBuild("../..")
includeBuild("../..")

rootProject.name = "loxone-client-kotlin-java-example"
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
import cz.smarteon.loxone.Command;
import cz.smarteon.loxone.LoxoneClient;
import cz.smarteon.loxone.LoxoneResponse;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.EmptyCoroutineContext;
import kotlinx.coroutines.BuildersKt;
import kotlinx.coroutines.CoroutineScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

import static java.util.Objects.requireNonNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package cz.smarteon.loxone.example;

import cz.smarteon.loxone.LoxoneAuth;
import cz.smarteon.loxone.LoxoneClient;
import cz.smarteon.loxone.LoxoneCredentials;
import cz.smarteon.loxone.LoxoneEndpoint;
import cz.smarteon.loxone.LoxoneProfile;
import cz.smarteon.loxone.LoxoneTokenAuthenticator;
import cz.smarteon.loxone.ktor.HttpLoxoneClient;
import cz.smarteon.loxone.ktor.KtorHttpLoxoneClient;
import cz.smarteon.loxone.message.ApiInfo;

public class LoxoneClientExample {
Expand All @@ -14,14 +12,9 @@ public static void main(String[] args) {
System.out.println("Test");

final var endpoint = new LoxoneEndpoint(args[0], 443);
final LoxoneClient loxoneClient = new HttpLoxoneClient(
final LoxoneClient loxoneClient = new KtorHttpLoxoneClient(
endpoint,
new LoxoneTokenAuthenticator(
new LoxoneProfile(
endpoint,
new LoxoneCredentials(args[1], args[2])
)
)
new LoxoneAuth.Basic(args[1], args[2])
);

final var client = new BlockingHttpLoxoneClient(loxoneClient);
Expand Down
16 changes: 15 additions & 1 deletion src/commonMain/kotlin/LoxoneClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,21 @@ interface LoxoneClient {
suspend fun close()
}

interface HttpLoxoneClient : LoxoneClient
/**
* Loxone client using HTTP for communication.
*/
interface HttpLoxoneClient : LoxoneClient {
/**
* Sends given [payload] to [command]'s endpoint using HTTP POST to Loxone and returns
* the response body as text.
*
* @param command Command to call.
* @param payload Payload to send.
* @return Response body as text.
*/
suspend fun postRaw(command: String, payload: ByteArray): String
}

interface WebsocketLoxoneClient : LoxoneClient

suspend inline fun <reified VAL : LoxoneMsgVal> LoxoneClient.callForMsg(command: LoxoneMsgCommand<VAL>): VAL {
Expand Down
11 changes: 11 additions & 0 deletions src/commonMain/kotlin/ktor/KtorHttpLoxoneClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import io.ktor.client.engine.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import kotlin.jvm.JvmOverloads
Expand Down Expand Up @@ -68,6 +69,16 @@ class KtorHttpLoxoneClient internal constructor(
}.body()
}

override suspend fun postRaw(command: String, payload: ByteArray): String {
authentication.tokenAuthenticator?.ensureAuthenticated(this)
return httpClient.post {
commandRequest {
appendPathSegments(command)
}
setBody(payload)
}.bodyAsText()
}

@Suppress("TooGenericExceptionCaught")
override suspend fun close() {
try {
Expand Down
15 changes: 14 additions & 1 deletion src/commonTest/kotlin/ktor/HttpLoxoneClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import io.kotest.matchers.shouldBe
import io.ktor.client.engine.mock.*
import io.ktor.client.plugins.*
import io.ktor.http.*
import io.ktor.utils.io.core.*

class HttpLoxoneClientTest : WordSpec({

Expand Down Expand Up @@ -52,7 +53,12 @@ class HttpLoxoneClientTest : WordSpec({

path.startsWith("/jdev/sys/getjwt") -> {
val validUntil = TimeUtils.currentLoxoneSeconds().plus(60)
respondJson(okMsg(path, token(validUntil)))
respondJson(okMsg(path.substring(1), token(validUntil)))
}

request.method == HttpMethod.Post && path.startsWith("/dev/fsput") -> {
val payloadSize = request.body.toByteArray().size
respondJson(okMsg(path.substring(1), payloadSize.toString()))
}

else -> respondHtmlError(HttpStatusCode.NotFound)
Expand Down Expand Up @@ -101,6 +107,13 @@ class HttpLoxoneClientTest : WordSpec({
response.code shouldBe "200"
client.close()
}

"call post" {
val client = KtorHttpLoxoneClient(endpoint, LoxoneAuth.Basic(profile), mockEngine)
val response = client.postRaw("dev/fsput/test", "test".toByteArray())
response shouldBe okMsg("dev/fsput/test", "4")
client.close()
}
}

})

0 comments on commit d17f4af

Please sign in to comment.