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

com.lithic.api.core.HttpRequestBodies.kt Maven / Gradle / Ivy

Go to download

The Lithic Developer API is designed to provide a predictable programmatic interface for accessing your Lithic account through an API and transaction webhooks. Note that your API key is a secret and should be treated as such. Don't share it with anyone, including us. We will never ask you for it.

The newest version!
@file:JvmName("HttpRequestBodies")

package com.lithic.api.core

import com.fasterxml.jackson.databind.json.JsonMapper
import com.lithic.api.core.http.HttpRequestBody
import com.lithic.api.errors.LithicException
import java.io.ByteArrayOutputStream
import java.io.OutputStream
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder

internal inline fun  json(
    jsonMapper: JsonMapper,
    value: T,
): HttpRequestBody {
    return object : HttpRequestBody {
        private var cachedBytes: ByteArray? = null

        private fun serialize(): ByteArray {
            if (cachedBytes != null) return cachedBytes!!

            val buffer = ByteArrayOutputStream()
            try {
                jsonMapper.writeValue(buffer, value)
                cachedBytes = buffer.toByteArray()
                return cachedBytes!!
            } catch (e: Exception) {
                throw LithicException("Error writing request", e)
            }
        }

        override fun writeTo(outputStream: OutputStream) {
            outputStream.write(serialize())
        }

        override fun contentType(): String = "application/json"

        override fun contentLength(): Long {
            return serialize().size.toLong()
        }

        override fun repeatable(): Boolean = true

        override fun close() {}
    }
}

internal fun multipartFormData(
    jsonMapper: JsonMapper,
    parts: Array?>
): HttpRequestBody {
    val builder = MultipartEntityBuilder.create()
    parts.forEach { part ->
        if (part?.value != null) {
            when (part.value) {
                is JsonValue -> {
                    val buffer = ByteArrayOutputStream()
                    try {
                        jsonMapper.writeValue(buffer, part.value)
                    } catch (e: Exception) {
                        throw LithicException("Error serializing value to json", e)
                    }
                    builder.addBinaryBody(
                        part.name,
                        buffer.toByteArray(),
                        part.contentType,
                        part.filename
                    )
                }
                is Boolean ->
                    builder.addTextBody(
                        part.name,
                        if (part.value) "true" else "false",
                        part.contentType
                    )
                is Int -> builder.addTextBody(part.name, part.value.toString(), part.contentType)
                is Long -> builder.addTextBody(part.name, part.value.toString(), part.contentType)
                is Double -> builder.addTextBody(part.name, part.value.toString(), part.contentType)
                is ByteArray ->
                    builder.addBinaryBody(part.name, part.value, part.contentType, part.filename)
                is String -> builder.addTextBody(part.name, part.value, part.contentType)
                is Enum -> builder.addTextBody(part.name, part.value.toString(), part.contentType)
                else ->
                    throw IllegalArgumentException(
                        "Unsupported content type: ${part.value::class.java.simpleName}"
                    )
            }
        }
    }
    val entity = builder.build()

    return object : HttpRequestBody {
        override fun writeTo(outputStream: OutputStream) {
            try {
                return entity.writeTo(outputStream)
            } catch (e: Exception) {
                throw LithicException("Error writing request", e)
            }
        }

        override fun contentType(): String = entity.contentType

        override fun contentLength(): Long = -1

        override fun repeatable(): Boolean = entity.isRepeatable

        override fun close() = entity.close()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy