Created
February 22, 2019 05:06
-
-
Save raghunandankavi2010/5113a10843ae19ad08150ea899a8757d to your computer and use it in GitHub Desktop.
Service Generator
This file contains hidden or 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.text.TextUtils; | |
import org.jetbrains.annotations.NotNull; | |
import java.io.IOException; | |
import java.util.concurrent.TimeUnit; | |
import okhttp3.Interceptor; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import okhttp3.logging.HttpLoggingInterceptor; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
public class ServiceGenerator { | |
public static final String API_BASE_URL = "https://someurl.com"; | |
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); | |
private static Retrofit.Builder builder = | |
new Retrofit.Builder() | |
.baseUrl(API_BASE_URL) | |
.addConverterFactory(GsonConverterFactory.create()); | |
private static Retrofit retrofit = builder.build(); | |
public static <S> S createService(Class<S> serviceClass) { | |
return createService(serviceClass, null, null); | |
} | |
public static <S> S createService( | |
Class<S> serviceClass, String username, String authtoken) { | |
if (!TextUtils.isEmpty(username) | |
&& !TextUtils.isEmpty(authtoken)) { | |
return createServices(serviceClass,username, authtoken); | |
} | |
return createServices(serviceClass,null, null); | |
} | |
public static <S> S createServices( | |
Class<S> serviceClass, final String username , final String authToken) { | |
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); | |
logging.setLevel(HttpLoggingInterceptor.Level.BODY); | |
httpClient.readTimeout(5000, TimeUnit.SECONDS); | |
httpClient .callTimeout(5000, TimeUnit.SECONDS); | |
httpClient .addInterceptor(logging); | |
if (!TextUtils.isEmpty(authToken)) { | |
ServiceInterceptor interceptor = | |
new ServiceInterceptor(username,authToken); | |
if (!httpClient.interceptors().contains(interceptor)) { | |
httpClient.addInterceptor(interceptor); | |
builder.client(httpClient.build()); | |
retrofit = builder.build(); | |
} | |
} | |
return retrofit.create(serviceClass); | |
} | |
static class ServiceInterceptor implements Interceptor { | |
private String mUsername,authtoken; | |
public ServiceInterceptor(String username, String authToken) { | |
mUsername = username; | |
authtoken = authToken; | |
} | |
@NotNull | |
@Override | |
public Response intercept(@NotNull Chain chain) throws IOException { | |
Request request = chain.request(); | |
if (request.header("No-Authentication") == null) { | |
// to be replaced by real auth token once api is ready | |
// auth token from shared preferences which is stored once user login | |
request = request.newBuilder().addHeader("authtoken", "authtoken").addHeader("username","username").build(); | |
} | |
return chain.proceed(request); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment