This commit is contained in:
70
src/Application.kt
Normal file
70
src/Application.kt
Normal file
@ -0,0 +1,70 @@
|
||||
package com.kmlabz.k8s
|
||||
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.google.gson.internal.GsonBuildConfig
|
||||
import io.ktor.application.*
|
||||
import io.ktor.response.*
|
||||
import io.ktor.request.*
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.engine.jetty.*
|
||||
import io.ktor.client.features.json.*
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.client.request.forms.*
|
||||
import io.ktor.client.statement.*
|
||||
import io.ktor.http.*
|
||||
import io.ktor.http.ContentDisposition.Companion.File
|
||||
import io.ktor.http.content.*
|
||||
import io.ktor.util.*
|
||||
import io.ktor.utils.io.streams.*
|
||||
import kotlinx.coroutines.*
|
||||
import java.io.File
|
||||
|
||||
fun main(args: Array<String>): Unit = io.ktor.server.jetty.EngineMain.main(args)
|
||||
|
||||
@KtorExperimentalAPI
|
||||
@Suppress("unused") // Referenced in application.conf
|
||||
@kotlin.jvm.JvmOverloads
|
||||
fun Application.module(testing: Boolean = false) {
|
||||
val client = HttpClient(Jetty) {
|
||||
install(JsonFeature) {
|
||||
serializer = GsonSerializer()
|
||||
}
|
||||
}
|
||||
runBlocking {
|
||||
val cachedFile = File("/app/wave.wav").readBytes()
|
||||
val gson = GsonBuilder().setPrettyPrinting().create()
|
||||
uploadData(1, gson, cachedFile)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@KtorExperimentalAPI
|
||||
suspend fun uploadData(currIteration: Int, gson: Gson, inputSound: ByteArray) {
|
||||
HttpClient(Jetty).use { client ->
|
||||
val parts: List<PartData> = formData {
|
||||
val headersBuilder = HeadersBuilder()
|
||||
headersBuilder[HttpHeaders.ContentType] = "audio/wave"
|
||||
headersBuilder[HttpHeaders.ContentDisposition] = "filename=csirip.wav"
|
||||
this.append(
|
||||
"file",
|
||||
inputSound,
|
||||
headersBuilder.build()
|
||||
)
|
||||
val inputSvcApi = InputSvcApi("2020-02-02T02:02:02", currIteration)
|
||||
val jsonHeadersBuilder = HeadersBuilder()
|
||||
jsonHeadersBuilder[HttpHeaders.ContentType] = "application/json"
|
||||
this.append(
|
||||
"description",
|
||||
gson.toJson(inputSvcApi),
|
||||
jsonHeadersBuilder.build()
|
||||
)
|
||||
}
|
||||
|
||||
client.submitFormWithBinaryData<Unit>(formData = parts) {
|
||||
url("https://birb.k8s.kmlabz.com/sample")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class InputSvcApi(val date: String, val device_id: Int)
|
Reference in New Issue
Block a user