Created
March 12, 2018 14:10
-
-
Save SergeyKharuk/129afa39fa94e69e90c5d68ebc582437 to your computer and use it in GitHub Desktop.
Singleton connector
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 walltreesoft.petpals.api; | |
import android.content.Context; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import java.util.concurrent.TimeUnit; | |
import okhttp3.OkHttpClient; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
import walltreesoft.petpals.api.managers.IChatManager; | |
import walltreesoft.petpals.api.managers.IEventManager; | |
import walltreesoft.petpals.api.managers.IForumManager; | |
import walltreesoft.petpals.api.managers.IFriendsManager; | |
import walltreesoft.petpals.api.managers.IGalleryManager; | |
import walltreesoft.petpals.api.managers.INotificationManager; | |
import walltreesoft.petpals.api.managers.IPetManager; | |
import walltreesoft.petpals.api.managers.IPlacesManager; | |
import walltreesoft.petpals.api.managers.IPostManager; | |
import walltreesoft.petpals.api.managers.IUserManager; | |
import walltreesoft.petpals.api.managers.IVetRecordManager; | |
public class Connector { | |
private final Retrofit mRetrofit; | |
private static Connector INSTANCE_CONNECTOR; | |
private Context context; | |
/** | |
* Configure Retrofit instance with and GsonConverter | |
*/ | |
private Connector(Context context) { | |
final OkHttpClient.Builder clientBuilder = Config.getClientBuilderWithCookieAction(); | |
this.context = context; | |
clientBuilder.readTimeout(60, TimeUnit.SECONDS); | |
clientBuilder.connectTimeout(30, TimeUnit.SECONDS); | |
final OkHttpClient client = clientBuilder | |
.addInterceptor(new ConnectivityInterceptor(context)) | |
.build(); | |
Gson gson = new GsonBuilder() | |
.setLenient() | |
.create(); | |
this.mRetrofit = new Retrofit.Builder() | |
.baseUrl(Config.BASE_URL) | |
.client(client) | |
.addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create()) | |
.addConverterFactory(new NullOnEmptyConverterFactory()) | |
.addConverterFactory(GsonConverterFactory.create(gson)) | |
.build(); | |
} | |
public static Connector getInstance(Context context){ | |
if (INSTANCE_CONNECTOR == null) { | |
INSTANCE_CONNECTOR = new Connector(context); | |
} | |
return INSTANCE_CONNECTOR; | |
} | |
/** | |
* @return retrofit rest interface with pet shit related methods | |
*/ | |
public IUserManager getUserManager() { | |
return mRetrofit.create(IUserManager.class); | |
} | |
public IPetManager getPetManager() { | |
return mRetrofit.create(IPetManager.class); | |
} | |
public IPostManager getPostManager() { | |
return mRetrofit.create(IPostManager.class); | |
} | |
public IVetRecordManager getVetRecordsManager() { | |
return mRetrofit.create(IVetRecordManager.class); | |
} | |
public IEventManager getEventManager() { | |
return mRetrofit.create(IEventManager.class); | |
} | |
public IGalleryManager getGalleryManager() { | |
return mRetrofit.create(IGalleryManager.class); | |
} | |
public IForumManager getForumManager() { | |
return mRetrofit.create(IForumManager.class); | |
} | |
public IFriendsManager getFriendsManager() { | |
return mRetrofit.create(IFriendsManager.class); | |
} | |
public IChatManager getChatManager() { | |
return mRetrofit.create(IChatManager.class); | |
} | |
public IPlacesManager getPlacesManager() { | |
return mRetrofit.create(IPlacesManager.class); | |
} | |
public INotificationManager getNotificationManager() { | |
return mRetrofit.create(INotificationManager.class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment