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

kotlin-client.libraries.jvm-spring-webclient.infrastructure.ApiClient.kt.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
package {{packageName}}.infrastructure;

import org.springframework.core.ParameterizedTypeReference
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.http.MediaType
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.http.ResponseEntity
import org.springframework.http.client.MultipartBodyBuilder
import org.springframework.util.LinkedMultiValueMap
import reactor.core.publisher.Mono

{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}open class ApiClient(protected val client: WebClient) {

    protected inline fun  request(requestConfig: RequestConfig): Mono> {
        return prepare(defaults(requestConfig))
            .retrieve()
            .toEntity(object : ParameterizedTypeReference() {})
    }

    protected fun  prepare(requestConfig: RequestConfig) =
        client.method(requestConfig)
            .uri(requestConfig)
            .headers(requestConfig)
            .body(requestConfig)

    protected fun  defaults(requestConfig: RequestConfig) =
        requestConfig.apply {
            if (body != null && headers[HttpHeaders.CONTENT_TYPE].isNullOrEmpty()) {
                headers[HttpHeaders.CONTENT_TYPE] = MediaType.APPLICATION_JSON_VALUE
            }
            if (headers[HttpHeaders.ACCEPT].isNullOrEmpty()) {
                headers[HttpHeaders.ACCEPT] = MediaType.APPLICATION_JSON_VALUE
            }
        }

    private fun  WebClient.method(requestConfig: RequestConfig)=
        method(HttpMethod.valueOf(requestConfig.method.name))

    private fun  WebClient.RequestBodyUriSpec.uri(requestConfig: RequestConfig) =
        uri { builder ->
            builder
                .path(requestConfig.path)
                .queryParams(LinkedMultiValueMap(requestConfig.query))
                .build(requestConfig.params)
        }

    private fun  WebClient.RequestBodySpec.headers(requestConfig: RequestConfig) =
        apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }

    private fun  WebClient.RequestBodySpec.body(requestConfig: RequestConfig): WebClient.RequestBodySpec {
        when {
            requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
                val builder = MultipartBodyBuilder()
                (requestConfig.body as Map>).forEach { (name, part) ->
                    if (part.body != null) {
                        val partBuilder = builder.part(name, part.body)
                        val partHeaders = part.headers
                        partHeaders.forEach { partBuilder.header(it.key, it.value) }
                    }
                }
                return apply { bodyValue(builder.build()) }
            }
            else -> {
                return apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
            }
        }
    }
}

{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}inline fun  parseDateToQueryString(value : T): String {
        {{#toJson}}
        /*
        .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.
        */
        {{#jackson}}
        return Serializer.jacksonObjectMapper.writeValueAsString(value).replace("\"", "")
        {{/jackson}}
        {{/toJson}}
        {{^toJson}}
        return value.toString()
        {{/toJson}}
    }