47 lines
1.4 KiB
Kotlin
47 lines
1.4 KiB
Kotlin
package com.example.tvcontroller.services
|
|
|
|
import android.util.Log
|
|
import io.ktor.client.engine.cio.*
|
|
import io.ktor.client.*
|
|
import io.ktor.client.call.body
|
|
import io.ktor.client.request.headers
|
|
import io.ktor.client.request.request
|
|
import io.ktor.client.request.setBody
|
|
import io.ktor.client.statement.HttpResponse
|
|
import io.ktor.http.HttpMethod
|
|
import org.json.JSONObject
|
|
|
|
object DeviceService {
|
|
private var client = HttpClient(CIO)
|
|
private var serverAddress: String = ""
|
|
|
|
suspend fun createIntegration(name: String, code: String) {
|
|
Log.i("DeviceService", "Creating integration for $name with code $code at $serverAddress")
|
|
val json = JSONObject()
|
|
json.put("name", name)
|
|
json.put("code", code)
|
|
try {
|
|
val response: HttpResponse = client.request("http://$serverAddress/api/integrations") {
|
|
method = HttpMethod.Post
|
|
setBody(json.toString())
|
|
headers {
|
|
append("Content-Type", "application/json")
|
|
}
|
|
}
|
|
|
|
val body: String = response.body()
|
|
Log.i("DeviceService", "Response: ${response.status.value} $body")
|
|
} catch (e: Exception) {
|
|
Log.e("DeviceService", "Error creating integration", e)
|
|
}
|
|
}
|
|
|
|
fun setServerAddress(url: String) {
|
|
serverAddress = url
|
|
}
|
|
|
|
fun getServerAddress(): String {
|
|
return serverAddress
|
|
}
|
|
}
|