@@ -5,14 +5,14 @@ import java.io.File
5
5
6
6
open class ApiClient(val baseUrl: String) {
7
7
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"
13
13
14
14
@JvmStatic
15
- val client : OkHttpClient = OkHttpClient()
15
+ val client: OkHttpClient = OkHttpClient()
16
16
17
17
@JvmStatic
18
18
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) {
21
21
val jsonHeaders: Map< String, String> = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType)
22
22
}
23
23
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.")
35
44
}
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
- }
48
45
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) {
52
49
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source())
53
50
else -> TODO()
54
51
}
55
52
}
56
53
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?> {
58
55
val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.")
59
56
60
57
var urlBuilder = httpUrl.newBuilder()
@@ -69,19 +66,19 @@ open class ApiClient(val baseUrl: String) {
69
66
val url = urlBuilder.build()
70
67
val headers = requestConfig.headers + defaultHeaders
71
68
72
- if(headers[ContentType] ?: "" == "") {
69
+ if (headers[ContentType] ?: "" == "") {
73
70
throw kotlin.IllegalStateException("Missing Content-Type header. This is required.")
74
71
}
75
72
76
- if(headers[Accept] ?: "" == "") {
73
+ if (headers[Accept] ?: "" == "") {
77
74
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
78
75
}
79
76
80
77
// TODO: support multiple contentType,accept options here.
81
78
val contentType = (headers[ContentType] as String).substringBefore(";").toLowerCase()
82
79
val accept = (headers[Accept] as String).substringBefore(";").toLowerCase()
83
80
84
- var request : Request.Builder = when (requestConfig.method) {
81
+ var request: Request.Builder = when (requestConfig.method) {
85
82
RequestMethod.DELETE -> Request.Builder().url(url).delete()
86
83
RequestMethod.GET -> Request.Builder().url(url)
87
84
RequestMethod.HEAD -> Request.Builder().url(url).head()
0 commit comments