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

Commit

Permalink
Merge pull request #2 from stardogventures/retry-response-and-file-up…
Browse files Browse the repository at this point in the history
…load

Retry response and file upload
  • Loading branch information
eonwhite authored Jul 15, 2020
2 parents 849dc7c + 0c03814 commit df3b69d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class {{classname}}(basePath: kotlin.String = "{{{basePath}}}", baseHeaders: Map
ResponseType.Success -> {{#returnType}}(response as Success<*>).data as {{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.Retry -> throw RetryException((response as RetryResponse<*>).body as? String ?: "Retry request")
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
else -> throw kotlin.IllegalStateException("Undefined ResponseType.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.io.File
import java.io.IOException
import java.util.regex.Pattern

open class ApiClient(val baseUrl: String, val baseHeaders: Map<String,String> = emptyMap()) {
open class ApiClient(val baseUrl: String, val baseHeaders: Map<String, String> = emptyMap()) {
companion object {
protected val ContentType = "Content-Type"
protected val Accept = "Accept"
Expand Down Expand Up @@ -38,15 +38,14 @@ open class ApiClient(val baseUrl: String, val baseHeaders: Map<String,String> =
// content's type *must* be Map<String, Any>
@Suppress("UNCHECKED_CAST")
(content as Map<String,Any>).forEach { key, value ->
(content as Map<String, Any>).forEach { (key, value) ->
if(value::class == File::class) {
val file = value as File
requestBodyBuilder.addFormDataPart(key, file.name, RequestBody.create(MediaType.parse("application/octet-stream"), file))
} else {
val stringValue = value as String
requestBodyBuilder.addFormDataPart(key, stringValue)
}
TODO("Handle other types inside FormDataMediaType")
}
return requestBodyBuilder.build()
Expand All @@ -63,9 +62,9 @@ open class ApiClient(val baseUrl: String, val baseHeaders: Map<String,String> =
}
inline protected fun <reified T: Any?> responseBody(response: Response, mediaType: String = JsonMediaType): T? {
if(response.body() == null) return null
if (response.body() == null) return null
if(T::class.java == java.io.File::class.java){
if (T::class.java == java.io.File::class.java) {
return downloadFileFromResponse(response) as T
} else if(T::class == kotlin.Unit::class) {
return kotlin.Unit as T
Expand All @@ -77,7 +76,7 @@ open class ApiClient(val baseUrl: String, val baseHeaders: Map<String,String> =
contentType = JsonMediaType
}
if(isJsonMime(contentType)){
if (isJsonMime(contentType)) {
return Serializer.moshi.adapter(T::class.java).fromJson(response.body()?.source())
} else if(contentType.equals(String.javaClass)){
return response.body().toString() as T
Expand Down Expand Up @@ -144,6 +143,11 @@ open class ApiClient(val baseUrl: String, val baseHeaders: Map<String,String> =
response.code(),
response.headers().toMultimap()
)
response.code() == 202 -> return RetryResponse(
responseBody(response, accept),
response.code(),
response.headers().toMultimap()
)
response.isSuccessful -> return Success(
responseBody(response, accept),
response.code(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package {{packageName}}.infrastructure

enum class ResponseType {
Success, Informational, Redirection, ClientError, ServerError
Success, Retry, Informational, Redirection, ClientError, ServerError
}

abstract class ApiInfrastructureResponse<T>(val responseType: ResponseType) {
Expand Down Expand Up @@ -37,4 +37,10 @@ class ServerError<T>(
val body: Any? = null,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>>
): ApiInfrastructureResponse<T>(ResponseType.ServerError)
): ApiInfrastructureResponse<T>(ResponseType.ServerError)

class RetryResponse<T>(
val body: Any? = null,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
) : ApiInfrastructureResponse<T>(ResponseType.Success)
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,23 @@ open class ServerException : RuntimeException {
companion object {
private const val serialVersionUID: Long = 456L
}
}

open class RetryException : RuntimeException {
/**
* Constructs an [RetryException] with no detail message.
*/
constructor() : super()
/**
* Constructs an [RetryException] with the specified detail message.
* @param message the detail message.
*/
constructor(message: kotlin.String) : super(message)
companion object {
private const val serialVersionUID: Long = 789L
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package {{packageName}}.infrastructure

import com.squareup.moshi.KotlinJsonAdapterFactory
import com.squareup.moshi.Moshi
import com.squareup.moshi.Rfc3339DateJsonAdapter
import com.squareup.moshi.*
import java.math.BigDecimal
import java.time.LocalDate
import java.util.*

object Serializer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ public void arrayPropertyTest() {
Assert.assertEquals(property.baseName, "examples");
Assert.assertEquals(property.getter, "getExamples");
Assert.assertEquals(property.setter, "setExamples");
Assert.assertEquals(property.datatype, "kotlin.Array<kotlin.String>");
Assert.assertEquals(property.datatype, "kotlin.collections.List<kotlin.String>");
Assert.assertEquals(property.name, "examples");
Assert.assertEquals(property.defaultValue, "null");
Assert.assertEquals(property.baseType, "kotlin.Array");
Assert.assertEquals(property.baseType, "kotlin.collections.List");
Assert.assertEquals(property.containerType, "array");
Assert.assertFalse(property.required);
Assert.assertTrue(property.isContainer);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@
</repository>
</repositories>
<properties>
<swagger-parser-version>1.0.49-SNAPSHOT</swagger-parser-version>
<swagger-parser-version>1.0.49</swagger-parser-version>
<scala-version>2.11.1</scala-version>
<felix-version>3.3.0</felix-version>
<swagger-core-version>1.6.1-SNAPSHOT</swagger-core-version>
Expand Down

0 comments on commit df3b69d

Please sign in to comment.