Skip to content
This repository was archived by the owner on Sep 25, 2024. It is now read-only.

Commit df3b69d

Browse files
authored
Merge pull request #2 from stardogventures/retry-response-and-file-upload
Retry response and file upload
2 parents 849dc7c + 0c03814 commit df3b69d

File tree

7 files changed

+44
-14
lines changed

7 files changed

+44
-14
lines changed

modules/swagger-codegen/src/main/resources/kotlin-client/api.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class {{classname}}(basePath: kotlin.String = "{{{basePath}}}", baseHeaders: Map
5050
ResponseType.Success -> {{#returnType}}(response as Success<*>).data as {{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}
5151
ResponseType.Informational -> TODO()
5252
ResponseType.Redirection -> TODO()
53+
ResponseType.Retry -> throw RetryException((response as RetryResponse<*>).body as? String ?: "Retry request")
5354
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
5455
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
5556
else -> throw kotlin.IllegalStateException("Undefined ResponseType.")

modules/swagger-codegen/src/main/resources/kotlin-client/infrastructure/ApiClient.kt.mustache

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import java.io.File
55
import java.io.IOException
66
import java.util.regex.Pattern
77

8-
open class ApiClient(val baseUrl: String, val baseHeaders: Map<String,String> = emptyMap()) {
8+
open class ApiClient(val baseUrl: String, val baseHeaders: Map<String, String> = emptyMap()) {
99
companion object {
1010
protected val ContentType = "Content-Type"
1111
protected val Accept = "Accept"
@@ -38,15 +38,14 @@ open class ApiClient(val baseUrl: String, val baseHeaders: Map<String,String> =
3838
3939
// content's type *must* be Map<String, Any>
4040
@Suppress("UNCHECKED_CAST")
41-
(content as Map<String,Any>).forEach { key, value ->
41+
(content as Map<String, Any>).forEach { (key, value) ->
4242
if(value::class == File::class) {
4343
val file = value as File
4444
requestBodyBuilder.addFormDataPart(key, file.name, RequestBody.create(MediaType.parse("application/octet-stream"), file))
4545
} else {
4646
val stringValue = value as String
4747
requestBodyBuilder.addFormDataPart(key, stringValue)
4848
}
49-
TODO("Handle other types inside FormDataMediaType")
5049
}
5150
5251
return requestBodyBuilder.build()
@@ -63,9 +62,9 @@ open class ApiClient(val baseUrl: String, val baseHeaders: Map<String,String> =
6362
}
6463
6564
inline protected fun <reified T: Any?> responseBody(response: Response, mediaType: String = JsonMediaType): T? {
66-
if(response.body() == null) return null
65+
if (response.body() == null) return null
6766
68-
if(T::class.java == java.io.File::class.java){
67+
if (T::class.java == java.io.File::class.java) {
6968
return downloadFileFromResponse(response) as T
7069
} else if(T::class == kotlin.Unit::class) {
7170
return kotlin.Unit as T
@@ -77,7 +76,7 @@ open class ApiClient(val baseUrl: String, val baseHeaders: Map<String,String> =
7776
contentType = JsonMediaType
7877
}
7978
80-
if(isJsonMime(contentType)){
79+
if (isJsonMime(contentType)) {
8180
return Serializer.moshi.adapter(T::class.java).fromJson(response.body()?.source())
8281
} else if(contentType.equals(String.javaClass)){
8382
return response.body().toString() as T
@@ -144,6 +143,11 @@ open class ApiClient(val baseUrl: String, val baseHeaders: Map<String,String> =
144143
response.code(),
145144
response.headers().toMultimap()
146145
)
146+
response.code() == 202 -> return RetryResponse(
147+
responseBody(response, accept),
148+
response.code(),
149+
response.headers().toMultimap()
150+
)
147151
response.isSuccessful -> return Success(
148152
responseBody(response, accept),
149153
response.code(),

modules/swagger-codegen/src/main/resources/kotlin-client/infrastructure/ApiInfrastructureResponse.kt.mustache

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package {{packageName}}.infrastructure
22

33
enum class ResponseType {
4-
Success, Informational, Redirection, ClientError, ServerError
4+
Success, Retry, Informational, Redirection, ClientError, ServerError
55
}
66

77
abstract class ApiInfrastructureResponse<T>(val responseType: ResponseType) {
@@ -37,4 +37,10 @@ class ServerError<T>(
3737
val body: Any? = null,
3838
override val statusCode: Int = -1,
3939
override val headers: Map<String, List<String>>
40-
): ApiInfrastructureResponse<T>(ResponseType.ServerError)
40+
): ApiInfrastructureResponse<T>(ResponseType.ServerError)
41+
42+
class RetryResponse<T>(
43+
val body: Any? = null,
44+
override val statusCode: Int = -1,
45+
override val headers: Map<String, List<String>> = mapOf()
46+
) : ApiInfrastructureResponse<T>(ResponseType.Success)

modules/swagger-codegen/src/main/resources/kotlin-client/infrastructure/Errors.kt.mustache

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,23 @@ open class ServerException : RuntimeException {
3939
companion object {
4040
private const val serialVersionUID: Long = 456L
4141
}
42+
}
43+
44+
open class RetryException : RuntimeException {
45+
46+
/**
47+
* Constructs an [RetryException] with no detail message.
48+
*/
49+
constructor() : super()
50+
51+
/**
52+
* Constructs an [RetryException] with the specified detail message.
53+
54+
* @param message the detail message.
55+
*/
56+
constructor(message: kotlin.String) : super(message)
57+
58+
companion object {
59+
private const val serialVersionUID: Long = 789L
60+
}
4261
}

modules/swagger-codegen/src/main/resources/kotlin-client/infrastructure/Serializer.kt.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package {{packageName}}.infrastructure
22

3-
import com.squareup.moshi.KotlinJsonAdapterFactory
4-
import com.squareup.moshi.Moshi
5-
import com.squareup.moshi.Rfc3339DateJsonAdapter
3+
import com.squareup.moshi.*
4+
import java.math.BigDecimal
5+
import java.time.LocalDate
66
import java.util.*
77

88
object Serializer {

modules/swagger-codegen/src/test/java/io/swagger/codegen/kotlin/KotlinClientCodegenModelTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ public void arrayPropertyTest() {
165165
Assert.assertEquals(property.baseName, "examples");
166166
Assert.assertEquals(property.getter, "getExamples");
167167
Assert.assertEquals(property.setter, "setExamples");
168-
Assert.assertEquals(property.datatype, "kotlin.Array<kotlin.String>");
168+
Assert.assertEquals(property.datatype, "kotlin.collections.List<kotlin.String>");
169169
Assert.assertEquals(property.name, "examples");
170170
Assert.assertEquals(property.defaultValue, "null");
171-
Assert.assertEquals(property.baseType, "kotlin.Array");
171+
Assert.assertEquals(property.baseType, "kotlin.collections.List");
172172
Assert.assertEquals(property.containerType, "array");
173173
Assert.assertFalse(property.required);
174174
Assert.assertTrue(property.isContainer);

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@
938938
</repository>
939939
</repositories>
940940
<properties>
941-
<swagger-parser-version>1.0.49-SNAPSHOT</swagger-parser-version>
941+
<swagger-parser-version>1.0.49</swagger-parser-version>
942942
<scala-version>2.11.1</scala-version>
943943
<felix-version>3.3.0</felix-version>
944944
<swagger-core-version>1.6.1-SNAPSHOT</swagger-core-version>

0 commit comments

Comments
 (0)