Last active
February 8, 2020 07:46
-
-
Save esabook/51dffaacc199a3265c3191817475e1d5 to your computer and use it in GitHub Desktop.
retrofit service builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ***.__data; | |
import android.os.Handler; | |
import android.os.Looper; | |
import com.google.gson.GsonBuilder; | |
import ***.R; | |
import ***.utils.Contents; | |
import ***.utils.__ConnectionEvent; | |
import ***.utils.__GlobalApp; | |
import ***.utils.__Utils; | |
import ***.utils.helper.serializers.DoubleDeserializer; | |
import ***.utils.helper.serializers.StringDeserializer; | |
import java.io.IOException; | |
import java.util.concurrent.TimeUnit; | |
import okhttp3.Interceptor; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
public class __DataFactory { | |
public static String __URL = __GlobalApp | |
.getAppContext() | |
.getResources() | |
.getString(R.string.urlServer); | |
public static OkHttpClient.Builder clientBuilder; | |
private static String token; | |
private static GsonBuilder mGsonBuilder = new GsonBuilder(); | |
private Retrofit mRetrofit; | |
////INIT | |
public __DataFactory() { | |
try { | |
setToken(Contents.getLogInfo().getToken()); | |
} catch (Exception ignore) { | |
} | |
} | |
public __DataFactory(String url, String token) { | |
__URL = url; | |
setToken(token); | |
} | |
public __DataFactory(String token) { | |
setToken(token); | |
} | |
public String getToken() { | |
return this.token; | |
} | |
////GET-SET | |
public __DataFactory setToken(String tokenStr) { | |
this.token = tokenStr; | |
return this; | |
} | |
/** | |
* create retrofit object based | |
* | |
* @param t Class of T | |
* @return | |
*/ | |
public <T> T create(Class<T> t) { | |
return getRetrofit().create(t); | |
} | |
/** | |
* Get retrofit build | |
* | |
* @return Retrofit | |
*/ | |
public Retrofit getRetrofit() { | |
return getRetrofit(__URL); | |
} | |
public static GsonBuilder getGsonBuilder() { | |
mGsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss"); | |
mGsonBuilder.registerTypeAdapter(Double.class, new DoubleDeserializer()); | |
mGsonBuilder.registerTypeAdapter(String.class, new StringDeserializer()); | |
return mGsonBuilder; | |
} | |
/** | |
* @param url | |
* @return | |
*/ | |
public Retrofit getRetrofit(String url) { | |
if (mRetrofit == null) { | |
mRetrofit = new Retrofit.Builder() | |
.baseUrl(url) | |
.addConverterFactory(GsonConverterFactory.create(getGsonBuilder().create())) | |
.client(getHttpConfiguration()) | |
.build(); | |
} | |
return mRetrofit; | |
} | |
/** | |
* | |
* @return | |
*/ | |
public OkHttpClient getHttpConfiguration() { | |
if (clientBuilder != null) return clientBuilder.build(); | |
int connectTimeOutMS = 2000; | |
int readTimeOutMS = 2000; | |
clientBuilder = new OkHttpClient.Builder() | |
.addInterceptor(new Interceptor() { | |
@Override | |
public Response intercept(Chain chain) throws IOException { | |
Request newRequest = chain.request() | |
.newBuilder() | |
.addHeader("Authorization", "Bearer " + getToken()) | |
.build(); | |
Response response = chain.proceed(newRequest); | |
if (response.code() == 401) { | |
new Handler(Looper.getMainLooper()).post(__Utils::relogin); | |
} | |
return response; | |
} | |
}) | |
.eventListener(new __ConnectionEvent()) | |
.connectTimeout(connectTimeOutMS, TimeUnit.SECONDS) | |
.readTimeout(readTimeOutMS, TimeUnit.SECONDS); | |
return clientBuilder.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment