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

com.svix.kotlin.internal.infrastructure.ApiClient.kt Maven / Gradle / Ivy

package com.svix.kotlin.internal.infrastructure

import okhttp3.OkHttpClient
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.FormBody
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.ResponseBody
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.Request
import okhttp3.Headers
import okhttp3.MultipartBody
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import java.net.URLConnection
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.OffsetDateTime
import java.time.OffsetTime
import java.util.Date
import java.util.Locale
import kotlinx.coroutines.delay
import kotlin.random.Random

open class ApiClient(val baseUrl: String) {
    companion object {
        protected const val ContentType = "Content-Type"
        protected const val Accept = "Accept"
        protected const val Authorization = "Authorization"
        protected const val JsonMediaType = "application/json"
        protected const val FormDataMediaType = "multipart/form-data"
        protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
        protected const val XmlMediaType = "application/xml"
        protected const val UserAgent = "User-Agent"

        val apiKey: MutableMap = mutableMapOf()
        val apiKeyPrefix: MutableMap = mutableMapOf()
        var username: String? = null
        var password: String? = null

        @JvmStatic
        val client: OkHttpClient by lazy {
            builder.build()
        }

        @JvmStatic
        val builder: OkHttpClient.Builder = OkHttpClient.Builder()
    }

    var accessToken: String? = null
    var userAgent: String? = null
    var initialRetryDelayMillis = 50L
    var numRetries: Int = 3

    /**
     * Guess Content-Type header from the given file (defaults to "application/octet-stream").
     *
     * @param file The given file
     * @return The guessed Content-Type
     */
    protected fun guessContentTypeFromFile(file: File): String {
        val contentType = URLConnection.guessContentTypeFromName(file.name)
        return contentType ?: "application/octet-stream"
    }

    protected inline fun  requestBody(content: T, mediaType: String = JsonMediaType): RequestBody =
        when {
            content is File -> content.asRequestBody(mediaType.toMediaTypeOrNull())
            mediaType == FormDataMediaType -> {
                MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .apply {
                        // content's type *must* be Map
                        @Suppress("UNCHECKED_CAST")
                        (content as Map).forEach { (key, value) ->
                            if (value is File) {
                                val partHeaders = Headers.headersOf(
                                    "Content-Disposition",
                                    "form-data; name=\"$key\"; filename=\"${value.name}\""
                                )
                                val fileMediaType = guessContentTypeFromFile(value).toMediaTypeOrNull()
                                addPart(partHeaders, value.asRequestBody(fileMediaType))
                            } else {
                                val partHeaders = Headers.headersOf(
                                    "Content-Disposition",
                                    "form-data; name=\"$key\""
                                )
                                addPart(
                                    partHeaders,
                                    parameterToString(value).toRequestBody(null)
                                )
                            }
                        }
                    }.build()
            }
            mediaType == FormUrlEncMediaType -> {
                FormBody.Builder().apply {
                    // content's type *must* be Map
                    @Suppress("UNCHECKED_CAST")
                    (content as Map).forEach { (key, value) ->
                        add(key, parameterToString(value))
                    }
                }.build()
            }
            mediaType == JsonMediaType -> {
                // Don't call `toJson` on Unit type:
                if (T::class.java == Unit::class.java) {
                    ByteArray(0).toRequestBody()
                } else {
                    Serializer.moshi.adapter(T::class.java).toJson(content).toRequestBody(
                        mediaType.toMediaTypeOrNull()
                    )
                }
            }
            mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
            // TODO: this should be extended with other serializers
            else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
        }

