Skip to content

Commit

Permalink
Fix some Kotlin formatting issues and make source more Kotlin like (#427
Browse files Browse the repository at this point in the history
)
  • Loading branch information
guenhter authored and jmini committed Jul 3, 2018
1 parent bece8d2 commit c1eda61
Show file tree
Hide file tree
Showing 18 changed files with 210 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import java.io.File

open class ApiClient(val baseUrl: String) {
companion object {
protected val ContentType = "Content-Type"
protected val Accept = "Accept"
protected val JsonMediaType = "application/json"
protected val FormDataMediaType = "multipart/form-data"
protected val XmlMediaType = "application/xml"
protected const val ContentType = "Content-Type"
protected const val Accept = "Accept"
protected const val JsonMediaType = "application/json"
protected const val FormDataMediaType = "multipart/form-data"
protected const val XmlMediaType = "application/xml"
@JvmStatic
val client by lazy {
Expand All @@ -26,40 +26,37 @@ open class ApiClient(val baseUrl: String) {
val jsonHeaders: Map<String, String> = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType)
}

inline protected fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody {
if(content is File) {
return RequestBody.create(
MediaType.parse(mediaType), content
)
} else if(mediaType == FormDataMediaType) {
var builder = FormBody.Builder()
// content's type *must* be Map<String, Any>
@Suppress("UNCHECKED_CAST")
(content as Map<String,String>).forEach { key, value ->
builder = builder.add(key, value)
}
return builder.build()
} else if(mediaType == JsonMediaType) {
return RequestBody.create(
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
)
} else if (mediaType == XmlMediaType) {
TODO("xml not currently supported.")
}
// TODO: this should be extended with other serializers
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> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody =
when {
content is File -> RequestBody.create(
MediaType.parse(mediaType), content
)
mediaType == FormDataMediaType -> {
var builder = FormBody.Builder()
// content's type *must* be Map<String, Any>
@Suppress("UNCHECKED_CAST")
(content as Map<String,String>).forEach { key, value ->
builder = builder.add(key, value)
}
builder.build()
}
mediaType == JsonMediaType -> RequestBody.create(
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
)
mediaType == XmlMediaType -> TODO("xml not currently supported.")
// TODO: this should be extended with other serializers
else -> TODO("requestBody currently only supports JSON body and File body.")
}
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
if(body == null) return null
return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source())
else -> TODO()
}
}
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.")
var urlBuilder = httpUrl.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ abstract class ApiInfrastructureResponse<T>(val responseType: ResponseType) {
}

class Success<T>(
val data: T,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
val data: T,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
): ApiInfrastructureResponse<T>(ResponseType.Success)

class Informational<T>(
val statusText: String,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
val statusText: String,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
) : ApiInfrastructureResponse<T>(ResponseType.Informational)

class Redirection<T>(
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
) : ApiInfrastructureResponse<T>(ResponseType.Redirection)

class ClientError<T>(
val body: Any? = null,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
val body: Any? = null,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
) : ApiInfrastructureResponse<T>(ResponseType.ClientError)

