From 5616eb908ea6feb393367cbcbb60d69a6904171c Mon Sep 17 00:00:00 2001 From: Lucas Paim Date: Fri, 25 Aug 2017 14:54:31 -0300 Subject: [PATCH] permit a dynamic gson configure --- .../vithor/yamvpframework/rest/api/ApiClient.kt | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/rest/src/main/java/io/vithor/yamvpframework/rest/api/ApiClient.kt b/rest/src/main/java/io/vithor/yamvpframework/rest/api/ApiClient.kt index f29d900..1a8b47e 100644 --- a/rest/src/main/java/io/vithor/yamvpframework/rest/api/ApiClient.kt +++ b/rest/src/main/java/io/vithor/yamvpframework/rest/api/ApiClient.kt @@ -10,7 +10,10 @@ import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory import java.util.* -class ApiClient(var baseUrl: String, val interceptor: Interceptor? = null, vararg var providedInterceptors: Interceptor?) { +class ApiClient(var baseUrl: String, + val interceptor: Interceptor? = null, + vararg var providedInterceptors: Interceptor?, + gsonConfigure: ((builder: GsonBuilder) -> Unit)? = null) { var apiAuthorizations: MutableMap? = null get() = apiAuthorizations @@ -23,15 +26,16 @@ class ApiClient(var baseUrl: String, val interceptor: Interceptor? = null, varar init { if (!baseUrl.endsWith("/")) baseUrl += "/" - apiAuthorizations = LinkedHashMap() - createDefaultAdapter(baseUrl) + apiAuthorizations = LinkedHashMap() + createDefaultAdapter(baseUrl, gsonConfigure) } - fun createDefaultAdapter(baseUrl: String) { - val gson = GsonBuilder() + fun createDefaultAdapter(baseUrl: String, gsonConfigure: ((builder: GsonBuilder) -> Unit)?) { + val gsonBuilder = GsonBuilder() // .registerTypeAdapter(Date.class, new GsonDateMultiDeserializer()) .setDateFormat("EEE, dd MMM yyyy HH:mm:ss z") - .create() + gsonConfigure?.invoke(gsonBuilder) + val gson = gsonBuilder.create() val okBuilder = OkHttpClient.Builder() .addInterceptor {