Fix some Kotlin formatting issues and make source more Kotlin like (#427)

This commit is contained in:
Günther Grill 2018-07-03 08:25:48 +02:00 committed by Jérémie Bresson
parent bece8d2a39
commit c1eda61874
18 changed files with 206 additions and 214 deletions

View File

@ -5,11 +5,11 @@ import java.io.File
open class ApiClient(val baseUrl: String) { open class ApiClient(val baseUrl: String) {
companion object { companion object {
protected val ContentType = "Content-Type" protected const val ContentType = "Content-Type"
protected val Accept = "Accept" protected const val Accept = "Accept"
protected val JsonMediaType = "application/json" protected const val JsonMediaType = "application/json"
protected val FormDataMediaType = "multipart/form-data" protected const val FormDataMediaType = "multipart/form-data"
protected val XmlMediaType = "application/xml" protected const val XmlMediaType = "application/xml"
@JvmStatic @JvmStatic
val client by lazy { val client by lazy {
@ -26,32 +26,29 @@ open class ApiClient(val baseUrl: String) {
val jsonHeaders: Map<String, String> = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType) val jsonHeaders: Map<String, String> = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType)
} }
inline protected fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody { protected inline fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody =
if(content is File) { when {
return RequestBody.create( content is File -> RequestBody.create(
MediaType.parse(mediaType), content MediaType.parse(mediaType), content
) )
} else if(mediaType == FormDataMediaType) { mediaType == FormDataMediaType -> {
var builder = FormBody.Builder() var builder = FormBody.Builder()
// content's type *must* be Map<String, Any> // content's type *must* be Map<String, Any>
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
(content as Map<String,String>).forEach { key, value -> (content as Map<String,String>).forEach { key, value ->
builder = builder.add(key, value) builder = builder.add(key, value)
} }
return builder.build() builder.build()
} else if(mediaType == JsonMediaType) { }
return RequestBody.create( mediaType == JsonMediaType -> RequestBody.create(
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content) MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
) )
} else if (mediaType == XmlMediaType) { mediaType == XmlMediaType -> TODO("xml not currently supported.")
TODO("xml not currently supported.")
}
// TODO: this should be extended with other serializers // TODO: this should be extended with other serializers
TODO("requestBody currently only supports JSON body and File body.") else -> TODO("requestBody currently only supports JSON body and File body.")
} }
inline protected fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? { protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
if(body == null) return null if(body == null) return null
return when(mediaType) { return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source()) JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source())
@ -59,7 +56,7 @@ open class ApiClient(val baseUrl: String) {
} }
} }
inline protected fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> { protected inline fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.") val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.")
var urlBuilder = httpUrl.newBuilder() var urlBuilder = httpUrl.newBuilder()

View File

@ -12,4 +12,5 @@ data class RequestConfig(
val method: RequestMethod, val method: RequestMethod,
val path: String, val path: String,
val headers: Map<String, String> = mapOf(), val headers: Map<String, String> = mapOf(),
val query: Map<String, List<String>> = mapOf()) val query: Map<String, List<String>> = mapOf()
)

View File

