-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve LoxoneEndpoint constructors
- Loading branch information
Showing
7 changed files
with
155 additions
and
12 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
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
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cz.smarteon.loxone | ||
|
||
import io.ktor.http.* | ||
import kotlin.jvm.JvmOverloads | ||
import kotlin.jvm.JvmStatic | ||
|
||
/** | ||
* Loxone connection endpoint representation. | ||
* | ||
* @param host Loxone host name or IP address, without protocol prefix, port or path. | ||
* @param port Loxone port, default is 443 (HTTPS). | ||
* @param useSsl Whether to use SSL, default is true. | ||
* @param path Loxone path, url encoded, default is empty string. | ||
*/ | ||
data class LoxoneEndpoint @JvmOverloads constructor( | ||
val host: String, | ||
val port: Int = HTTPS_PORT, | ||
val useSsl: Boolean = true, | ||
val path: String = "" | ||
) { | ||
|
||
init { | ||
require(host.isNotBlank()) { "Host must not be blank" } | ||
require(!host.contains(":")) { "Host must not contain port or protocol" } | ||
} | ||
companion object { | ||
private const val HTTP_PORT = 80 | ||
private const val HTTPS_PORT = 443 | ||
|
||
/** | ||
* Creates Loxone endpoint by parsing URL. | ||
* @param url Loxone URL. | ||
*/ | ||
@JvmStatic | ||
fun fromUrl(url: String): LoxoneEndpoint { | ||
val parsed = URLBuilder().takeFrom(url) | ||
val port = when { | ||
parsed.port > 0 -> parsed.port | ||
parsed.protocol.isSecure() -> HTTPS_PORT | ||
else -> HTTP_PORT | ||
} | ||
println(parsed) | ||
println(parsed.encodedPath) | ||
return LoxoneEndpoint(parsed.host, port, parsed.protocol.isSecure(), parsed.encodedPath) | ||
} | ||
|
||
/** | ||
* Creates Loxone endpoint for local connection. | ||
* | ||
* @param address Loxone IP address. | ||
* @param port Loxone port, default is 80 (HTTP). | ||
* @param path Loxone path, url encoded, default is empty string. | ||
*/ | ||
@JvmStatic @JvmOverloads | ||
fun local(address: String, port: Int = HTTP_PORT, path: String = ""): LoxoneEndpoint { | ||
require(hostIsIp(address)) { "Local address must be IP" } | ||
return LoxoneEndpoint(address, port, false, path) | ||
} | ||
|
||
/** | ||
* Creates Loxone endpoint for public domain connection. | ||
* | ||
* @param domain Loxone host domain. | ||
* @param port Loxone port, default is 443 (HTTPS). | ||
* @param path Loxone path, url encoded, default is empty string. | ||
*/ | ||
@JvmStatic @JvmOverloads | ||
fun public(domain: String, port: Int = HTTPS_PORT, path: String = ""): LoxoneEndpoint { | ||
require(!hostIsIp(domain)) { "Public domain must not be IP" } | ||
return LoxoneEndpoint(domain, port, true, path) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package cz.smarteon.loxone | ||
|
||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.core.spec.style.WordSpec | ||
import io.kotest.datatest.withData | ||
import io.kotest.matchers.shouldBe | ||
|
||
class LoxoneEndpointTest : WordSpec({ | ||
|
||
"Loxone endpoint constructed" When { | ||
|
||
"with default values" should { | ||
LoxoneEndpoint("somehost").apply { | ||
"have host" { host shouldBe "somehost"} | ||
"have port" { port shouldBe 443 } | ||
"have useSsl" { useSsl shouldBe true } | ||
"have path" { path shouldBe "" } | ||
} | ||
} | ||
|
||
withData( | ||
nameFn = { (case, _) -> "with $case host" }, | ||
"empty" to "", | ||
"contains port" to "host:123", | ||
"contains protocol" to "http://host" | ||
) { (_, host) -> | ||
shouldThrow<IllegalArgumentException> { | ||
LoxoneEndpoint(host) | ||
} | ||
} | ||
} | ||
|
||
"FromUrl endpoint constructed" When { | ||
withData( | ||
nameFn = { (url, _) -> url }, | ||
"http://some:7780/testPath" to LoxoneEndpoint("some", 7780, false, "/testPath"), | ||
"http://some/testPath" to LoxoneEndpoint("some", 80, false, "/testPath"), | ||
"https://some.smarteon.cz:7743/testPath" to LoxoneEndpoint("some.smarteon.cz", 7743, true, "/testPath"), | ||
"https://some.smarteon.cz/testPath" to LoxoneEndpoint("some.smarteon.cz", 443, true, "/testPath") | ||
) { (url, expected) -> | ||
LoxoneEndpoint.fromUrl(url) shouldBe expected | ||
} | ||
} | ||
|
||
"Local endpoint constructed" When { | ||
"with default values" should { | ||
LoxoneEndpoint.local("192.168.9.77").apply { | ||
"have host" { host shouldBe "192.168.9.77" } | ||
"have port" { port shouldBe 80 } | ||
"have useSsl" { useSsl shouldBe false } | ||
"have path" { path shouldBe "" } | ||
} | ||
} | ||
|
||
"with non-IP address" should { | ||
shouldThrow<IllegalArgumentException> { | ||
LoxoneEndpoint.local("somehost") | ||
} | ||
} | ||
} | ||
|
||
"Public domain endpoint constructed" When { | ||
"with default values" should { | ||
LoxoneEndpoint.public("test.smarteon.cz").apply { | ||
"have host" { host shouldBe "test.smarteon.cz" } | ||
"have port" { port shouldBe 443 } | ||
"have useSsl" { useSsl shouldBe true } | ||
"have path" { path shouldBe "" } | ||
} | ||
} | ||
|
||
"with non-IP address" should { | ||
shouldThrow<IllegalArgumentException> { | ||
LoxoneEndpoint.public("192.168.9.77") | ||
} | ||
} | ||
} | ||
}) |