All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.xebia.functional.openai.FormBuilderOps.kt Maven / Gradle / Ivy

There is a newer version: 0.0.5-alpha.119
Show newest version
package com.xebia.functional.openai

import io.ktor.client.request.forms.*
import io.ktor.http.*
import io.ktor.util.*
import io.ktor.utils.io.core.*
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.serializerOrNull

@OptIn(InternalAPI::class)
fun  FormBuilder.appendGen(key: String, value: T, headers: Headers = Headers.Empty): Unit {
  when (value) {
    is String -> append(key, value, headers)
    is Number -> append(key, value, headers)
    is Boolean -> append(key, value, headers)
    is ByteArray -> append(key, value, headers)
    is ByteReadPacket -> append(key, value, headers)
    is InputProvider -> append(key, value, headers)
    is ChannelProvider -> append(key, value, headers)
    is UploadFile -> appendUploadedFile(key, value)
    is Enum<*> -> append(key, serialNameOrEnumValue(value), headers)
    else -> append(key, value, headers)
  }
}

internal fun FormBuilder.appendUploadedFile(key: String, file: UploadFile) {
  append(
    key = key,
    filename = file.filename,
    contentType = file.contentType ?: ContentType.Application.OctetStream,
    size = file.size,
    bodyBuilder = file.bodyBuilder
  )
}

@OptIn(InternalSerializationApi::class, ExperimentalSerializationApi::class)
fun > serialNameOrEnumValue(v: Enum): String =
  v::class.serializerOrNull()?.descriptor?.getElementName(v.ordinal) ?: v.toString()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy