Last active
August 31, 2020 15:22
-
-
Save Masoud-z/ee79165ad0c109f6f0c104f28484d16c to your computer and use it in GitHub Desktop.
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
Here yo can find Retrofit Builder with HttpLoggingInterceptor. |
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
import android.content.Context; | |
import android.util.Log; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import java.io.IOException; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.LinkedBlockingQueue; | |
import java.util.concurrent.ThreadPoolExecutor; | |
import java.util.concurrent.TimeUnit; | |
import okhttp3.Dispatcher; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.ResponseBody; | |
import okhttp3.logging.HttpLoggingInterceptor; | |
import retrofit2.Response; | |
import retrofit2.Retrofit; | |
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
import retrofit2.converter.scalars.ScalarsConverterFactory; | |
public class RetrofitBase { | |
private static final String TAG = RetrofitBase.class.getName(); | |
protected Retrofit retrofit; | |
protected Context context; | |
private Logger logger; | |
private SharedPreferencesHelper preferencesHelper; | |
public RetrofitBase(Context context) { | |
init(context, false); | |
} | |
public RetrofitBase(Context context, boolean isUploading) { | |
init(context, isUploading); | |
} | |
private void init(Context context, boolean isUploading) { | |
Log.i(TAG, "RetrofitBase: " + System.currentTimeMillis()); | |
this.context = context; | |
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); | |
if (BuildConfig.DEBUG) { | |
interceptor.level(HttpLoggingInterceptor.Level.BODY); | |
} else { | |
interceptor.level(HttpLoggingInterceptor.Level.NONE); | |
} | |
OkHttpClient.Builder httpClientBuilder = new OkHttpClient().newBuilder().addInterceptor(interceptor); | |
httpClientBuilder.addInterceptor(new NetworkInterceptor(context)); | |
httpClientBuilder.addInterceptor(new DomainInterceptor()); | |
ExecutorService exec = new ThreadPoolExecutor(20, 20, 1, TimeUnit.HOURS, new LinkedBlockingQueue<>()); | |
Dispatcher d = new Dispatcher(exec); | |
d.setMaxRequestsPerHost(20); | |
httpClientBuilder.dispatcher(d); | |
if (!isUploading) { | |
httpClientBuilder.readTimeout(RetrofitConstants.TimeOut.SOCKET_TIME_OUT, TimeUnit.SECONDS); | |
httpClientBuilder.connectTimeout(RetrofitConstants.TimeOut.CONNECTION_TIME_OUT, TimeUnit.SECONDS); | |
} else { | |
httpClientBuilder | |
.readTimeout(RetrofitConstants.TimeOut.UPLOAD_SOCKET_TIMEOUT, TimeUnit.SECONDS) | |
.connectTimeout(RetrofitConstants.TimeOut.UPLOAD_CONNECTION_TIMEOUT, TimeUnit.SECONDS) | |
.writeTimeout(RetrofitConstants.TimeOut.UPLOAD_CONNECTION_TIMEOUT, TimeUnit.SECONDS) | |
.callTimeout(RetrofitConstants.TimeOut.UPLOAD_CONNECTION_TIMEOUT, TimeUnit.SECONDS); | |
} | |
addHeaders(httpClientBuilder, context); | |
OkHttpClient httpClient = httpClientBuilder.build(); | |
logger = new Logger(RetrofitBase.class.getSimpleName()); | |
Gson gson = new GsonBuilder() | |
.setLenient() | |
.create(); | |
retrofit = new Retrofit.Builder() | |
.baseUrl(ApiConstants.BASE_URL) | |
.client(httpClient) | |
.addConverterFactory(GsonConverterFactory.create(gson)) | |
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | |
.addConverterFactory(ScalarsConverterFactory.create()) | |
.build(); | |
} | |
private void addHeaders(OkHttpClient.Builder builder, Context context) { | |
final String authorization = ApiConstants.AUTHORIZATION_HEADER; | |
final String contentType = ApiConstants.CONTENT_TYPE_HEADER; | |
final String contentTypeValue = ApiConstants.APPLICATION_JSON; | |
final String authorizationValue = SaveData.getAuthCode(); | |
builder.interceptors().add(chain -> { | |
Request request = chain.request().newBuilder() | |
.addHeader(authorization, authorizationValue) | |
.addHeader(contentType, contentTypeValue) | |
.build(); | |
return chain.proceed(request); | |
}); | |
} | |
void validateResponse(Response response, ApiResponseListener apiResponseListener, ApiConstants.ApiFlags apiFlag, long eventId) { | |
if (response.code() == 200) { | |
NetworkEventBus.delete(eventId); | |
ResponseBody responseBody = (ResponseBody) response.body(); | |
try { | |
apiResponseListener.onResponseSuccess(responseBody != null ? responseBody.string() : null, apiFlag); | |
} catch (IOException e) { | |
error(response, apiResponseListener, apiFlag); | |
} | |
} else { | |
error(response, apiResponseListener, apiFlag); | |
} | |
} | |
private void error(Response response, ApiResponseListener apiResponseListener, ApiConstants.ApiFlags apiFlag) { | |
Gson gson = new Gson(); | |
ApiStatus apiStatusPojo; | |
try { | |
apiStatusPojo = gson.fromJson((response.errorBody()).string(), ApiStatus.class); | |
apiStatusPojo.setResponseCode(response.code()); | |
if (apiStatusPojo == null) { | |
apiStatusPojo = HttpUtil.getServerErrorPojo(context); | |
} | |
apiResponseListener.onResponseError(apiStatusPojo, null, apiFlag); | |
} catch (Exception e) { | |
apiResponseListener.onResponseError(HttpUtil.getServerErrorPojo(context), null, apiFlag); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment