Skip to content

Commit 9c0443b

Browse files
committed
Kotlin style formatting
1 parent fa29397 commit 9c0443b

9 files changed

+61
-65
lines changed

src/main/resources/handlebars/kotlin-client/api.mustache

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ class {{classname}}(basePath: kotlin.String = "{{{basePath}}}") : ApiClient(base
4343
ResponseType.Redirection -> TODO()
4444
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
4545
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
46-
else -> throw kotlin.IllegalStateException("Undefined ResponseType.")
4746
}
4847
}
49-
5048
{{/contents}}
5149
{{/operation}}
5250
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
2-
* {{{description}}}
3-
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^@last}},{{/@last}}{{/enumVars}}{{/allowableValues}}
4-
*/
2+
* {{{description}}}
3+
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^@last}},{{/@last}}{{/enumVars}}{{/allowableValues}}
4+
*/
55
enum class {{classname}}(val value: {{dataType}}){
66
{{#allowableValues}}{{#enumVars}}
77
{{&name}}({{{value}}}){{^@last}},{{/@last}}{{#@last}};{{/@last}}
88
{{/enumVars}}{{/allowableValues}}
9-
}
9+
}

src/main/resources/handlebars/kotlin-client/infrastructure/ApiAbstractions.kt.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package {{packageName}}.infrastructure
22

3-
typealias MultiValueMap = Map<String,List<String>>
3+
typealias MultiValueMap = Map<String, List<String>>
44

5-
fun collectionDelimiter(collectionFormat: String) = when(collectionFormat) {
5+
fun collectionDelimiter(collectionFormat: String) = when (collectionFormat) {
66
"csv" -> ","
77
"tsv" -> "\t"
88
"pipes" -> "|"
@@ -12,8 +12,8 @@ fun collectionDelimiter(collectionFormat: String) = when(collectionFormat) {
1212

1313
val defaultMultiValueConverter: (item: Any?) -> String = { item -> "$item" }
1414

15-
fun <T: Any?> toMultiValue(items: List<T>, collectionFormat: String, map: (item: Any?) -> String = defaultMultiValueConverter): List<String> {
16-
return when(collectionFormat) {
15+
fun <T : Any?> toMultiValue(items: List<T>, collectionFormat: String, map: (item: Any?) -> String = defaultMultiValueConverter): List<String> {
16+
return when (collectionFormat) {
1717
"multi" -> items.map(map)
1818
else -> listOf(items.map(map).joinToString(separator = collectionDelimiter(collectionFormat)))
1919
}

src/main/resources/handlebars/kotlin-client/infrastructure/ApiClient.kt.mustache

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import java.io.File
55

66
open class ApiClient(val baseUrl: String) {
77
companion object {
8-
protected val ContentType = "Content-Type"
9-
protected val Accept = "Accept"
10-
protected val JsonMediaType = "application/json"
11-
protected val FormDataMediaType = "multipart/form-data"
12-
protected val XmlMediaType = "application/xml"
8+
protected const val ContentType = "Content-Type"
9+
protected const val Accept = "Accept"
10+
protected const val JsonMediaType = "application/json"
11+
protected const val FormDataMediaType = "multipart/form-data"
12+
protected const val XmlMediaType = "application/xml"
1313
1414
@JvmStatic
15-
val client : OkHttpClient = OkHttpClient()
15+
val client: OkHttpClient = OkHttpClient()
1616
1717
@JvmStatic
1818
var defaultHeaders: Map<String, String> by ApplicationDelegates.setOnce(mapOf(ContentType to JsonMediaType, Accept to JsonMediaType))
@@ -21,40 +21,37 @@ open class ApiClient(val baseUrl: String) {
2121
val jsonHeaders: Map<String, String> = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType)
2222
}
2323

24-
inline protected fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody {
25-
if(content is File) {
26-
return RequestBody.create(
27-
MediaType.parse(mediaType), content
28-
)
29-
} else if(mediaType == FormDataMediaType) {
30-
var builder = FormBody.Builder()
31-
// content's type *must* be Map<String, Any>
32-
@Suppress("UNCHECKED_CAST")
33-
(content as Map<String,String>).forEach { key, value ->
34-
builder = builder.add(key, value)
24+
protected inline fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody =
25+
when {
26+
content is File -> RequestBody.create(MediaType.parse(mediaType), content)
27+
28+
mediaType == FormDataMediaType -> {
29+
var builder = FormBody.Builder()
30+
// content's type *must* be Map<String, Any>
31+
@Suppress("UNCHECKED_CAST")
32+
(content as Map<String, String>).forEach { key, value ->
33+
builder = builder.add(key, value)
34+
}
35+
builder.build()
36+
}
37+
mediaType == JsonMediaType -> RequestBody.create(
38+
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
39+
)
40+
mediaType == XmlMediaType -> TODO("xml not currently supported.")
41+
42+
// TODO: this should be extended with other serializers
43+
else -> TODO("requestBody currently only supports JSON body and File body.")
3544
}
36-
return builder.build()
37-
} else if(mediaType == JsonMediaType) {
38-
return RequestBody.create(
39-
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
40-
)
41-
} else if (mediaType == XmlMediaType) {
42-
TODO("xml not currently supported.")
43-
}
44-
45-
// TODO: this should be extended with other serializers
46-
TODO("requestBody currently only supports JSON body and File body.")
47-
}
4845
49-
inline protected fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
50-
if(body == null) return null
51-
return when(mediaType) {
46+
protected inline fun <reified T : Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
47+
if (body == null) return null
48+
return when (mediaType) {
5249
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source())
5350
else -> TODO()
5451
}
5552
}
5653
57-
inline protected fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
54+
protected inline fun <reified T : Any?> request(requestConfig: RequestConfig, body: Any? = null): ApiInfrastructureResponse<T?> {
5855
val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.")
5956
6057
var urlBuilder = httpUrl.newBuilder()
@@ -69,19 +66,19 @@ open class ApiClient(val baseUrl: String) {
6966
val url = urlBuilder.build()
7067
val headers = requestConfig.headers + defaultHeaders
7168
72-
if(headers[ContentType] ?: "" == "") {
69+
if (headers[ContentType] ?: "" == "") {
7370
throw kotlin.IllegalStateException("Missing Content-Type header. This is required.")
7471
}
7572
76-
if(headers[Accept] ?: "" == "") {
73+
if (headers[Accept] ?: "" == "") {
7774
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
7875
}
7976
8077
// TODO: support multiple contentType,accept options here.
8178
val contentType = (headers[ContentType] as String).substringBefore(";").toLowerCase()
8279
val accept = (headers[Accept] as String).substringBefore(";").toLowerCase()
8380
84-
var request : Request.Builder = when (requestConfig.method) {
81+
var request: Request.Builder = when (requestConfig.method) {
8582
RequestMethod.DELETE -> Request.Builder().url(url).delete()
8683
RequestMethod.GET -> Request.Builder().url(url)
8784
RequestMethod.HEAD -> Request.Builder().url(url).head()

src/main/resources/handlebars/kotlin-client/infrastructure/ApiInfrastructureResponse.kt.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ enum class ResponseType {
66

77
abstract class ApiInfrastructureResponse<T>(val responseType: ResponseType) {
88
abstract val statusCode: Int
9-
abstract val headers: Map<String,List<String>>
9+
abstract val headers: Map<String, List<String>>
1010
}
1111

1212
class Success<T>(
1313
val data: T,
1414
override val statusCode: Int = -1,
1515
override val headers: Map<String, List<String>> = mapOf()
16-
): ApiInfrastructureResponse<T>(ResponseType.Success)
16+
) : ApiInfrastructureResponse<T>(ResponseType.Success)
1717

1818
class Informational<T>(
1919
val statusText: String,
@@ -37,4 +37,4 @@ class ServerError<T>(
3737
val body: Any? = null,
3838
override val statusCode: Int = -1,
3939
override val headers: Map<String, List<String>>
40-
): ApiInfrastructureResponse<T>(ResponseType.ServerError)
40+
) : ApiInfrastructureResponse<T>(ResponseType.ServerError)

src/main/resources/handlebars/kotlin-client/infrastructure/ApplicationDelegates.kt.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object ApplicationDelegates {
99
*
1010
* If unset (no default value), a get on the property will throw [IllegalStateException].
1111
*/
12-
fun <T> setOnce(defaultValue: T? = null) : ReadWriteProperty<Any?, T> = SetOnce(defaultValue)
12+
fun <T> setOnce(defaultValue: T? = null): ReadWriteProperty<Any?, T> = SetOnce(defaultValue)
1313
1414
private class SetOnce<T>(defaultValue: T? = null) : ReadWriteProperty<Any?, T> {
1515
private var isSet = false

src/main/resources/handlebars/kotlin-client/infrastructure/RequestConfig.kt.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ data class RequestConfig(
1212
val method: RequestMethod,
1313
val path: String,
1414
val headers: Map<String, String> = mapOf(),
15-
val query: Map<String, List<String>> = mapOf())
15+
val query: Map<String, List<String>> = mapOf()
16+
)

src/main/resources/handlebars/kotlin-client/infrastructure/ResponseExtensions.kt.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import okhttp3.Response
55
/**
66
* Provides an extension to evaluation whether the response is a 1xx code
77
*/
8-
val Response.isInformational : Boolean get() = this.code() in 100..199
8+
val Response.isInformational: Boolean get() = this.code() in 100..199
99

1010
/**
1111
* Provides an extension to evaluation whether the response is a 3xx code
1212
*/
13-
val Response.isRedirect : Boolean get() = this.code() in 300..399
13+
val Response.isRedirect: Boolean get() = this.code() in 300..399
1414

1515
/**
1616
* Provides an extension to evaluation whether the response is a 4xx code
1717
*/
18-
val Response.isClientError : Boolean get() = this.code() in 400..499
18+
val Response.isClientError: Boolean get() = this.code() in 400..499
1919

2020
/**
2121
* Provides an extension to evaluation whether the response is a 5xx (Standard) through 999 (non-standard) code
2222
*/
23-
val Response.isServerError : Boolean get() = this.code() in 500..999
23+
val Response.isServerError: Boolean get() = this.code() in 500..999
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
2-
* {{{appName}}}
3-
* {{{appDescription}}}
4-
*
5-
* {{#version}}OpenAPI spec version: {{{version}}}{{/version}}
6-
* {{#infoEmail}}Contact: {{{infoEmail}}}{{/infoEmail}}
7-
*
8-
* NOTE: This class is auto generated by the swagger code generator program.
9-
* https://github.com/swagger-api/swagger-codegen.git
10-
* Do not edit the class manually.
11-
*/
2+
* {{{appName}}}
3+
* {{{appDescription}}}
4+
*
5+
* {{#version}}OpenAPI spec version: {{{version}}}{{/version}}
6+
* {{#infoEmail}}Contact: {{{infoEmail}}}{{/infoEmail}}
7+
*
8+
* NOTE: This class is auto generated by the swagger code generator program.
9+
* https://github.com/swagger-api/swagger-codegen.git
10+
* Do not edit the class manually.
11+
*/

0 commit comments

Comments
 (0)