@ -5,11 +5,11 @@ import java.io.File
open class ApiClient(val baseUrl: String) { open class ApiClient(val baseUrl: String) {
companion object { companion object {
protected val ContentType = "Content-Type" protected const val ContentType = "Content-Type"
protected val Accept = "Accept" protected const val Accept = "Accept"
protected val JsonMediaType = "application/json" protected const val JsonMediaType = "application/json"
protected val FormDataMediaType = "multipart/form-data" protected const val FormDataMediaType = "multipart/form-data"
protected val XmlMediaType = "application/xml" protected const val XmlMediaType = "application/xml"
@JvmStatic @JvmStatic
val client by lazy { val client by lazy {
@ -26,32 +26,29 @@ open class ApiClient(val baseUrl: String) {
val jsonHeaders: Map<String, String> = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType) val jsonHeaders: Map<String, String> = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType)
} }
inline protected fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody { protected inline fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody =
if(content is File) { when {
return RequestBody.create( content is File -> RequestBody.create(
MediaType.parse(mediaType), content MediaType.parse(mediaType), content
) )
} else if(mediaType == FormDataMediaType) { mediaType == FormDataMediaType -> {
var builder = FormBody.Builder() var builder = FormBody.Builder()
// content's type *must* be Map<String, Any> // content's type *must* be Map<String, Any>
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
(content as Map<String,String>).forEach { key, value -> (content as Map<String,String>).forEach { key, value ->
builder = builder.add(key, value) builder = builder.add(key, value)
} }
return builder.build() builder.build()
} else if(mediaType == JsonMediaType) { }
return RequestBody.create( mediaType == JsonMediaType -> RequestBody.create(
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content) MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
) )
} else if (mediaType == XmlMediaType) { mediaType == XmlMediaType -> TODO("xml not currently supported.")
TODO("xml not currently supported.")
}
// TODO: this should be extended with other serializers // TODO: this should be extended with other serializers
TODO("requestBody currently only supports JSON body and File body.") else -> TODO("requestBody currently only supports JSON body and File body.")
} }
inline protected fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? { protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
if(body == null) return null if(body == null) return null
return when(mediaType) { return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source()) JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source())
@ -59,7 +56,7 @@ open class ApiClient(val baseUrl: String) {
} }
} }
inline protected fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> { protected inline fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.") val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.")
var urlBuilder = httpUrl.newBuilder() var urlBuilder = httpUrl.newBuilder()

View File

@ -12,4 +12,5 @@ data class RequestConfig(
val method: RequestMethod, val method: RequestMethod,
val path: String, val path: String,
val headers: Map<String, String> = mapOf(), val headers: Map<String, String> = mapOf(),
val query: Map<String, List<String>> = mapOf()) val query: Map<String, List<String>> = mapOf()
)

View File

@ -5,11 +5,11 @@ import java.io.File
open class ApiClient(val baseUrl: String) { open class ApiClient(val baseUrl: String) {
companion object { companion object {
protected val ContentType = "Content-Type" protected const val ContentType = "Content-Type"
protected val Accept = "Accept" protected const val Accept = "Accept"
protected val JsonMediaType = "application/json" protected const val JsonMediaType = "application/json"
protected val FormDataMediaType = "multipart/form-data" protected const val FormDataMediaType = "multipart/form-data"
protected val XmlMediaType = "application/xml" protected const val XmlMediaType = "application/xml"
@JvmStatic @JvmStatic
val client by lazy { val client by lazy {
@ -26,32 +26,29 @@ open class ApiClient(val baseUrl: String) {
val jsonHeaders: Map<String, String> = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType) val jsonHeaders: Map<String, String> = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType)
} }
inline protected fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody { protected inline fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody =
if(content is File) { when {
return RequestBody.create( content is File -> RequestBody.create(
MediaType.parse(mediaType), content MediaType.parse(mediaType), content
) )
} else if(mediaType == FormDataMediaType) { mediaType == FormDataMediaType -> {
var builder = FormBody.Builder() var builder = FormBody.Builder()
// content's type *must* be Map<String, Any> // content's type *must* be Map<String, Any>
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
(content as Map<String,String>).forEach { key, value -> (content as Map<String,String>).forEach { key, value ->
builder = builder.add(key, value) builder = builder.add(key, value)
} }
return builder.build() builder.build()
} else if(mediaType == JsonMediaType) { }
return RequestBody.create( mediaType == JsonMediaType -> RequestBody.create(
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content) MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
) )
} else if (mediaType == XmlMediaType) { mediaType == XmlMediaType -> TODO("xml not currently supported.")
TODO("xml not currently supported.")
}
// TODO: this should be extended with other serializers // TODO: this should be extended with other serializers
TODO("requestBody currently only supports JSON body and File body.") else -> TODO("requestBody currently only supports JSON body and File body.")
} }
inline protected fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? { protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
if(body == null) return null if(body == null) return null
return when(mediaType) { return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source()) JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source())
@ -59,7 +56,7 @@ open class ApiClient(val baseUrl: String) {
} }
} }
inline protected fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> { protected inline fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.") val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.")
var urlBuilder = httpUrl.newBuilder() var urlBuilder = httpUrl.newBuilder()

View File

@ -12,4 +12,5 @@ data class RequestConfig(
val method: RequestMethod, val method: RequestMethod,
val path: String, val path: String,
val headers: Map<String, String> = mapOf(), val headers: Map<String, String> = mapOf(),
val query: Map<String, List<String>> = mapOf()) val query: Map<String, List<String>> = mapOf()
)

View File

@ -5,11 +5,11 @@ import java.io.File
open class ApiClient(val baseUrl: String) { open class ApiClient(val baseUrl: String) {
companion object { companion object {
protected val ContentType = "Content-Type" protected const val ContentType = "Content-Type"
protected val Accept = "Accept" protected const val Accept = "Accept"
protected val JsonMediaType = "application/json" protected const val JsonMediaType = "application/json"
protected val FormDataMediaType = "multipart/form-data" protected const val FormDataMediaType = "multipart/form-data"
protected val XmlMediaType = "application/xml" protected const val XmlMediaType = "application/xml"
@JvmStatic @JvmStatic
val client by lazy { val client by lazy {
@ -26,32 +26,29 @@ open class ApiClient(val baseUrl: String) {
val jsonHeaders: Map<String, String> = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType) val jsonHeaders: Map<String, String> = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType)
} }
inline protected fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody { protected inline fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody =
if(content is File) { when {
return RequestBody.create( content is File -> RequestBody.create(
MediaType.parse(mediaType), content MediaType.parse(mediaType), content
) )
} else if(mediaType == FormDataMediaType) { mediaType == FormDataMediaType -> {
var builder = FormBody.Builder() var builder = FormBody.Builder()
// content's type *must* be Map<String, Any> // content's type *must* be Map<String, Any>
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
(content as Map<String,String>).forEach { key, value -> (content as Map<String,String>).forEach { key, value ->
builder = builder.add(key, value) builder = builder.add(key, value)
} }
return builder.build() builder.build()
} else if(mediaType == JsonMediaType) { }
return RequestBody.create( mediaType == JsonMediaType -> RequestBody.create(
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content) MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
) )
} else if (mediaType == XmlMediaType) { mediaType == XmlMediaType -> TODO("xml not currently supported.")
TODO("xml not currently supported.")
}
// TODO: this should be extended with other serializers // TODO: this should be extended with other serializers
TODO("requestBody currently only supports JSON body and File body.") else -> TODO("requestBody currently only supports JSON body and File body.")
} }
inline protected fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? { protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
if(body == null) return null if(body == null) return null
return when(mediaType) { return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source()) JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source())
@ -59,7 +56,7 @@ open class ApiClient(val baseUrl: String) {
} }
} }
inline protected fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> { protected inline fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.") val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.")
var urlBuilder = httpUrl.newBuilder() var urlBuilder = httpUrl.newBuilder()

View File

@ -12,4 +12,5 @@ data class RequestConfig(
val method: RequestMethod, val method: RequestMethod,
val path: String, val path: String,
val headers: Map<String, String> = mapOf(), val headers: Map<String, String> = mapOf(),
val query: Map<String, List<String>> = mapOf()) val query: Map<String, List<String>> = mapOf()
)

View File

@ -1 +1 @@
3.0.2-SNAPSHOT 3.1.0-SNAPSHOT

View File

@ -2,7 +2,7 @@
This is a sample server Petstore server. For this sample, you can use the api key &#x60;special-key&#x60; to test the authorization filters. This is a sample server Petstore server. For this sample, you can use the api key &#x60;special-key&#x60; to test the authorization filters.
Generated by OpenAPI Generator 3.0.2-SNAPSHOT. Generated by OpenAPI Generator 3.1.0-SNAPSHOT.
## Requires ## Requires