@goast/kotlin 0.5.1-beta.1 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/LICENSE +21 -21
  2. package/assets/client/okhttp3/ApiAbstractions.kt +30 -30
  3. package/assets/client/okhttp3/ApiClient.kt +253 -253
  4. package/assets/client/okhttp3/ApiResponse.kt +43 -43
  5. package/assets/client/okhttp3/Errors.kt +21 -21
  6. package/assets/client/okhttp3/PartConfig.kt +11 -11
  7. package/assets/client/okhttp3/RequestConfig.kt +18 -18
  8. package/assets/client/okhttp3/RequestMethod.kt +8 -8
  9. package/assets/client/okhttp3/ResponseExtensions.kt +24 -24
  10. package/assets/client/spring-reactive-web-clients/ApiRequestFile.kt +33 -33
  11. package/esm/src/generators/models/model-generator.d.ts.map +1 -1
  12. package/esm/src/generators/models/model-generator.js +1 -0
  13. package/package.json +2 -2
  14. package/script/src/generators/models/model-generator.d.ts.map +1 -1
  15. package/script/src/generators/models/model-generator.js +1 -0
  16. package/src/mod.ts +8 -0
  17. package/src/src/assets.ts +9 -0
  18. package/src/src/ast/_index.ts +66 -0
  19. package/src/src/ast/common.ts +1 -0
  20. package/src/src/ast/index.ts +1 -0
  21. package/src/src/ast/node.ts +10 -0
  22. package/src/src/ast/nodes/annotation.ts +79 -0
  23. package/src/src/ast/nodes/argument.ts +62 -0
  24. package/src/src/ast/nodes/call.ts +75 -0
  25. package/src/src/ast/nodes/class.ts +178 -0
  26. package/src/src/ast/nodes/collection-literal.ts +49 -0
  27. package/src/src/ast/nodes/constructor.ts +126 -0
  28. package/src/src/ast/nodes/doc-tag.ts +138 -0
  29. package/src/src/ast/nodes/doc.ts +111 -0
  30. package/src/src/ast/nodes/enum-value.ts +100 -0
  31. package/src/src/ast/nodes/enum.ts +163 -0
  32. package/src/src/ast/nodes/function.ts +178 -0
  33. package/src/src/ast/nodes/generic-parameter.ts +54 -0
  34. package/src/src/ast/nodes/init-block.ts +38 -0
  35. package/src/src/ast/nodes/interface.ts +133 -0
  36. package/src/src/ast/nodes/lambda-type.ts +73 -0
  37. package/src/src/ast/nodes/lambda.ts +74 -0
  38. package/src/src/ast/nodes/object.ts +102 -0
  39. package/src/src/ast/nodes/parameter.ts +118 -0
  40. package/src/src/ast/nodes/property.ts +225 -0
  41. package/src/src/ast/nodes/reference.ts +178 -0
  42. package/src/src/ast/nodes/string.ts +114 -0
  43. package/src/src/ast/nodes/types.ts +23 -0
  44. package/src/src/ast/references/index.ts +10 -0
  45. package/src/src/ast/references/jackson.ts +44 -0
  46. package/src/src/ast/references/jakarta.ts +14 -0
  47. package/src/src/ast/references/java.ts +20 -0
  48. package/src/src/ast/references/kotlin.ts +41 -0
  49. package/src/src/ast/references/kotlinx.ts +14 -0
  50. package/src/src/ast/references/okhttp3.ts +5 -0
  51. package/src/src/ast/references/reactor.ts +5 -0
  52. package/src/src/ast/references/spring-reactive.ts +33 -0
  53. package/src/src/ast/references/spring.ts +86 -0
  54. package/src/src/ast/references/swagger.ts +23 -0
  55. package/src/src/ast/utils/get-kotlin-builder-options.ts +19 -0
  56. package/src/src/ast/utils/to-kt-node.ts +31 -0
  57. package/src/src/ast/utils/write-kt-annotations.ts +15 -0
  58. package/src/src/ast/utils/write-kt-arguments.ts +45 -0
  59. package/src/src/ast/utils/write-kt-enum-values.ts +27 -0
  60. package/src/src/ast/utils/write-kt-generic-parameters.ts +12 -0
  61. package/src/src/ast/utils/write-kt-members.ts +25 -0
  62. package/src/src/ast/utils/write-kt-node.ts +37 -0
  63. package/src/src/ast/utils/write-kt-parameters.ts +25 -0
  64. package/src/src/common-results.ts +4 -0
  65. package/src/src/config.ts +41 -0
  66. package/src/src/file-builder.ts +112 -0
  67. package/src/src/generators/file-generator.ts +29 -0
  68. package/src/src/generators/index.ts +5 -0
  69. package/src/src/generators/models/args.ts +132 -0
  70. package/src/src/generators/models/index.ts +4 -0
  71. package/src/src/generators/models/model-generator.ts +703 -0
  72. package/src/src/generators/models/models-generator.ts +65 -0
  73. package/src/src/generators/models/models.ts +95 -0
  74. package/src/src/generators/services/okhttp3-clients/args.ts +88 -0
  75. package/src/src/generators/services/okhttp3-clients/index.ts +4 -0
  76. package/src/src/generators/services/okhttp3-clients/models.ts +73 -0
  77. package/src/src/generators/services/okhttp3-clients/okhttp3-client-generator.ts +597 -0
  78. package/src/src/generators/services/okhttp3-clients/okhttp3-clients-generator.ts +169 -0
  79. package/src/src/generators/services/okhttp3-clients/refs.ts +59 -0
  80. package/src/src/generators/services/spring-controllers/args.ts +93 -0
  81. package/src/src/generators/services/spring-controllers/index.ts +4 -0
  82. package/src/src/generators/services/spring-controllers/models.ts +76 -0
  83. package/src/src/generators/services/spring-controllers/refs.ts +17 -0
  84. package/src/src/generators/services/spring-controllers/spring-controller-generator.ts +1084 -0
  85. package/src/src/generators/services/spring-controllers/spring-controllers-generator.ts +140 -0
  86. package/src/src/generators/services/spring-reactive-web-clients/args.ts +101 -0
  87. package/src/src/generators/services/spring-reactive-web-clients/index.ts +4 -0
  88. package/src/src/generators/services/spring-reactive-web-clients/models.ts +62 -0
  89. package/src/src/generators/services/spring-reactive-web-clients/refs.ts +11 -0
  90. package/src/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.ts +571 -0
  91. package/src/src/generators/services/spring-reactive-web-clients/spring-reactive-web-clients-generator.ts +125 -0
  92. package/src/src/import-collection.ts +98 -0
  93. package/src/src/types.ts +3 -0
  94. package/src/src/utils.ts +39 -0
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2021 Marc Schmidt
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Marc Schmidt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,30 +1,30 @@
1
- package @PACKAGE_NAME@
2
-
3
- typealias MultiValueMap = MutableMap<String, List<String>>
4
-
5
- fun collectionDelimiter(collectionFormat: String) = when (collectionFormat) {
6
- "csv" -> ","
7
- "tsv" -> "\t"
8
- "pipe" -> "|"
9
- "space" -> " "
10
- else -> ""
11
- }
12
-
13
- val defaultMultiValueConverter: (item: Any?) -> String = { item -> "$item" }
14
-
15
- fun <T : Any?> toMultiValue(
16
- items: Array<T>,
17
- collectionFormat: String,
18
- map: (item: T) -> String = defaultMultiValueConverter
19
- ) = toMultiValue(items.asIterable(), collectionFormat, map)
20
-
21
- fun <T : Any?> toMultiValue(
22
- items: Iterable<T>,
23
- collectionFormat: String,
24
- map: (item: T) -> String = defaultMultiValueConverter
25
- ): List<String> {
26
- return when (collectionFormat) {
27
- "multi" -> items.map(map)
28
- else -> listOf(items.joinToString(separator = collectionDelimiter(collectionFormat), transform = map))
29
- }
30
- }
1
+ package @PACKAGE_NAME@
2
+
3
+ typealias MultiValueMap = MutableMap<String, List<String>>
4
+
5
+ fun collectionDelimiter(collectionFormat: String) = when (collectionFormat) {
6
+ "csv" -> ","
7
+ "tsv" -> "\t"
8
+ "pipe" -> "|"
9
+ "space" -> " "
10
+ else -> ""
11
+ }
12
+
13
+ val defaultMultiValueConverter: (item: Any?) -> String = { item -> "$item" }
14
+
15
+ fun <T : Any?> toMultiValue(
16
+ items: Array<T>,
17
+ collectionFormat: String,
18
+ map: (item: T) -> String = defaultMultiValueConverter
19
+ ) = toMultiValue(items.asIterable(), collectionFormat, map)
20
+
21
+ fun <T : Any?> toMultiValue(
22
+ items: Iterable<T>,
23
+ collectionFormat: String,
24
+ map: (item: T) -> String = defaultMultiValueConverter
25
+ ): List<String> {
26
+ return when (collectionFormat) {
27
+ "multi" -> items.map(map)
28
+ else -> listOf(items.joinToString(separator = collectionDelimiter(collectionFormat), transform = map))
29
+ }
30
+ }
@@ -1,253 +1,253 @@
1
- package @PACKAGE_NAME@
2
-
3
- import com.fasterxml.jackson.core.type.TypeReference
4
- import com.fasterxml.jackson.databind.ObjectMapper
5
- import okhttp3.FormBody
6
- import okhttp3.Headers.Companion.toHeaders
7
- import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
8
- import okhttp3.MediaType.Companion.toMediaTypeOrNull
9
- import okhttp3.MultipartBody
10
- import okhttp3.Call.Factory
11
- import okhttp3.OkHttpClient
12
- import okhttp3.Request
13
- import okhttp3.RequestBody
14
- import okhttp3.RequestBody.Companion.asRequestBody
15
- import okhttp3.RequestBody.Companion.toRequestBody
16
- import okhttp3.ResponseBody
17
- import okhttp3.internal.EMPTY_REQUEST
18
- import java.io.File
19
- import java.net.URLConnection
20
- import java.time.LocalDate
21
- import java.time.LocalDateTime
22
- import java.time.LocalTime
23
- import java.time.OffsetDateTime
24
- import java.time.OffsetTime
25
- import java.util.Locale
26
-
27
- open class ApiClient(@API_CLIENT_PARAMETERS@) {
28
- companion object {
29
- protected const val ContentType = "Content-Type"
30
- protected const val Accept = "Accept"
31
- protected const val Authorization = "Authorization"
32
- protected const val JsonMediaType = "application/json"
33
- protected const val FormDataMediaType = "multipart/form-data"
34
- protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
35
- protected const val XmlMediaType = "application/xml"
36
-
37
- val apiKey: MutableMap<String, String> = mutableMapOf()
38
- val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
39
- var username: String? = null
40
- var password: String? = null
41
- var accessToken: String? = null
42
- const val baseUrlKey = "org.openapitools.client.baseUrl"
43
-
44
- @JvmStatic
45
- val defaultClient: OkHttpClient by lazy {
46
- builder.build()
47
- }
48
-
49
- @JvmStatic
50
- val builder: OkHttpClient.Builder = OkHttpClient.Builder()
51
- }
52
-
53
- /**
54
- * Guess Content-Type header from the given file (defaults to "application/octet-stream").
55
- *
56
- * @param file The given file
57
- * @return The guessed Content-Type
58
- */
59
- protected fun guessContentTypeFromFile(file: File): String {
60
- val contentType = URLConnection.guessContentTypeFromName(file.name)
61
- return contentType ?: "application/octet-stream"
62
- }
63
-
64
- protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
65
- when {
66
- content is File -> content.asRequestBody(
67
- (mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()
68
- )
69
-
70
- mediaType == FormDataMediaType ->
71
- MultipartBody.Builder()
72
- .setType(MultipartBody.FORM)
73
- .apply {
74
- // content's type *must* be Map<String, PartConfig<*>>
75
- @Suppress("UNCHECKED_CAST")
76
- (content as Map<String, PartConfig<*>>).forEach { (name, part) ->
77
- if (part.body is File) {
78
- val partHeaders = part.headers.toMutableMap() +
79
- ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
80
- val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
81
- addPart(
82
- partHeaders.toHeaders(),
83
- part.body.asRequestBody(fileMediaType)
84
- )
85
- } else {
86
- val partHeaders = part.headers.toMutableMap() +
87
- ("Content-Disposition" to "form-data; name=\"$name\"")
88
- addPart(
89
- partHeaders.toHeaders(),
90
- objectMapper.writeValueAsString(part.body)
91
- .toRequestBody(JsonMediaType.toMediaTypeOrNull())
92
- )
93
- }
94
- }
95
- }.build()
96
-
97
- mediaType == FormUrlEncMediaType -> {
98
- FormBody.Builder().apply {
99
- // content's type *must* be Map<String, PartConfig<*>>
100
- @Suppress("UNCHECKED_CAST")
101
- (content as Map<String, PartConfig<*>>).forEach { (name, part) ->
102
- add(name, parameterToString(part.body))
103
- }
104
- }.build()
105
- }
106
-
107
- mediaType == null || mediaType.startsWith("application/") && mediaType.endsWith("json") ->
108
- if (content == null) {
109
- EMPTY_REQUEST
110
- } else {
111
- objectMapper.writeValueAsString(content)
112
- .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
113
- }
114
-
115
- mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
116
- // TODO: this should be extended with other serializers
117
- else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
118
- }
119
-
120
- protected inline fun <reified T : Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
121
- if (body == null) {
122
- return null
123
- }
124
- if (T::class.java == File::class.java) {
125
- // return tempFile
126
- // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options
127
- val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
128
- tempFile.deleteOnExit()
129
- body.byteStream().use { inputStream ->
130
- tempFile.outputStream().use { tempFileOutputStream ->
131
- inputStream.copyTo(tempFileOutputStream)
132
- }
133
- }
134
- return tempFile as T
135
- }
136
- val bodyContent = body.string()
137
- if (bodyContent.isEmpty()) {
138
- return null
139
- }
140
- return when {
141
- mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
142
- objectMapper.readValue(bodyContent, object : TypeReference<T>() {})
143
-
144
- mediaType.startsWith("text/") && T::class.java == String::class.java -> bodyContent as T
145
-
146
- else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
147
- }
148
- }
149
-
150
- protected inline fun <reified I, reified T : Any?> request(requestConfig: RequestConfig<I>): ApiResponse<T?> {
151
- val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.")
152
-
153
- val url = httpUrl.newBuilder()
154
- .addEncodedPathSegments(requestConfig.path.trimStart('/'))
155
- .apply {
156
- requestConfig.query.forEach { query ->
157
- query.value.forEach { queryValue ->
158
- addQueryParameter(query.key, queryValue)
159
- }
160
- }
161
- }.build()
162
-
163
- // take content-type/accept from spec or set to default (application/json) if not defined
164
- if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
165
- requestConfig.headers[ContentType] = JsonMediaType
166
- }
167
- if (requestConfig.headers[Accept].isNullOrEmpty()) {
168
- requestConfig.headers[Accept] = JsonMediaType
169
- }
170
- val headers = requestConfig.headers
171
-
172
- if (headers[Accept].isNullOrEmpty()) {
173
- throw kotlin.IllegalStateException("Missing Accept header. This is required.")
174
- }
175
-
176
- val contentType = if (headers[ContentType] != null) {
177
- // TODO: support multiple contentType options here.
178
- (headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
179
- } else {
180
- null
181
- }
182
-
183
- val request = when (requestConfig.method) {
184
- RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))
185
- RequestMethod.GET -> Request.Builder().url(url)
186
- RequestMethod.HEAD -> Request.Builder().url(url).head()
187
- RequestMethod.PATCH -> Request.Builder().url(url).patch(requestBody(requestConfig.body, contentType))
188
- RequestMethod.PUT -> Request.Builder().url(url).put(requestBody(requestConfig.body, contentType))
189
- RequestMethod.POST -> Request.Builder().url(url).post(requestBody(requestConfig.body, contentType))
190
- RequestMethod.OPTIONS -> Request.Builder().url(url).method("OPTIONS", null)
191
- }.apply {
192
- headers.forEach { header -> addHeader(header.key, header.value) }
193
- }.build()
194
-
195
- val response = client.newCall(request).execute()
196
-
197
- val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault())
198
-
199
- // TODO: handle specific mapping types. e.g. Map<int, Class<?>>
200
- return when {
201
- response.isRedirect -> Redirection(
202
- response.code,
203
- response.headers.toMultimap()
204
- )
205
-
206
- response.isInformational -> Informational(
207
- response.message,
208
- response.code,
209
- response.headers.toMultimap()
210
- )
211
-
212
- response.isSuccessful -> Success(
213
- responseBody(response.body, accept),
214
- response.code,
215
- response.headers.toMultimap()
216
- )
217
-
218
- response.isClientError -> ClientError(
219
- response.message,
220
- response.body?.string(),
221
- response.code,
222
- response.headers.toMultimap()
223
- )
224
-
225
- else -> ServerError(
226
- response.message,
227
- response.body?.string(),
228
- response.code,
229
- response.headers.toMultimap()
230
- )
231
- }
232
- }
233
-
234
- protected fun parameterToString(value: Any?): String = when (value) {
235
- null -> ""
236
- is Array<*> -> toMultiValue(value, "csv").toString()
237
- is Iterable<*> -> toMultiValue(value, "csv").toString()
238
- is OffsetDateTime, is OffsetTime, is LocalDateTime, is LocalDate, is LocalTime ->
239
- parseDateToQueryString(value)
240
-
241
- else -> value.toString()
242
- }
243
-
244
- protected inline fun <reified T : Any> parseDateToQueryString(value: T): String {
245
- /*
246
- .replace("\"", "") converts the json object string to an actual string for the query parameter.
247
- The moshi or gson adapter allows a more generic solution instead of trying to use a native
248
- formatter. It also easily allows to provide a simple way to define a custom date format pattern
249
- inside a gson/moshi adapter.
250
- */
251
- return objectMapper.writeValueAsString(value).replace("\"", "")
252
- }
253
- }
1
+ package @PACKAGE_NAME@
2
+
3
+ import com.fasterxml.jackson.core.type.TypeReference
4
+ import com.fasterxml.jackson.databind.ObjectMapper
5
+ import okhttp3.FormBody
6
+ import okhttp3.Headers.Companion.toHeaders
7
+ import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
8
+ import okhttp3.MediaType.Companion.toMediaTypeOrNull
9
+ import okhttp3.MultipartBody
10
+ import okhttp3.Call.Factory
11
+ import okhttp3.OkHttpClient
12
+ import okhttp3.Request
13
+ import okhttp3.RequestBody
14
+ import okhttp3.RequestBody.Companion.asRequestBody
15
+ import okhttp3.RequestBody.Companion.toRequestBody
16
+ import okhttp3.ResponseBody
17
+ import okhttp3.internal.EMPTY_REQUEST
18
+ import java.io.File
19
+ import java.net.URLConnection
20
+ import java.time.LocalDate
21
+ import java.time.LocalDateTime
22
+ import java.time.LocalTime
23
+ import java.time.OffsetDateTime
24
+ import java.time.OffsetTime
25
+ import java.util.Locale
26
+
27
+ open class ApiClient(@API_CLIENT_PARAMETERS@) {
28
+ companion object {
29
+ protected const val ContentType = "Content-Type"
30
+ protected const val Accept = "Accept"
31
+ protected const val Authorization = "Authorization"
32
+ protected const val JsonMediaType = "application/json"
33
+ protected const val FormDataMediaType = "multipart/form-data"
34
+ protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
35
+ protected const val XmlMediaType = "application/xml"
36
+
37
+ val apiKey: MutableMap<String, String> = mutableMapOf()
38
+ val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
39
+ var username: String? = null
40
+ var password: String? = null
41
+ var accessToken: String? = null
42
+ const val baseUrlKey = "org.openapitools.client.baseUrl"
43
+
44
+ @JvmStatic
45
+ val defaultClient: OkHttpClient by lazy {
46
+ builder.build()
47
+ }
48
+
49
+ @JvmStatic
50
+ val builder: OkHttpClient.Builder = OkHttpClient.Builder()
51
+ }
52
+
53
+ /**
54
+ * Guess Content-Type header from the given file (defaults to "application/octet-stream").
55
+ *
56
+ * @param file The given file
57
+ * @return The guessed Content-Type
58
+ */
59
+ protected fun guessContentTypeFromFile(file: File): String {
60
+ val contentType = URLConnection.guessContentTypeFromName(file.name)
61
+ return contentType ?: "application/octet-stream"
62
+ }
63
+
64
+ protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
65
+ when {
66
+ content is File -> content.asRequestBody(
67
+ (mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()
68
+ )
69
+
70
+ mediaType == FormDataMediaType ->
71
+ MultipartBody.Builder()
72
+ .setType(MultipartBody.FORM)
73
+ .apply {
74
+ // content's type *must* be Map<String, PartConfig<*>>
75
+ @Suppress("UNCHECKED_CAST")
76
+ (content as Map<String, PartConfig<*>>).forEach { (name, part) ->
77
+ if (part.body is File) {
78
+ val partHeaders = part.headers.toMutableMap() +
79
+ ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
80
+ val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
81
+ addPart(
82
+ partHeaders.toHeaders(),
83
+ part.body.asRequestBody(fileMediaType)
84
+ )
85
+ } else {
86
+ val partHeaders = part.headers.toMutableMap() +
87
+ ("Content-Disposition" to "form-data; name=\"$name\"")
88
+ addPart(
89
+ partHeaders.toHeaders(),
90
+ objectMapper.writeValueAsString(part.body)
91
+ .toRequestBody(JsonMediaType.toMediaTypeOrNull())
92
+ )
93
+ }
94
+ }
95
+ }.build()
96
+
97
+ mediaType == FormUrlEncMediaType -> {
98
+ FormBody.Builder().apply {
99
+ // content's type *must* be Map<String, PartConfig<*>>
100
+ @Suppress("UNCHECKED_CAST")
101
+ (content as Map<String, PartConfig<*>>).forEach { (name, part) ->
102
+ add(name, parameterToString(part.body))
103
+ }
104
+ }.build()
105
+ }
106
+
107
+ mediaType == null || mediaType.startsWith("application/") && mediaType.endsWith("json") ->
108
+ if (content == null) {
109
+ EMPTY_REQUEST
110
+ } else {
111
+ objectMapper.writeValueAsString(content)
112
+ .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
113
+ }
114
+
115
+ mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
116
+ // TODO: this should be extended with other serializers
117
+ else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
118
+ }
119
+
120
+ protected inline fun <reified T : Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
121
+ if (body == null) {
122
+ return null
123
+ }
124
+ if (T::class.java == File::class.java) {
125
+ // return tempFile
126
+ // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options
127
+ val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
128
+ tempFile.deleteOnExit()
129
+ body.byteStream().use { inputStream ->
130
+ tempFile.outputStream().use { tempFileOutputStream ->
131
+ inputStream.copyTo(tempFileOutputStream)
132
+ }
133
+ }
134
+ return tempFile as T
135
+ }
136
+ val bodyContent = body.string()
137
+ if (bodyContent.isEmpty()) {
138
+ return null
139
+ }
140
+ return when {
141
+ mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
142
+ objectMapper.readValue(bodyContent, object : TypeReference<T>() {})
143
+
144
+ mediaType.startsWith("text/") && T::class.java == String::class.java -> bodyContent as T
145
+
146
+ else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
147
+ }
148
+ }
149
+
150
+ protected inline fun <reified I, reified T : Any?> request(requestConfig: RequestConfig<I>): ApiResponse<T?> {
151
+ val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.")
152
+
153
+ val url = httpUrl.newBuilder()
154
+ .addEncodedPathSegments(requestConfig.path.trimStart('/'))
155
+ .apply {
156
+ requestConfig.query.forEach { query ->
157
+ query.value.forEach { queryValue ->
158
+ addQueryParameter(query.key, queryValue)
159
+ }
160
+ }
161
+ }.build()
162
+
163
+ // take content-type/accept from spec or set to default (application/json) if not defined
164
+ if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
165
+ requestConfig.headers[ContentType] = JsonMediaType
166
+ }
167
+ if (requestConfig.headers[Accept].isNullOrEmpty()) {
168
+ requestConfig.headers[Accept] = JsonMediaType
169
+ }
170
+ val headers = requestConfig.headers
171
+
172
+ if (headers[Accept].isNullOrEmpty()) {
173
+ throw kotlin.IllegalStateException("Missing Accept header. This is required.")
174
+ }
175
+
176
+ val contentType = if (headers[ContentType] != null) {
177
+ // TODO: support multiple contentType options here.
178
+ (headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
179
+ } else {
180
+ null
181
+ }
182
+
183
+ val request = when (requestConfig.method) {
184
+ RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))
185
+ RequestMethod.GET -> Request.Builder().url(url)
186
+ RequestMethod.HEAD -> Request.Builder().url(url).head()
187
+ RequestMethod.PATCH -> Request.Builder().url(url).patch(requestBody(requestConfig.body, contentType))
188
+ RequestMethod.PUT -> Request.Builder().url(url).put(requestBody(requestConfig.body, contentType))
189
+ RequestMethod.POST -> Request.Builder().url(url).post(requestBody(requestConfig.body, contentType))
190
+ RequestMethod.OPTIONS -> Request.Builder().url(url).method("OPTIONS", null)
191
+ }.apply {
192
+ headers.forEach { header -> addHeader(header.key, header.value) }
193
+ }.build()
194
+
195
+ val response = client.newCall(request).execute()
196
+
197
+ val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault())
198
+
199
+ // TODO: handle specific mapping types. e.g. Map<int, Class<?>>
200
+ return when {
201
+ response.isRedirect -> Redirection(
202
+ response.code,
203
+ response.headers.toMultimap()
204
+ )
205
+
206
+ response.isInformational -> Informational(
207
+ response.message,
208
+ response.code,
209
+ response.headers.toMultimap()
210
+ )
211
+
212
+ response.isSuccessful -> Success(
213
+ responseBody(response.body, accept),
214
+ response.code,
215
+ response.headers.toMultimap()
216
+ )
217
+
218
+ response.isClientError -> ClientError(
219
+ response.message,
220
+ response.body?.string(),
221
+ response.code,
222
+ response.headers.toMultimap()
223
+ )
224
+
225
+ else -> ServerError(
226
+ response.message,
227
+ response.body?.string(),
228
+ response.code,
229
+ response.headers.toMultimap()
230
+ )
231
+ }
232
+ }
233
+
234
+ protected fun parameterToString(value: Any?): String = when (value) {
235
+ null -> ""
236
+ is Array<*> -> toMultiValue(value, "csv").toString()
237
+ is Iterable<*> -> toMultiValue(value, "csv").toString()
238
+ is OffsetDateTime, is OffsetTime, is LocalDateTime, is LocalDate, is LocalTime ->
239
+ parseDateToQueryString(value)
240
+
241
+ else -> value.toString()
242
+ }
243
+
244
+ protected inline fun <reified T : Any> parseDateToQueryString(value: T): String {
245
+ /*
246
+ .replace("\"", "") converts the json object string to an actual string for the query parameter.
247
+ The moshi or gson adapter allows a more generic solution instead of trying to use a native
248
+ formatter. It also easily allows to provide a simple way to define a custom date format pattern
249
+ inside a gson/moshi adapter.
250
+ */
251
+ return objectMapper.writeValueAsString(value).replace("\"", "")
252
+ }
253
+ }