class ServerError<T>(
val message: String? = null,
val body: Any? = null,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>>
val message: String? = null,
val body: Any? = null,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>>
): ApiInfrastructureResponse<T>(ResponseType.ServerError)
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ package {{packageName}}.infrastructure
* multi-valued headers as csv-only.
*/
data class RequestConfig(
val method: RequestMethod,
val path: String,
val headers: Map<String, String> = mapOf(),
val query: Map<String, List<String>> = mapOf())
val method: RequestMethod,
val path: String,
val headers: Map<String, String> = mapOf(),
val query: Map<String, List<String>> = mapOf()
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import java.util.*
object Serializer {
@JvmStatic
val moshi: Moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
.build()
.add(KotlinJsonAdapterFactory())
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import java.io.File

open class ApiClient(val baseUrl: String) {
companion object {
protected val ContentType = "Content-Type"
protected val Accept = "Accept"
protected val JsonMediaType = "application/json"
protected val FormDataMediaType = "multipart/form-data"
protected val XmlMediaType = "application/xml"
protected const val ContentType = "Content-Type"
protected const val Accept = "Accept"
protected const val JsonMediaType = "application/json"
protected const val FormDataMediaType = "multipart/form-data"
protected const val XmlMediaType = "application/xml"

@JvmStatic
val client by lazy {
Expand All @@ -26,40 +26,37 @@ open class ApiClient(val baseUrl: String) {
val jsonHeaders: Map<String, String> = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType)
}

inline protected fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody {
if(content is File) {
return RequestBody.create(
MediaType.parse(mediaType), content
)
} else if(mediaType == FormDataMediaType) {
var builder = FormBody.Builder()
// content's type *must* be Map<String, Any>
@Suppress("UNCHECKED_CAST")
(content as Map<String,String>).forEach { key, value ->
builder = builder.add(key, value)
}
return builder.build()
} else if(mediaType == JsonMediaType) {
return RequestBody.create(
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
)
} else if (mediaType == XmlMediaType) {
TODO("xml not currently supported.")
}

// TODO: this should be extended with other serializers
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> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody =
when {
content is File -> RequestBody.create(
MediaType.parse(mediaType), content
)
mediaType == FormDataMediaType -> {
var builder = FormBody.Builder()
// content's type *must* be Map<String, Any>
@Suppress("UNCHECKED_CAST")
(content as Map<String,String>).forEach { key, value ->
builder = builder.add(key, value)
}
builder.build()
}
mediaType == JsonMediaType -> RequestBody.create(
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
)
mediaType == XmlMediaType -> TODO("xml not currently supported.")
// TODO: this should be extended with other serializers
else -> TODO("requestBody currently only supports JSON body and File body.")
}

protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
if(body == null) return null
return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source())
else -> TODO()
}
}

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.")

var urlBuilder = httpUrl.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ abstract class ApiInfrastructureResponse<T>(val responseType: ResponseType) {
}

class Success<T>(
val data: T,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
val data: T,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
): ApiInfrastructureResponse<T>(ResponseType.Success)

class Informational<T>(
val statusText: String,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
val statusText: String,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
) : ApiInfrastructureResponse<T>(ResponseType.Informational)

class Redirection<T>(
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
) : ApiInfrastructureResponse<T>(ResponseType.Redirection)

class ClientError<T>(
val body: Any? = null,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
val body: Any? = null,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
) : ApiInfrastructureResponse<T>(ResponseType.ClientError)

class ServerError<T>(
val message: String? = null,
val body: Any? = null,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>>
val message: String? = null,
val body: Any? = null,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>>
): ApiInfrastructureResponse<T>(ResponseType.ServerError)
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ package org.openapitools.client.infrastructure
* multi-valued headers as csv-only.
*/
data class RequestConfig(
val method: RequestMethod,
val path: String,
val headers: Map<String, String> = mapOf(),
val query: Map<String, List<String>> = mapOf())
val method: RequestMethod,
val path: String,
val headers: Map<String, String> = mapOf(),
val query: Map<String, List<String>> = mapOf()
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import java.util.*
object Serializer {
@JvmStatic
val moshi: Moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
.build()
.add(KotlinJsonAdapterFactory())
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import java.io.File

open class ApiClient(val baseUrl: String) {
companion object {
protected val ContentType = "Content-Type"
protected val Accept = "Accept"
protected val JsonMediaType = "application/json"
protected val FormDataMediaType = "multipart/form-data"
protected val XmlMediaType = "application/xml"
protected const val ContentType = "Content-Type"
protected const val Accept = "Accept"
protected const val JsonMediaType = "application/json"
protected const val FormDataMediaType = "multipart/form-data"
protected const val XmlMediaType = "application/xml"

@JvmStatic
val client by lazy {
Expand All @@ -26,40 +26,37 @@ open class ApiClient(val baseUrl: String) {
val jsonHeaders: Map<String, String> = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType)
}

inline protected fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody {
if(content is File) {
return RequestBody.create(
MediaType.parse(mediaType), content
)
} else if(mediaType == FormDataMediaType) {
var builder = FormBody.Builder()
// content's type *must* be Map<String, Any>
@Suppress("UNCHECKED_CAST")
(content as Map<String,String>).forEach { key, value ->
builder = builder.add(key, value)
}
return builder.build()
} else if(mediaType == JsonMediaType) {
return RequestBody.create(
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
)
} else if (mediaType == XmlMediaType) {
TODO("xml not currently supported.")
}

// TODO: this should be extended with other serializers
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> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody =
when {
content is File -> RequestBody.create(
MediaType.parse(mediaType), content
)
mediaType == FormDataMediaType -> {
var builder = FormBody.Builder()
// content's type *must* be Map<String, Any>
@Suppress("UNCHECKED_CAST")
(content as Map<String,String>).forEach { key, value ->
builder = builder.add(key, value)
}
builder.build()
}
mediaType == JsonMediaType -> RequestBody.create(
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
)
mediaType == XmlMediaType -> TODO("xml not currently supported.")
// TODO: this should be extended with other serializers
else -> TODO("requestBody currently only supports JSON body and File body.")
}

protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
if(body == null) return null
return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source())
else -> TODO()
}
}

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.")

var urlBuilder = httpUrl.newBuilder()
Expand Down
Loading

0 comments on commit c1eda61

Please sign in to comment.