kotlin-client.libraries.multiplatform.api.mustache Maven / Gradle / Ivy
{{>licenseInfo}}
package {{apiPackage}}
{{#imports}}import {{import}}
{{/imports}}
import {{packageName}}.infrastructure.*
import io.ktor.client.request.forms.formData
import kotlinx.serialization.UnstableDefault
import io.ktor.client.engine.HttpClientEngine
import io.ktor.client.features.json.serializer.KotlinxSerializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonConfiguration
import io.ktor.http.ParametersBuilder
import kotlinx.serialization.*
import kotlinx.serialization.internal.StringDescriptor
{{#operations}}
{{#nonPublicApi}}internal {{/nonPublicApi}}class {{classname}} @UseExperimental(UnstableDefault::class) constructor(
baseUrl: kotlin.String = "{{{basePath}}}",
httpClientEngine: HttpClientEngine? = null,
serializer: KotlinxSerializer
) : ApiClient(baseUrl, httpClientEngine, serializer) {
@UseExperimental(UnstableDefault::class)
constructor(
baseUrl: kotlin.String = "{{{basePath}}}",
httpClientEngine: HttpClientEngine? = null,
jsonConfiguration: JsonConfiguration = JsonConfiguration.Default
) : this(baseUrl, httpClientEngine, KotlinxSerializer(Json(jsonConfiguration)))
{{#operation}}
/**
* {{summary}}
* {{notes}}
{{#allParams}} * @param {{{paramName}}} {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}} * @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
*/
{{#returnType}}
@Suppress("UNCHECKED_CAST")
{{/returnType}}
suspend fun {{operationId}}({{#allParams}}{{{paramName}}}: {{{dataType}}}{{^required}}?{{/required}}{{^-last}}, {{/-last}}{{/allParams}}): HttpResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}> {
val localVariableAuthNames = listOf({{#authMethods}}"{{name}}"{{^-last}}, {{/-last}}{{/authMethods}})
val localVariableBody = {{#hasBodyParam}}{{#bodyParam}}{{#isArray}}{{operationIdCamelCase}}Request({{{paramName}}}{{^isList}}.asList(){{/isList}}){{/isArray}}{{^isArray}}{{#isMap}}{{operationIdCamelCase}}Request({{{paramName}}}){{/isMap}}{{^isMap}}{{{paramName}}}{{/isMap}}{{/isArray}}{{/bodyParam}}{{/hasBodyParam}}
{{^hasBodyParam}}
{{#hasFormParams}}
{{#isMultipart}}
formData {
{{#formParams}}
{{{paramName}}}?.apply { append("{{{baseName}}}", {{{paramName}}}) }
{{/formParams}}
}
{{/isMultipart}}
{{^isMultipart}}
ParametersBuilder().also {
{{#formParams}}
{{{paramName}}}?.apply { it.append("{{{baseName}}}", {{{paramName}}}.toString()) }
{{/formParams}}
}.build()
{{/isMultipart}}
{{/hasFormParams}}
{{^hasFormParams}}
io.ktor.client.utils.EmptyContent
{{/hasFormParams}}
{{/hasBodyParam}}
val localVariableQuery = mutableMapOf>()
{{#queryParams}}
{{{paramName}}}?.apply { localVariableQuery["{{baseName}}"] = {{#isContainer}}toMultiValue(this, "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf("${{{paramName}}}"){{/isContainer}} }
{{/queryParams}}
val localVariableHeaders = mutableMapOf()
{{#headerParams}}
{{{paramName}}}?.apply { localVariableHeaders["{{baseName}}"] = {{#isContainer}}this.joinToString(separator = collectionDelimiter("{{collectionFormat}}")){{/isContainer}}{{^isContainer}}this.toString(){{/isContainer}} }
{{/headerParams}}
val localVariableConfig = RequestConfig(
RequestMethod.{{httpMethod}},
"{{path}}"{{#pathParams}}.replace("{" + "{{baseName}}" + "}", "${{{paramName}}}"){{/pathParams}},
query = localVariableQuery,
headers = localVariableHeaders
)
return {{#hasBodyParam}}jsonRequest{{/hasBodyParam}}{{^hasBodyParam}}{{#hasFormParams}}{{#isMultipart}}multipartFormRequest{{/isMultipart}}{{^isMultipart}}urlEncodedFormRequest{{/isMultipart}}{{/hasFormParams}}{{^hasFormParams}}request{{/hasFormParams}}{{/hasBodyParam}}(
localVariableConfig,
localVariableBody,
localVariableAuthNames
).{{#isArray}}wrap<{{operationIdCamelCase}}Response>().map { value{{^isList}}.toTypedArray(){{/isList}} }{{/isArray}}{{^isArray}}{{#isMap}}wrap<{{operationIdCamelCase}}Response>().map { value }{{/isMap}}{{^isMap}}wrap(){{/isMap}}{{/isArray}}
}
{{#hasBodyParam}}
{{#bodyParam}}
{{#isArray}}{{>serial_wrapper_request_list}}{{/isArray}}{{#isMap}}{{>serial_wrapper_request_map}}{{/isMap}}
{{/bodyParam}}
{{/hasBodyParam}}
{{#isArray}}
{{>serial_wrapper_response_list}}
{{/isArray}}
{{#isMap}}
{{>serial_wrapper_response_map}}
{{/isMap}}
{{/operation}}
{{#nonPublicApi}}internal {{/nonPublicApi}}companion object {
internal fun setMappers(serializer: KotlinxSerializer) {
{{#operation}}
{{#hasBodyParam}}
{{#bodyParam}}
{{#isArray}}serializer.setMapper({{operationIdCamelCase}}Request::class, {{operationIdCamelCase}}Request.serializer()){{/isArray}}{{#isMap}}serializer.setMapper({{operationIdCamelCase}}Request::class, {{operationIdCamelCase}}Request.serializer()){{/isMap}}
{{/bodyParam}}
{{/hasBodyParam}}
{{#isArray}}
serializer.setMapper({{operationIdCamelCase}}Response::class, {{operationIdCamelCase}}Response.serializer())
{{/isArray}}
{{#isMap}}
serializer.setMapper({{operationIdCamelCase}}Response::class, {{operationIdCamelCase}}Response.serializer())
{{/isMap}}
{{/operation}}
}
}
}
{{/operations}}