Created
May 4, 2020 15:48
-
-
Save baggednismo/e475d4e68d90102d3312c096e40cbf57 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
public class OkHttpFactory { | |
private static final int DEFAULT_CONNECTION_TIMEOUT = 30; // seconds | |
private OkHttpClient mOkHttpClient; | |
public void OkHttpFactory() { | |
mOkHttpClient = new OkHttpClient(); | |
} | |
/** | |
* To get default OkHttpClient with out any interceptor or log level. To add OkHttpClient with its properties use {@link Builder} class | |
* | |
* @return default OkHttpClient | |
*/ | |
public OkHttpClient getOkHttpClient() { | |
return mOkHttpClient; | |
} | |
/** | |
* Builder class to add properties in OkHttpClient like read timeout, connection timeout, Interceptor | |
*/ | |
public static class Builder { | |
private OkHttpClient.Builder mBuilder; | |
public Builder() { | |
mBuilder = new OkHttpClient.Builder(); | |
setConnectTimeOut(DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS); | |
setReadTimeOut(DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS); // socket timeout | |
setWriteTimeOut(DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS); | |
} | |
/** | |
* To get {@link OkHttpClient} instance with it properties set by using builder methods | |
* | |
* @return {@link OkHttpClient} instance | |
*/ | |
public OkHttpClient build() { | |
return mBuilder.build(); | |
} | |
/** | |
* To add given headers with given key and value | |
* | |
* @param aHeaderKeyValue list of key value pairs | |
* @return {@link Builder} instance to add new properties or to get {@link OkHttpClient}instance | |
*/ | |
public Builder addHeaders(ArrayMap<String, String> aHeaderKeyValue) { | |
final RequestInterceptor requestInterceptor = new RequestInterceptor(aHeaderKeyValue); | |
mBuilder.addInterceptor(requestInterceptor); | |
return this; | |
} | |
/** | |
* To add Http request, response or header log based on given log level | |
* | |
* @param aLevel Log level to print in logcat | |
* @return {@link Builder} instance to add new properties or to get {@link OkHttpClient}instance | |
* @see HttpLoggingInterceptor.Level | |
*/ | |
public Builder enableHttpLog(HttpLoggingInterceptor.Level aLevel) { | |
final HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); | |
httpLoggingInterceptor.setLevel(aLevel); | |
mBuilder.addInterceptor(httpLoggingInterceptor); | |
return this; | |
} | |
/** | |
* To add cookie manager for session web api integration | |
* | |
* @param aContext Application or activity context to store cookies in shared pref. Recommend to use Application context | |
* @return {@link Builder} instance to add new properties or to get {@link OkHttpClient}instance | |
*/ | |
public Builder addCookieManager(Context aContext) { | |
final CookieHandler cookieHandler = new CookieManager(new PersistentCookieStore(aContext), CookiePolicy.ACCEPT_ALL); | |
mBuilder.cookieJar(new JavaNetCookieJar(cookieHandler)); | |
return this; | |
} | |
/** | |
* To add any Interceptor in OkHttpClient interceptor list | |
* | |
* @param aInterceptor | |
* @return {@link Builder} instance to add new properties or to get {@link OkHttpClient}instance | |
*/ | |
public Builder addInterceptor(Interceptor aInterceptor) { | |
if (aInterceptor != null) | |
mBuilder.addInterceptor(aInterceptor); | |
return this; | |
} | |
/** | |
* @param aAuthenticator authenticator to authenticate user for session based logic | |
* @return {@link Builder} instance to add new properties or to get {@link OkHttpClient}instance | |
*/ | |
public Builder addAuthenticator(Authenticator aAuthenticator) { | |
if (aAuthenticator != null) | |
mBuilder.authenticator(aAuthenticator); | |
return this; | |
} | |
/** | |
* To set read time out for web api | |
* | |
* @param aReadTimeOut Read time out for api calling. | |
* @param aTimeUnit Time unit for read time out | |
* @return {@link Builder} instance to add new properties or to get {@link OkHttpClient}instance | |
*/ | |
public void setReadTimeOut(long aReadTimeOut, TimeUnit aTimeUnit) { | |
mBuilder.readTimeout(aReadTimeOut, aTimeUnit); | |
} | |
/** | |
* To set connection timeout for web api. | |
* | |
* @param aConnectionTimeout connection time out for api calling. | |
* @param aTimeUnit Time unit for read time out | |
* @return {@link Builder} instance to add new properties or to get {@link OkHttpClient}instance | |
*/ | |
public void setConnectTimeOut(long aConnectionTimeout, TimeUnit aTimeUnit) { | |
mBuilder.connectTimeout(aConnectionTimeout, aTimeUnit); | |
} | |
public void setWriteTimeOut(long aConnectionTimeout, TimeUnit aTimeUnit) { | |
mBuilder.writeTimeout(aConnectionTimeout, aTimeUnit); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment