forked from loafle/openapi-generator-original
Fix some Kotlin formatting issues and make source more Kotlin like (#427)
This commit is contained in:
parent
bece8d2a39
commit
c1eda61874
@ -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
|
||||||
}
|
else -> TODO("requestBody currently only supports JSON body and File body.")
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: this should be extended with other serializers
|
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
|
||||||
TODO("requestBody currently only supports JSON body and File body.")
|
|
||||||
}
|
|
||||||
|
|
||||||
inline protected 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()
|
||||||
|
@ -10,31 +10,31 @@ abstract class ApiInfrastructureResponse<T>(val responseType: ResponseType) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Success<T>(
|
class Success<T>(
|
||||||
val data: T,
|
val data: T,
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>> = mapOf()
|
override val headers: Map<String, List<String>> = mapOf()
|
||||||
): ApiInfrastructureResponse<T>(ResponseType.Success)
|
): ApiInfrastructureResponse<T>(ResponseType.Success)
|
||||||
|
|
||||||
class Informational<T>(
|
class Informational<T>(
|
||||||
val statusText: String,
|
val statusText: String,
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>> = mapOf()
|
override val headers: Map<String, List<String>> = mapOf()
|
||||||
) : ApiInfrastructureResponse<T>(ResponseType.Informational)
|
) : ApiInfrastructureResponse<T>(ResponseType.Informational)
|
||||||
|
|
||||||
class Redirection<T>(
|
class Redirection<T>(
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>> = mapOf()
|
override val headers: Map<String, List<String>> = mapOf()
|
||||||
) : ApiInfrastructureResponse<T>(ResponseType.Redirection)
|
) : ApiInfrastructureResponse<T>(ResponseType.Redirection)
|
||||||
|
|
||||||
class ClientError<T>(
|
class ClientError<T>(
|
||||||
val body: Any? = null,
|
val body: Any? = null,
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>> = mapOf()
|
override val headers: Map<String, List<String>> = mapOf()
|
||||||
) : ApiInfrastructureResponse<T>(ResponseType.ClientError)
|
) : ApiInfrastructureResponse<T>(ResponseType.ClientError)
|
||||||
|
|
||||||
class ServerError<T>(
|
class ServerError<T>(
|
||||||
val message: String? = null,
|
val message: String? = null,
|
||||||
val body: Any? = null,
|
val body: Any? = null,
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>>
|
override val headers: Map<String, List<String>>
|
||||||
): ApiInfrastructureResponse<T>(ResponseType.ServerError)
|
): ApiInfrastructureResponse<T>(ResponseType.ServerError)
|
@ -9,7 +9,8 @@ package {{packageName}}.infrastructure
|
|||||||
* multi-valued headers as csv-only.
|
* multi-valued headers as csv-only.
|
||||||
*/
|
*/
|
||||||
data class RequestConfig(
|
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()
|
||||||
|
)
|
@ -8,7 +8,7 @@ import java.util.*
|
|||||||
object Serializer {
|
object Serializer {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
val moshi: Moshi = Moshi.Builder()
|
val moshi: Moshi = Moshi.Builder()
|
||||||
.add(KotlinJsonAdapterFactory())
|
.add(KotlinJsonAdapterFactory())
|
||||||
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
|
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
else -> TODO("requestBody currently only supports JSON body and File body.")
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: this should be extended with other serializers
|
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
|
||||||
TODO("requestBody currently only supports JSON body and File body.")
|
|
||||||
}
|
|
||||||
|
|
||||||
inline protected 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()
|
||||||
|
@ -10,31 +10,31 @@ abstract class ApiInfrastructureResponse<T>(val responseType: ResponseType) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Success<T>(
|
class Success<T>(
|
||||||
val data: T,
|
val data: T,
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>> = mapOf()
|
override val headers: Map<String, List<String>> = mapOf()
|
||||||
): ApiInfrastructureResponse<T>(ResponseType.Success)
|
): ApiInfrastructureResponse<T>(ResponseType.Success)
|
||||||
|
|
||||||
class Informational<T>(
|
class Informational<T>(
|
||||||
val statusText: String,
|
val statusText: String,
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>> = mapOf()
|
override val headers: Map<String, List<String>> = mapOf()
|
||||||
) : ApiInfrastructureResponse<T>(ResponseType.Informational)
|
) : ApiInfrastructureResponse<T>(ResponseType.Informational)
|
||||||
|
|
||||||
class Redirection<T>(
|
class Redirection<T>(
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>> = mapOf()
|
override val headers: Map<String, List<String>> = mapOf()
|
||||||
) : ApiInfrastructureResponse<T>(ResponseType.Redirection)
|
) : ApiInfrastructureResponse<T>(ResponseType.Redirection)
|
||||||
|
|
||||||
class ClientError<T>(
|
class ClientError<T>(
|
||||||
val body: Any? = null,
|
val body: Any? = null,
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>> = mapOf()
|
override val headers: Map<String, List<String>> = mapOf()
|
||||||
) : ApiInfrastructureResponse<T>(ResponseType.ClientError)
|
) : ApiInfrastructureResponse<T>(ResponseType.ClientError)
|
||||||
|
|
||||||
class ServerError<T>(
|
class ServerError<T>(
|
||||||
val message: String? = null,
|
val message: String? = null,
|
||||||
val body: Any? = null,
|
val body: Any? = null,
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>>
|
override val headers: Map<String, List<String>>
|
||||||
): ApiInfrastructureResponse<T>(ResponseType.ServerError)
|
): ApiInfrastructureResponse<T>(ResponseType.ServerError)
|
@ -9,7 +9,8 @@ package org.openapitools.client.infrastructure
|
|||||||
* multi-valued headers as csv-only.
|
* multi-valued headers as csv-only.
|
||||||
*/
|
*/
|
||||||
data class RequestConfig(
|
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()
|
||||||
|
)
|
@ -8,7 +8,7 @@ import java.util.*
|
|||||||
object Serializer {
|
object Serializer {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
val moshi: Moshi = Moshi.Builder()
|
val moshi: Moshi = Moshi.Builder()
|
||||||
.add(KotlinJsonAdapterFactory())
|
.add(KotlinJsonAdapterFactory())
|
||||||
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
|
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
else -> TODO("requestBody currently only supports JSON body and File body.")
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: this should be extended with other serializers
|
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
|
||||||
TODO("requestBody currently only supports JSON body and File body.")
|
|
||||||
}
|
|
||||||
|
|
||||||
inline protected 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()
|
||||||
|
@ -10,31 +10,31 @@ abstract class ApiInfrastructureResponse<T>(val responseType: ResponseType) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Success<T>(
|
class Success<T>(
|
||||||
val data: T,
|
val data: T,
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>> = mapOf()
|
override val headers: Map<String, List<String>> = mapOf()
|
||||||
): ApiInfrastructureResponse<T>(ResponseType.Success)
|
): ApiInfrastructureResponse<T>(ResponseType.Success)
|
||||||
|
|
||||||
class Informational<T>(
|
class Informational<T>(
|
||||||
val statusText: String,
|
val statusText: String,
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>> = mapOf()
|
override val headers: Map<String, List<String>> = mapOf()
|
||||||
) : ApiInfrastructureResponse<T>(ResponseType.Informational)
|
) : ApiInfrastructureResponse<T>(ResponseType.Informational)
|
||||||
|
|
||||||
class Redirection<T>(
|
class Redirection<T>(
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>> = mapOf()
|
override val headers: Map<String, List<String>> = mapOf()
|
||||||
) : ApiInfrastructureResponse<T>(ResponseType.Redirection)
|
) : ApiInfrastructureResponse<T>(ResponseType.Redirection)
|
||||||
|
|
||||||
class ClientError<T>(
|
class ClientError<T>(
|
||||||
val body: Any? = null,
|
val body: Any? = null,
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>> = mapOf()
|
override val headers: Map<String, List<String>> = mapOf()
|
||||||
) : ApiInfrastructureResponse<T>(ResponseType.ClientError)
|
) : ApiInfrastructureResponse<T>(ResponseType.ClientError)
|
||||||
|
|
||||||
class ServerError<T>(
|
class ServerError<T>(
|
||||||
val message: String? = null,
|
val message: String? = null,
|
||||||
val body: Any? = null,
|
val body: Any? = null,
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>>
|
override val headers: Map<String, List<String>>
|
||||||
): ApiInfrastructureResponse<T>(ResponseType.ServerError)
|
): ApiInfrastructureResponse<T>(ResponseType.ServerError)
|
@ -9,7 +9,8 @@ package org.openapitools.client.infrastructure
|
|||||||
* multi-valued headers as csv-only.
|
* multi-valued headers as csv-only.
|
||||||
*/
|
*/
|
||||||
data class RequestConfig(
|
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()
|
||||||
|
)
|
@ -8,7 +8,7 @@ import java.util.*
|
|||||||
object Serializer {
|
object Serializer {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
val moshi: Moshi = Moshi.Builder()
|
val moshi: Moshi = Moshi.Builder()
|
||||||
.add(KotlinJsonAdapterFactory())
|
.add(KotlinJsonAdapterFactory())
|
||||||
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
|
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
else -> TODO("requestBody currently only supports JSON body and File body.")
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: this should be extended with other serializers
|
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
|
||||||
TODO("requestBody currently only supports JSON body and File body.")
|
|
||||||
}
|
|
||||||
|
|
||||||
inline protected 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()
|
||||||
|
@ -10,31 +10,31 @@ abstract class ApiInfrastructureResponse<T>(val responseType: ResponseType) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Success<T>(
|
class Success<T>(
|
||||||
val data: T,
|
val data: T,
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>> = mapOf()
|
override val headers: Map<String, List<String>> = mapOf()
|
||||||
): ApiInfrastructureResponse<T>(ResponseType.Success)
|
): ApiInfrastructureResponse<T>(ResponseType.Success)
|
||||||
|
|
||||||
class Informational<T>(
|
class Informational<T>(
|
||||||
val statusText: String,
|
val statusText: String,
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>> = mapOf()
|
override val headers: Map<String, List<String>> = mapOf()
|
||||||
) : ApiInfrastructureResponse<T>(ResponseType.Informational)
|
) : ApiInfrastructureResponse<T>(ResponseType.Informational)
|
||||||
|
|
||||||
class Redirection<T>(
|
class Redirection<T>(
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>> = mapOf()
|
override val headers: Map<String, List<String>> = mapOf()
|
||||||
) : ApiInfrastructureResponse<T>(ResponseType.Redirection)
|
) : ApiInfrastructureResponse<T>(ResponseType.Redirection)
|
||||||
|
|
||||||
class ClientError<T>(
|
class ClientError<T>(
|
||||||
val body: Any? = null,
|
val body: Any? = null,
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>> = mapOf()
|
override val headers: Map<String, List<String>> = mapOf()
|
||||||
) : ApiInfrastructureResponse<T>(ResponseType.ClientError)
|
) : ApiInfrastructureResponse<T>(ResponseType.ClientError)
|
||||||
|
|
||||||
class ServerError<T>(
|
class ServerError<T>(
|
||||||
val message: String? = null,
|
val message: String? = null,
|
||||||
val body: Any? = null,
|
val body: Any? = null,
|
||||||
override val statusCode: Int = -1,
|
override val statusCode: Int = -1,
|
||||||
override val headers: Map<String, List<String>>
|
override val headers: Map<String, List<String>>
|
||||||
): ApiInfrastructureResponse<T>(ResponseType.ServerError)
|
): ApiInfrastructureResponse<T>(ResponseType.ServerError)
|
@ -9,7 +9,8 @@ package org.openapitools.client.infrastructure
|
|||||||
* multi-valued headers as csv-only.
|
* multi-valued headers as csv-only.
|
||||||
*/
|
*/
|
||||||
data class RequestConfig(
|
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()
|
||||||
|
)
|
@ -8,7 +8,7 @@ import java.util.*
|
|||||||
object Serializer {
|
object Serializer {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
val moshi: Moshi = Moshi.Builder()
|
val moshi: Moshi = Moshi.Builder()
|
||||||
.add(KotlinJsonAdapterFactory())
|
.add(KotlinJsonAdapterFactory())
|
||||||
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
|
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
@ -1 +1 @@
|
|||||||
3.0.2-SNAPSHOT
|
3.1.0-SNAPSHOT
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
|
This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
|
||||||
|
|
||||||
Generated by OpenAPI Generator 3.0.2-SNAPSHOT.
|
Generated by OpenAPI Generator 3.1.0-SNAPSHOT.
|
||||||
|
|
||||||
## Requires
|
## Requires
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user