@goast/kotlin 0.5.13 → 0.6.0-beta.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.
- package/LICENSE +21 -21
- package/README.md +45 -45
- package/assets/client/okhttp3/ApiAbstractions.kt +30 -30
- package/assets/client/okhttp3/ApiClient.kt +252 -252
- package/assets/client/okhttp3/ApiResponse.kt +43 -43
- package/assets/client/okhttp3/Errors.kt +21 -21
- package/assets/client/okhttp3/PartConfig.kt +11 -11
- package/assets/client/okhttp3/RequestConfig.kt +18 -18
- package/assets/client/okhttp3/RequestMethod.kt +8 -8
- package/assets/client/okhttp3/ResponseExtensions.kt +24 -24
- package/assets/client/spring-reactive-web-clients/ApiRequestFile.kt +33 -33
- package/esm/src/generators/models/model-generator.d.ts.map +1 -1
- package/esm/src/generators/models/model-generator.js +5 -14
- package/esm/src/generators/services/okhttp3-clients/okhttp3-client-generator.d.ts.map +1 -1
- package/esm/src/generators/services/okhttp3-clients/okhttp3-client-generator.js +40 -47
- package/esm/src/generators/services/okhttp3-clients/okhttp3-clients-generator.js +9 -9
- package/esm/src/generators/services/spring-controllers/spring-controller-generator.d.ts.map +1 -1
- package/esm/src/generators/services/spring-controllers/spring-controller-generator.js +31 -39
- package/esm/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.d.ts.map +1 -1
- package/esm/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.js +16 -25
- package/esm/src/utils.d.ts +1 -3
- package/esm/src/utils.d.ts.map +1 -1
- package/esm/src/utils.js +0 -7
- package/package.json +2 -2
- package/script/src/generators/models/model-generator.d.ts.map +1 -1
- package/script/src/generators/models/model-generator.js +4 -13
- package/script/src/generators/services/okhttp3-clients/okhttp3-client-generator.d.ts.map +1 -1
- package/script/src/generators/services/okhttp3-clients/okhttp3-client-generator.js +39 -46
- package/script/src/generators/services/okhttp3-clients/okhttp3-clients-generator.js +9 -9
- package/script/src/generators/services/spring-controllers/spring-controller-generator.d.ts.map +1 -1
- package/script/src/generators/services/spring-controllers/spring-controller-generator.js +30 -38
- package/script/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.d.ts.map +1 -1
- package/script/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.js +15 -24
- package/script/src/utils.d.ts +1 -3
- package/script/src/utils.d.ts.map +1 -1
- package/script/src/utils.js +0 -9
- package/src/mod.ts +0 -8
- package/src/src/assets.ts +0 -9
- package/src/src/ast/_index.ts +0 -66
- package/src/src/ast/common.ts +0 -1
- package/src/src/ast/index.ts +0 -1
- package/src/src/ast/node.ts +0 -10
- package/src/src/ast/nodes/annotation.ts +0 -79
- package/src/src/ast/nodes/argument.ts +0 -62
- package/src/src/ast/nodes/call.ts +0 -75
- package/src/src/ast/nodes/class.ts +0 -178
- package/src/src/ast/nodes/collection-literal.ts +0 -49
- package/src/src/ast/nodes/constructor.ts +0 -126
- package/src/src/ast/nodes/doc-tag.ts +0 -138
- package/src/src/ast/nodes/doc.ts +0 -111
- package/src/src/ast/nodes/enum-value.ts +0 -100
- package/src/src/ast/nodes/enum.ts +0 -163
- package/src/src/ast/nodes/function.ts +0 -178
- package/src/src/ast/nodes/generic-parameter.ts +0 -54
- package/src/src/ast/nodes/init-block.ts +0 -38
- package/src/src/ast/nodes/interface.ts +0 -133
- package/src/src/ast/nodes/lambda-type.ts +0 -73
- package/src/src/ast/nodes/lambda.ts +0 -74
- package/src/src/ast/nodes/object.ts +0 -102
- package/src/src/ast/nodes/parameter.ts +0 -118
- package/src/src/ast/nodes/property.ts +0 -225
- package/src/src/ast/nodes/reference.ts +0 -178
- package/src/src/ast/nodes/string.ts +0 -114
- package/src/src/ast/nodes/types.ts +0 -23
- package/src/src/ast/references/index.ts +0 -10
- package/src/src/ast/references/jackson.ts +0 -59
- package/src/src/ast/references/jakarta.ts +0 -14
- package/src/src/ast/references/java.ts +0 -27
- package/src/src/ast/references/kotlin.ts +0 -41
- package/src/src/ast/references/kotlinx.ts +0 -14
- package/src/src/ast/references/okhttp3.ts +0 -5
- package/src/src/ast/references/reactor.ts +0 -5
- package/src/src/ast/references/spring-reactive.ts +0 -37
- package/src/src/ast/references/spring.ts +0 -86
- package/src/src/ast/references/swagger.ts +0 -23
- package/src/src/ast/utils/get-kotlin-builder-options.ts +0 -19
- package/src/src/ast/utils/to-kt-node.ts +0 -31
- package/src/src/ast/utils/write-kt-annotations.ts +0 -15
- package/src/src/ast/utils/write-kt-arguments.ts +0 -45
- package/src/src/ast/utils/write-kt-enum-values.ts +0 -27
- package/src/src/ast/utils/write-kt-generic-parameters.ts +0 -12
- package/src/src/ast/utils/write-kt-members.ts +0 -25
- package/src/src/ast/utils/write-kt-node.ts +0 -37
- package/src/src/ast/utils/write-kt-parameters.ts +0 -25
- package/src/src/common-results.ts +0 -4
- package/src/src/config.ts +0 -61
- package/src/src/file-builder.ts +0 -112
- package/src/src/generators/file-generator.ts +0 -29
- package/src/src/generators/index.ts +0 -5
- package/src/src/generators/models/args.ts +0 -136
- package/src/src/generators/models/index.ts +0 -4
- package/src/src/generators/models/model-generator.ts +0 -736
- package/src/src/generators/models/models-generator.ts +0 -65
- package/src/src/generators/models/models.ts +0 -95
- package/src/src/generators/services/okhttp3-clients/args.ts +0 -90
- package/src/src/generators/services/okhttp3-clients/index.ts +0 -4
- package/src/src/generators/services/okhttp3-clients/models.ts +0 -73
- package/src/src/generators/services/okhttp3-clients/okhttp3-client-generator.ts +0 -627
- package/src/src/generators/services/okhttp3-clients/okhttp3-clients-generator.ts +0 -188
- package/src/src/generators/services/okhttp3-clients/refs.ts +0 -59
- package/src/src/generators/services/spring-controllers/args.ts +0 -95
- package/src/src/generators/services/spring-controllers/index.ts +0 -4
- package/src/src/generators/services/spring-controllers/models.ts +0 -76
- package/src/src/generators/services/spring-controllers/refs.ts +0 -17
- package/src/src/generators/services/spring-controllers/spring-controller-generator.ts +0 -1115
- package/src/src/generators/services/spring-controllers/spring-controllers-generator.ts +0 -140
- package/src/src/generators/services/spring-reactive-web-clients/args.ts +0 -112
- package/src/src/generators/services/spring-reactive-web-clients/index.ts +0 -4
- package/src/src/generators/services/spring-reactive-web-clients/models.ts +0 -78
- package/src/src/generators/services/spring-reactive-web-clients/refs.ts +0 -11
- package/src/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.ts +0 -641
- package/src/src/generators/services/spring-reactive-web-clients/spring-reactive-web-clients-generator.ts +0 -125
- package/src/src/import-collection.ts +0 -98
- package/src/src/types.ts +0 -3
- package/src/src/utils.ts +0 -55
|
@@ -1,252 +1,252 @@
|
|
|
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 java.io.File
|
|
18
|
-
import java.net.URLConnection
|
|
19
|
-
import java.time.LocalDate
|
|
20
|
-
import java.time.LocalDateTime
|
|
21
|
-
import java.time.LocalTime
|
|
22
|
-
import java.time.OffsetDateTime
|
|
23
|
-
import java.time.OffsetTime
|
|
24
|
-
import java.util.Locale
|
|
25
|
-
|
|
26
|
-
open class ApiClient(@API_CLIENT_PARAMETERS@) {
|
|
27
|
-
companion object {
|
|
28
|
-
protected const val ContentType = "Content-Type"
|
|
29
|
-
protected const val Accept = "Accept"
|
|
30
|
-
protected const val Authorization = "Authorization"
|
|
31
|
-
protected const val JsonMediaType = "application/json"
|
|
32
|
-
protected const val FormDataMediaType = "multipart/form-data"
|
|
33
|
-
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
|
|
34
|
-
protected const val XmlMediaType = "application/xml"
|
|
35
|
-
|
|
36
|
-
val apiKey: MutableMap<String, String> = mutableMapOf()
|
|
37
|
-
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
|
|
38
|
-
var username: String? = null
|
|
39
|
-
var password: String? = null
|
|
40
|
-
var accessToken: String? = null
|
|
41
|
-
const val baseUrlKey = "org.openapitools.client.baseUrl"
|
|
42
|
-
|
|
43
|
-
@JvmStatic
|
|
44
|
-
val defaultClient: OkHttpClient by lazy {
|
|
45
|
-
builder.build()
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
@JvmStatic
|
|
49
|
-
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
|
|
54
|
-
*
|
|
55
|
-
* @param file The given file
|
|
56
|
-
* @return The guessed Content-Type
|
|
57
|
-
*/
|
|
58
|
-
protected fun guessContentTypeFromFile(file: File): String {
|
|
59
|
-
val contentType = URLConnection.guessContentTypeFromName(file.name)
|
|
60
|
-
return contentType ?: "application/octet-stream"
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
|
64
|
-
when {
|
|
65
|
-
content is File -> content.asRequestBody(
|
|
66
|
-
(mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()
|
|
67
|
-
)
|
|
68
|
-
|
|
69
|
-
mediaType == FormDataMediaType ->
|
|
70
|
-
MultipartBody.Builder()
|
|
71
|
-
.setType(MultipartBody.FORM)
|
|
72
|
-
.apply {
|
|
73
|
-
// content's type *must* be Map<String, PartConfig<*>>
|
|
74
|
-
@Suppress("UNCHECKED_CAST")
|
|
75
|
-
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
|
76
|
-
if (part.body is File) {
|
|
77
|
-
val partHeaders = part.headers.toMutableMap() +
|
|
78
|
-
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
|
79
|
-
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
|
80
|
-
addPart(
|
|
81
|
-
partHeaders.toHeaders(),
|
|
82
|
-
part.body.asRequestBody(fileMediaType)
|
|
83
|
-
)
|
|
84
|
-
} else {
|
|
85
|
-
val partHeaders = part.headers.toMutableMap() +
|
|
86
|
-
("Content-Disposition" to "form-data; name=\"$name\"")
|
|
87
|
-
addPart(
|
|
88
|
-
partHeaders.toHeaders(),
|
|
89
|
-
objectMapper.writeValueAsString(part.body)
|
|
90
|
-
.toRequestBody(JsonMediaType.toMediaTypeOrNull())
|
|
91
|
-
)
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}.build()
|
|
95
|
-
|
|
96
|
-
mediaType == FormUrlEncMediaType -> {
|
|
97
|
-
FormBody.Builder().apply {
|
|
98
|
-
// content's type *must* be Map<String, PartConfig<*>>
|
|
99
|
-
@Suppress("UNCHECKED_CAST")
|
|
100
|
-
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
|
101
|
-
add(name, parameterToString(part.body))
|
|
102
|
-
}
|
|
103
|
-
}.build()
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
mediaType == null || mediaType.startsWith("application/") && mediaType.endsWith("json") ->
|
|
107
|
-
if (content == null) {
|
|
108
|
-
ByteArray(0).toRequestBody(null)
|
|
109
|
-
} else {
|
|
110
|
-
objectMapper.writeValueAsString(content)
|
|
111
|
-
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
|
|
115
|
-
// TODO: this should be extended with other serializers
|
|
116
|
-
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
protected inline fun <reified T : Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
|
|
120
|
-
if (body == null) {
|
|
121
|
-
return null
|
|
122
|
-
}
|
|
123
|
-
if (T::class.java == File::class.java) {
|
|
124
|
-
// return tempFile
|
|
125
|
-
// 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
|
|
126
|
-
val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
|
|
127
|
-
tempFile.deleteOnExit()
|
|
128
|
-
body.byteStream().use { inputStream ->
|
|
129
|
-
tempFile.outputStream().use { tempFileOutputStream ->
|
|
130
|
-
inputStream.copyTo(tempFileOutputStream)
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
return tempFile as T
|
|
134
|
-
}
|
|
135
|
-
val bodyContent = body.string()
|
|
136
|
-
if (bodyContent.isEmpty()) {
|
|
137
|
-
return null
|
|
138
|
-
}
|
|
139
|
-
return when {
|
|
140
|
-
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
|
|
141
|
-
objectMapper.readValue(bodyContent, object : TypeReference<T>() {})
|
|
142
|
-
|
|
143
|
-
mediaType.startsWith("text/") && T::class.java == String::class.java -> bodyContent as T
|
|
144
|
-
|
|
145
|
-
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
protected inline fun <reified I, reified T : Any?> request(requestConfig: RequestConfig<I>): ApiResponse<T?> {
|
|
150
|
-
val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.")
|
|
151
|
-
|
|
152
|
-
val url = httpUrl.newBuilder()
|
|
153
|
-
.addEncodedPathSegments(requestConfig.path.trimStart('/'))
|
|
154
|
-
.apply {
|
|
155
|
-
requestConfig.query.forEach { query ->
|
|
156
|
-
query.value.forEach { queryValue ->
|
|
157
|
-
addQueryParameter(query.key, queryValue)
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}.build()
|
|
161
|
-
|
|
162
|
-
// take content-type/accept from spec or set to default (application/json) if not defined
|
|
163
|
-
if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
|
|
164
|
-
requestConfig.headers[ContentType] = JsonMediaType
|
|
165
|
-
}
|
|
166
|
-
if (requestConfig.headers[Accept].isNullOrEmpty()) {
|
|
167
|
-
requestConfig.headers[Accept] = JsonMediaType
|
|
168
|
-
}
|
|
169
|
-
val headers = requestConfig.headers
|
|
170
|
-
|
|
171
|
-
if (headers[Accept].isNullOrEmpty()) {
|
|
172
|
-
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
val contentType = if (headers[ContentType] != null) {
|
|
176
|
-
// TODO: support multiple contentType options here.
|
|
177
|
-
(headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
|
|
178
|
-
} else {
|
|
179
|
-
null
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
val request = when (requestConfig.method) {
|
|
183
|
-
RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))
|
|
184
|
-
RequestMethod.GET -> Request.Builder().url(url)
|
|
185
|
-
RequestMethod.HEAD -> Request.Builder().url(url).head()
|
|
186
|
-
RequestMethod.PATCH -> Request.Builder().url(url).patch(requestBody(requestConfig.body, contentType))
|
|
187
|
-
RequestMethod.PUT -> Request.Builder().url(url).put(requestBody(requestConfig.body, contentType))
|
|
188
|
-
RequestMethod.POST -> Request.Builder().url(url).post(requestBody(requestConfig.body, contentType))
|
|
189
|
-
RequestMethod.OPTIONS -> Request.Builder().url(url).method("OPTIONS", null)
|
|
190
|
-
}.apply {
|
|
191
|
-
headers.forEach { header -> addHeader(header.key, header.value) }
|
|
192
|
-
}.build()
|
|
193
|
-
|
|
194
|
-
val response = client.newCall(request).execute()
|
|
195
|
-
|
|
196
|
-
val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault())
|
|
197
|
-
|
|
198
|
-
// TODO: handle specific mapping types. e.g. Map<int, Class<?>>
|
|
199
|
-
return when {
|
|
200
|
-
response.isRedirect -> Redirection(
|
|
201
|
-
response.code,
|
|
202
|
-
response.headers.toMultimap()
|
|
203
|
-
)
|
|
204
|
-
|
|
205
|
-
response.isInformational -> Informational(
|
|
206
|
-
response.message,
|
|
207
|
-
response.code,
|
|
208
|
-
response.headers.toMultimap()
|
|
209
|
-
)
|
|
210
|
-
|
|
211
|
-
response.isSuccessful -> Success(
|
|
212
|
-
responseBody(response.body, accept),
|
|
213
|
-
response.code,
|
|
214
|
-
response.headers.toMultimap()
|
|
215
|
-
)
|
|
216
|
-
|
|
217
|
-
response.isClientError -> ClientError(
|
|
218
|
-
response.message,
|
|
219
|
-
response.body?.string(),
|
|
220
|
-
response.code,
|
|
221
|
-
response.headers.toMultimap()
|
|
222
|
-
)
|
|
223
|
-
|
|
224
|
-
else -> ServerError(
|
|
225
|
-
response.message,
|
|
226
|
-
response.body?.string(),
|
|
227
|
-
response.code,
|
|
228
|
-
response.headers.toMultimap()
|
|
229
|
-
)
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
protected fun parameterToString(value: Any?): String = when (value) {
|
|
234
|
-
null -> ""
|
|
235
|
-
is Array<*> -> toMultiValue(value, "csv").toString()
|
|
236
|
-
is Iterable<*> -> toMultiValue(value, "csv").toString()
|
|
237
|
-
is OffsetDateTime, is OffsetTime, is LocalDateTime, is LocalDate, is LocalTime ->
|
|
238
|
-
parseDateToQueryString(value)
|
|
239
|
-
|
|
240
|
-
else -> value.toString()
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
protected fun parseDateToQueryString(value: Any): String {
|
|
244
|
-
/*
|
|
245
|
-
.replace("\"", "") converts the json object string to an actual string for the query parameter.
|
|
246
|
-
The moshi or gson adapter allows a more generic solution instead of trying to use a native
|
|
247
|
-
formatter. It also easily allows to provide a simple way to define a custom date format pattern
|
|
248
|
-
inside a gson/moshi adapter.
|
|
249
|
-
*/
|
|
250
|
-
return objectMapper.writeValueAsString(value).replace("\"", "")
|
|
251
|
-
}
|
|
252
|
-
}
|
|
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 java.io.File
|
|
18
|
+
import java.net.URLConnection
|
|
19
|
+
import java.time.LocalDate
|
|
20
|
+
import java.time.LocalDateTime
|
|
21
|
+
import java.time.LocalTime
|
|
22
|
+
import java.time.OffsetDateTime
|
|
23
|
+
import java.time.OffsetTime
|
|
24
|
+
import java.util.Locale
|
|
25
|
+
|
|
26
|
+
open class ApiClient(@API_CLIENT_PARAMETERS@) {
|
|
27
|
+
companion object {
|
|
28
|
+
protected const val ContentType = "Content-Type"
|
|
29
|
+
protected const val Accept = "Accept"
|
|
30
|
+
protected const val Authorization = "Authorization"
|
|
31
|
+
protected const val JsonMediaType = "application/json"
|
|
32
|
+
protected const val FormDataMediaType = "multipart/form-data"
|
|
33
|
+
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
|
|
34
|
+
protected const val XmlMediaType = "application/xml"
|
|
35
|
+
|
|
36
|
+
val apiKey: MutableMap<String, String> = mutableMapOf()
|
|
37
|
+
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
|
|
38
|
+
var username: String? = null
|
|
39
|
+
var password: String? = null
|
|
40
|
+
var accessToken: String? = null
|
|
41
|
+
const val baseUrlKey = "org.openapitools.client.baseUrl"
|
|
42
|
+
|
|
43
|
+
@JvmStatic
|
|
44
|
+
val defaultClient: OkHttpClient by lazy {
|
|
45
|
+
builder.build()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@JvmStatic
|
|
49
|
+
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
|
|
54
|
+
*
|
|
55
|
+
* @param file The given file
|
|
56
|
+
* @return The guessed Content-Type
|
|
57
|
+
*/
|
|
58
|
+
protected fun guessContentTypeFromFile(file: File): String {
|
|
59
|
+
val contentType = URLConnection.guessContentTypeFromName(file.name)
|
|
60
|
+
return contentType ?: "application/octet-stream"
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
|
64
|
+
when {
|
|
65
|
+
content is File -> content.asRequestBody(
|
|
66
|
+
(mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
mediaType == FormDataMediaType ->
|
|
70
|
+
MultipartBody.Builder()
|
|
71
|
+
.setType(MultipartBody.FORM)
|
|
72
|
+
.apply {
|
|
73
|
+
// content's type *must* be Map<String, PartConfig<*>>
|
|
74
|
+
@Suppress("UNCHECKED_CAST")
|
|
75
|
+
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
|
76
|
+
if (part.body is File) {
|
|
77
|
+
val partHeaders = part.headers.toMutableMap() +
|
|
78
|
+
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
|
79
|
+
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
|
80
|
+
addPart(
|
|
81
|
+
partHeaders.toHeaders(),
|
|
82
|
+
part.body.asRequestBody(fileMediaType)
|
|
83
|
+
)
|
|
84
|
+
} else {
|
|
85
|
+
val partHeaders = part.headers.toMutableMap() +
|
|
86
|
+
("Content-Disposition" to "form-data; name=\"$name\"")
|
|
87
|
+
addPart(
|
|
88
|
+
partHeaders.toHeaders(),
|
|
89
|
+
objectMapper.writeValueAsString(part.body)
|
|
90
|
+
.toRequestBody(JsonMediaType.toMediaTypeOrNull())
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}.build()
|
|
95
|
+
|
|
96
|
+
mediaType == FormUrlEncMediaType -> {
|
|
97
|
+
FormBody.Builder().apply {
|
|
98
|
+
// content's type *must* be Map<String, PartConfig<*>>
|
|
99
|
+
@Suppress("UNCHECKED_CAST")
|
|
100
|
+
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
|
101
|
+
add(name, parameterToString(part.body))
|
|
102
|
+
}
|
|
103
|
+
}.build()
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
mediaType == null || mediaType.startsWith("application/") && mediaType.endsWith("json") ->
|
|
107
|
+
if (content == null) {
|
|
108
|
+
ByteArray(0).toRequestBody(null)
|
|
109
|
+
} else {
|
|
110
|
+
objectMapper.writeValueAsString(content)
|
|
111
|
+
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
|
|
115
|
+
// TODO: this should be extended with other serializers
|
|
116
|
+
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
protected inline fun <reified T : Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
|
|
120
|
+
if (body == null) {
|
|
121
|
+
return null
|
|
122
|
+
}
|
|
123
|
+
if (T::class.java == File::class.java) {
|
|
124
|
+
// return tempFile
|
|
125
|
+
// 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
|
|
126
|
+
val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
|
|
127
|
+
tempFile.deleteOnExit()
|
|
128
|
+
body.byteStream().use { inputStream ->
|
|
129
|
+
tempFile.outputStream().use { tempFileOutputStream ->
|
|
130
|
+
inputStream.copyTo(tempFileOutputStream)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return tempFile as T
|
|
134
|
+
}
|
|
135
|
+
val bodyContent = body.string()
|
|
136
|
+
if (bodyContent.isEmpty()) {
|
|
137
|
+
return null
|
|
138
|
+
}
|
|
139
|
+
return when {
|
|
140
|
+
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
|
|
141
|
+
objectMapper.readValue(bodyContent, object : TypeReference<T>() {})
|
|
142
|
+
|
|
143
|
+
mediaType.startsWith("text/") && T::class.java == String::class.java -> bodyContent as T
|
|
144
|
+
|
|
145
|
+
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
protected inline fun <reified I, reified T : Any?> request(requestConfig: RequestConfig<I>): ApiResponse<T?> {
|
|
150
|
+
val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.")
|
|
151
|
+
|
|
152
|
+
val url = httpUrl.newBuilder()
|
|
153
|
+
.addEncodedPathSegments(requestConfig.path.trimStart('/'))
|
|
154
|
+
.apply {
|
|
155
|
+
requestConfig.query.forEach { query ->
|
|
156
|
+
query.value.forEach { queryValue ->
|
|
157
|
+
addQueryParameter(query.key, queryValue)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}.build()
|
|
161
|
+
|
|
162
|
+
// take content-type/accept from spec or set to default (application/json) if not defined
|
|
163
|
+
if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
|
|
164
|
+
requestConfig.headers[ContentType] = JsonMediaType
|
|
165
|
+
}
|
|
166
|
+
if (requestConfig.headers[Accept].isNullOrEmpty()) {
|
|
167
|
+
requestConfig.headers[Accept] = JsonMediaType
|
|
168
|
+
}
|
|
169
|
+
val headers = requestConfig.headers
|
|
170
|
+
|
|
171
|
+
if (headers[Accept].isNullOrEmpty()) {
|
|
172
|
+
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
val contentType = if (headers[ContentType] != null) {
|
|
176
|
+
// TODO: support multiple contentType options here.
|
|
177
|
+
(headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
|
|
178
|
+
} else {
|
|
179
|
+
null
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
val request = when (requestConfig.method) {
|
|
183
|
+
RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))
|
|
184
|
+
RequestMethod.GET -> Request.Builder().url(url)
|
|
185
|
+
RequestMethod.HEAD -> Request.Builder().url(url).head()
|
|
186
|
+
RequestMethod.PATCH -> Request.Builder().url(url).patch(requestBody(requestConfig.body, contentType))
|
|
187
|
+
RequestMethod.PUT -> Request.Builder().url(url).put(requestBody(requestConfig.body, contentType))
|
|
188
|
+
RequestMethod.POST -> Request.Builder().url(url).post(requestBody(requestConfig.body, contentType))
|
|
189
|
+
RequestMethod.OPTIONS -> Request.Builder().url(url).method("OPTIONS", null)
|
|
190
|
+
}.apply {
|
|
191
|
+
headers.forEach { header -> addHeader(header.key, header.value) }
|
|
192
|
+
}.build()
|
|
193
|
+
|
|
194
|
+
val response = client.newCall(request).execute()
|
|
195
|
+
|
|
196
|
+
val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault())
|
|
197
|
+
|
|
198
|
+
// TODO: handle specific mapping types. e.g. Map<int, Class<?>>
|
|
199
|
+
return when {
|
|
200
|
+
response.isRedirect -> Redirection(
|
|
201
|
+
response.code,
|
|
202
|
+
response.headers.toMultimap()
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
response.isInformational -> Informational(
|
|
206
|
+
response.message,
|
|
207
|
+
response.code,
|
|
208
|
+
response.headers.toMultimap()
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
response.isSuccessful -> Success(
|
|
212
|
+
responseBody(response.body, accept),
|
|
213
|
+
response.code,
|
|
214
|
+
response.headers.toMultimap()
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
response.isClientError -> ClientError(
|
|
218
|
+
response.message,
|
|
219
|
+
response.body?.string(),
|
|
220
|
+
response.code,
|
|
221
|
+
response.headers.toMultimap()
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
else -> ServerError(
|
|
225
|
+
response.message,
|
|
226
|
+
response.body?.string(),
|
|
227
|
+
response.code,
|
|
228
|
+
response.headers.toMultimap()
|
|
229
|
+
)
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
protected fun parameterToString(value: Any?): String = when (value) {
|
|
234
|
+
null -> ""
|
|
235
|
+
is Array<*> -> toMultiValue(value, "csv").toString()
|
|
236
|
+
is Iterable<*> -> toMultiValue(value, "csv").toString()
|
|
237
|
+
is OffsetDateTime, is OffsetTime, is LocalDateTime, is LocalDate, is LocalTime ->
|
|
238
|
+
parseDateToQueryString(value)
|
|
239
|
+
|
|
240
|
+
else -> value.toString()
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
protected fun parseDateToQueryString(value: Any): String {
|
|
244
|
+
/*
|
|
245
|
+
.replace("\"", "") converts the json object string to an actual string for the query parameter.
|
|
246
|
+
The moshi or gson adapter allows a more generic solution instead of trying to use a native
|
|
247
|
+
formatter. It also easily allows to provide a simple way to define a custom date format pattern
|
|
248
|
+
inside a gson/moshi adapter.
|
|
249
|
+
*/
|
|
250
|
+
return objectMapper.writeValueAsString(value).replace("\"", "")
|
|
251
|
+
}
|
|
252
|
+
}
|
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
package @PACKAGE_NAME@
|
|
2
|
-
|
|
3
|
-
enum class ResponseType {
|
|
4
|
-
Success, Informational, Redirection, ClientError, ServerError
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
interface Response
|
|
8
|
-
|
|
9
|
-
abstract class ApiResponse<T>(val responseType: ResponseType) : Response {
|
|
10
|
-
abstract val statusCode: Int
|
|
11
|
-
abstract val headers: Map<String, List<String>>
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
class Success<T>(
|
|
15
|
-
val data: T,
|
|
16
|
-
override val statusCode: Int = -1,
|
|
17
|
-
override val headers: Map<String, List<String>> = mapOf()
|
|
18
|
-
) : ApiResponse<T>(ResponseType.Success)
|
|
19
|
-
|
|
20
|
-
class Informational<T>(
|
|
21
|
-
val statusText: String,
|
|
22
|
-
override val statusCode: Int = -1,
|
|
23
|
-
override val headers: Map<String, List<String>> = mapOf()
|
|
24
|
-
) : ApiResponse<T>(ResponseType.Informational)
|
|
25
|
-
|
|
26
|
-
class Redirection<T>(
|
|
27
|
-
override val statusCode: Int = -1,
|
|
28
|
-
override val headers: Map<String, List<String>> = mapOf()
|
|
29
|
-
) : ApiResponse<T>(ResponseType.Redirection)
|
|
30
|
-
|
|
31
|
-
class ClientError<T>(
|
|
32
|
-
val message: String? = null,
|
|
33
|
-
val body: Any? = null,
|
|
34
|
-
override val statusCode: Int = -1,
|
|
35
|
-
override val headers: Map<String, List<String>> = mapOf()
|
|
36
|
-
) : ApiResponse<T>(ResponseType.ClientError)
|
|
37
|
-
|
|
38
|
-
class ServerError<T>(
|
|
39
|
-
val message: String? = null,
|
|
40
|
-
val body: Any? = null,
|
|
41
|
-
override val statusCode: Int = -1,
|
|
42
|
-
override val headers: Map<String, List<String>>
|
|
43
|
-
) : ApiResponse<T>(ResponseType.ServerError)
|
|
1
|
+
package @PACKAGE_NAME@
|
|
2
|
+
|
|
3
|
+
enum class ResponseType {
|
|
4
|
+
Success, Informational, Redirection, ClientError, ServerError
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface Response
|
|
8
|
+
|
|
9
|
+
abstract class ApiResponse<T>(val responseType: ResponseType) : Response {
|
|
10
|
+
abstract val statusCode: Int
|
|
11
|
+
abstract val headers: Map<String, List<String>>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
class Success<T>(
|
|
15
|
+
val data: T,
|
|
16
|
+
override val statusCode: Int = -1,
|
|
17
|
+
override val headers: Map<String, List<String>> = mapOf()
|
|
18
|
+
) : ApiResponse<T>(ResponseType.Success)
|
|
19
|
+
|
|
20
|
+
class Informational<T>(
|
|
21
|
+
val statusText: String,
|
|
22
|
+
override val statusCode: Int = -1,
|
|
23
|
+
override val headers: Map<String, List<String>> = mapOf()
|
|
24
|
+
) : ApiResponse<T>(ResponseType.Informational)
|
|
25
|
+
|
|
26
|
+
class Redirection<T>(
|
|
27
|
+
override val statusCode: Int = -1,
|
|
28
|
+
override val headers: Map<String, List<String>> = mapOf()
|
|
29
|
+
) : ApiResponse<T>(ResponseType.Redirection)
|
|
30
|
+
|
|
31
|
+
class ClientError<T>(
|
|
32
|
+
val message: String? = null,
|
|
33
|
+
val body: Any? = null,
|
|
34
|
+
override val statusCode: Int = -1,
|
|
35
|
+
override val headers: Map<String, List<String>> = mapOf()
|
|
36
|
+
) : ApiResponse<T>(ResponseType.ClientError)
|
|
37
|
+
|
|
38
|
+
class ServerError<T>(
|
|
39
|
+
val message: String? = null,
|
|
40
|
+
val body: Any? = null,
|
|
41
|
+
override val statusCode: Int = -1,
|
|
42
|
+
override val headers: Map<String, List<String>>
|
|
43
|
+
) : ApiResponse<T>(ResponseType.ServerError)
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
@file:Suppress("unused")
|
|
2
|
-
|
|
3
|
-
package @PACKAGE_NAME@
|
|
4
|
-
|
|
5
|
-
import java.lang.RuntimeException
|
|
6
|
-
|
|
7
|
-
open class ClientException(message: kotlin.String? = null, val statusCode: Int = -1, val response: Response? = null) :
|
|
8
|
-
RuntimeException(message) {
|
|
9
|
-
|
|
10
|
-
companion object {
|
|
11
|
-
private const val serialVersionUID: Long = 123L
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
open class ServerException(message: kotlin.String? = null, val statusCode: Int = -1, val response: Response? = null) :
|
|
16
|
-
RuntimeException(message) {
|
|
17
|
-
|
|
18
|
-
companion object {
|
|
19
|
-
private const val serialVersionUID: Long = 456L
|
|
20
|
-
}
|
|
21
|
-
}
|
|
1
|
+
@file:Suppress("unused")
|
|
2
|
+
|
|
3
|
+
package @PACKAGE_NAME@
|
|
4
|
+
|
|
5
|
+
import java.lang.RuntimeException
|
|
6
|
+
|
|
7
|
+
open class ClientException(message: kotlin.String? = null, val statusCode: Int = -1, val response: Response? = null) :
|
|
8
|
+
RuntimeException(message) {
|
|
9
|
+
|
|
10
|
+
companion object {
|
|
11
|
+
private const val serialVersionUID: Long = 123L
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
open class ServerException(message: kotlin.String? = null, val statusCode: Int = -1, val response: Response? = null) :
|
|
16
|
+
RuntimeException(message) {
|
|
17
|
+
|
|
18
|
+
companion object {
|
|
19
|
+
private const val serialVersionUID: Long = 456L
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
package @PACKAGE_NAME@
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Defines a config object for a given part of a multi-part request.
|
|
5
|
-
* NOTE: Headers is a Map<String,String> because rfc2616 defines
|
|
6
|
-
* multi-valued headers as csv-only.
|
|
7
|
-
*/
|
|
8
|
-
data class PartConfig<T>(
|
|
9
|
-
val headers: MutableMap<String, String> = mutableMapOf(),
|
|
10
|
-
val body: T? = null
|
|
11
|
-
)
|
|
1
|
+
package @PACKAGE_NAME@
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Defines a config object for a given part of a multi-part request.
|
|
5
|
+
* NOTE: Headers is a Map<String,String> because rfc2616 defines
|
|
6
|
+
* multi-valued headers as csv-only.
|
|
7
|
+
*/
|
|
8
|
+
data class PartConfig<T>(
|
|
9
|
+
val headers: MutableMap<String, String> = mutableMapOf(),
|
|
10
|
+
val body: T? = null
|
|
11
|
+
)
|