    protected inline fun  responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
        if(body == null) {
            return null
        }
        val bodyContent = body.string()
        if (bodyContent.isEmpty()) {
            return null
        }
        if (T::class.java == File::class.java) {
            // return tempfile
            val f = java.nio.file.Files.createTempFile("tmp.com.svix.kotlin.internal", null).toFile()
            f.deleteOnExit()
            val out = BufferedWriter(FileWriter(f))
            out.write(bodyContent)
            out.close()
            return f as T
        }
        return when(mediaType) {
            JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent)
            else ->  throw UnsupportedOperationException("responseBody currently only supports JSON body.")
        }
    }

    protected fun  updateAuthParams(requestConfig: RequestConfig) {
        if (requestConfig.headers[Authorization].isNullOrEmpty()) {
            accessToken?.let { accessToken ->
                requestConfig.headers[Authorization] = "Bearer $accessToken"
            }
        }
    }

    protected inline suspend fun  request(requestConfig: RequestConfig): ApiInfrastructureResponse {
        val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.")

        // take authMethod from operation
        updateAuthParams(requestConfig)

        // add user agent
        if (requestConfig.headers[UserAgent].isNullOrEmpty()) {
            userAgent?.let { userAgent ->
                requestConfig.headers[UserAgent] = userAgent
            }
        }

        val url = httpUrl.newBuilder()
            .addPathSegments(requestConfig.path.trimStart('/'))
            .apply {
                requestConfig.query.forEach { query ->
                    query.value.forEach { queryValue ->
                        addQueryParameter(query.key, queryValue)
                    }
                }
            }.build()

        // take content-type/accept from spec or set to default (application/json) if not defined
        if (requestConfig.headers[ContentType].isNullOrEmpty()) {
            requestConfig.headers[ContentType] = JsonMediaType
        }
        if (requestConfig.headers[Accept].isNullOrEmpty()) {
            requestConfig.headers[Accept] = JsonMediaType
        }
        if (requestConfig.headers["svix-req-id"].isNullOrEmpty()) {
            requestConfig.headers["svix-req-id"] = Math.abs(Random.nextBits(32)).toString()
        }
        val headers = requestConfig.headers

        if(headers[ContentType] ?: "" == "") {
            throw kotlin.IllegalStateException("Missing Content-Type header. This is required.")
        }

        if(headers[Accept] ?: "" == "") {
            throw kotlin.IllegalStateException("Missing Accept header. This is required.")
        }

        // TODO: support multiple contentType options here.
        val contentType = (headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())

        val request = when (requestConfig.method) {
            RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))
            RequestMethod.GET -> Request.Builder().url(url)
            RequestMethod.HEAD -> Request.Builder().url(url).head()
            RequestMethod.PATCH -> Request.Builder().url(url).patch(requestBody(requestConfig.body, contentType))
            RequestMethod.PUT -> Request.Builder().url(url).put(requestBody(requestConfig.body, contentType))
            RequestMethod.POST -> Request.Builder().url(url).post(requestBody(requestConfig.body, contentType))
            RequestMethod.OPTIONS -> Request.Builder().url(url).method("OPTIONS", null)
        }.apply {
            headers.forEach { header -> addHeader(header.key, header.value) }
        }.build()

        var response = client.newCall(request).execute()

        var sleepTime = initialRetryDelayMillis
        var retryCount = 0
        for (i in 0 until numRetries-1) {
            if (response.isSuccessful || response.code < 500) {
                break
            }
            response.close()
            retryCount = retryCount.inc()
            delay(sleepTime)
            sleepTime = sleepTime * 2
            var newRequest = request.newBuilder().header("svix-retry-count", retryCount.toString()).build()
            response = client.newCall(newRequest).execute()
        }

        val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault())

        // TODO: handle specific mapping types. e.g. Map>
        return when {
            response.isRedirect -> Redirection(
                response.code,
                response.headers.toMultimap()
            )
            response.isInformational -> Informational(
                response.message,
                response.code,
                response.headers.toMultimap()
            )
            response.isSuccessful -> Success(
                responseBody(response.body, accept),
                response.code,
                response.headers.toMultimap()
            )
            response.isClientError -> ClientError(
                response.message,
                response.body?.string(),
                response.code,
                response.headers.toMultimap()
            )
            else -> ServerError(
                response.message,
                response.body?.string(),
                response.code,
                response.headers.toMultimap()
            )
        }
    }

    protected fun parameterToString(value: Any?): String = when (value) {
        null -> ""
        is Array<*> -> toMultiValue(value, "csv").toString()
        is Iterable<*> -> toMultiValue(value, "csv").toString()
        is OffsetDateTime, is OffsetTime, is LocalDateTime, is LocalDate, is LocalTime, is Date ->
            parseDateToQueryString(value)
        else -> value.toString()
    }

    protected inline fun  parseDateToQueryString(value : T): String {
        /*
        .replace("\"", "") converts the json object string to an actual string for the query parameter.
        The moshi or gson adapter allows a more generic solution instead of trying to use a native
        formatter. It also easily allows to provide a simple way to define a custom date format pattern
        inside a gson/moshi adapter.
        */
        return Serializer.moshi.adapter(T::class.java).toJson(value).replace("\"", "")
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy