Last active
March 30, 2019 18:35
-
-
Save Zhuinden/57ca70f4a7ede5281f59b0207a9e6214 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 interface RetrofitService { | |
@POST("players") | |
Call<Profile> registerUser(@Body Registration registration); | |
} | |
public abstract class RetrofitCallback<T> | |
implements Callback<T> { | |
private static final String TAG = "RetrofitCallback"; | |
Retrofit retrofit; | |
Service<T> service; | |
public RetrofitCallback(Retrofit retrofit, Service<T> service) { | |
this.retrofit = retrofit; | |
this.service = service; | |
} | |
@Override | |
public void onResponse(Call<T> call, Response<T> response) { | |
if(!RetrofitErrorHandler.handleError(retrofit, response)) { | |
SingletonBus.INSTANCE.post(service.createSuccessEvent(response.body())); | |
} | |
} | |
@Override | |
public void onFailure(Call<T> call, Throwable t) { | |
Log.e(TAG, "Failed request [" + call.request().url().toString() + "]", t); | |
RetrofitErrorHandler.handleFailure(retrofit, t); | |
} | |
public static <T> RetrofitCallback<T> create(Retrofit retrofit, Service<T> service) { | |
return new RetrofitCallback<T>(retrofit, service) { | |
}; | |
} | |
} | |
public final class RetrofitErrorHandler { | |
private static final String TAG = "RetrofitErrorHandler"; | |
public static class FailureEvent | |
extends EventResult<Throwable> { | |
public FailureEvent(Throwable result) { | |
super(result); | |
} | |
} | |
public static class ErrorEvent | |
extends EventResult<ErrorResponse> { | |
public ErrorEvent(ErrorResponse result) { | |
super(result); | |
_hasResult = result != null; | |
} | |
boolean _hasResult; | |
public boolean hasResult() { | |
return _hasResult; | |
} | |
} | |
public interface ErrorListener { | |
void onEventMainThread(ErrorEvent e); | |
void onEventMainThread(FailureEvent e); | |
} | |
public static boolean handleError(Retrofit retrofit, Response<?> response) { | |
if(response != null && !response.isSuccessful() && response.errorBody() != null) { | |
Converter<ResponseBody, ErrorResponse> converter = retrofit.responseBodyConverter(ErrorResponse.class, new Annotation[0]); | |
try { | |
ErrorResponse errorResponse = converter.convert(response.errorBody()); | |
SingletonBus.INSTANCE.post(new RetrofitErrorHandler.ErrorEvent(errorResponse)); | |
} catch(IOException e) { | |
Log.e(TAG, "An error occurred", e); | |
SingletonBus.INSTANCE.post(new RetrofitErrorHandler.ErrorEvent(null)); | |
} | |
return true; | |
} | |
return false; | |
} | |
public static void handleFailure(Retrofit retrofit, Throwable throwable) { | |
SingletonBus.INSTANCE.post(new FailureEvent(throwable)); | |
} | |
} | |
public interface Service<T> { | |
EventResult<T> createSuccessEvent(T t); | |
} | |
public class EventResult<T> { | |
private T result; | |
public EventResult(T result) { | |
this.result = result; | |
} | |
public T getResult() { | |
return result; | |
} | |
} | |
public interface RegisterService | |
extends Service<Profile> { | |
class SuccessEvent | |
extends EventResult<Profile> { | |
public SuccessEvent(Profile result) { | |
super(result); | |
} | |
} | |
interface Listener | |
extends RetrofitErrorHandler.ErrorListener { | |
void onEventMainThread(SuccessEvent e); | |
} | |
void registerUser(Registration registration); | |
} | |
@Singleton | |
public class RegisterServiceImpl | |
extends BaseService<Profile> | |
implements RegisterService { | |
private static final String TAG = "RegisterServiceImpl"; | |
@Inject | |
public RegisterServiceImpl(Retrofit retrofit, RetrofitService retrofitService) { | |
super(retrofit, retrofitService); | |
} | |
@Override | |
public void registerUser(Registration registration) { | |
retrofitService.registerUser(registration).enqueue(RetrofitCallback.create(retrofit, this)); | |
} | |
@Override | |
public EventResult<Profile> createSuccessEvent(Profile profile) { | |
return new SuccessEvent(profile); | |
} | |
} | |
@Module | |
public class ServiceModule { | |
@Provides | |
@Singleton | |
public Retrofit retrofit(OkHttpClient okHttpClient) { | |
return new Retrofit.Builder() | |
.baseUrl(AppConfig.API_URL) | |
.addConverterFactory(LoganSquareConverterFactory.create()) | |
.client(okHttpClient) | |
.build(); | |
} | |
@Provides | |
@Singleton | |
public RetrofitService retrofitService(Retrofit retrofit) { | |
return retrofit.create(RetrofitService.class); | |
} | |
@Provides | |
public RegisterService registerService(RegisterServiceImpl impl) { | |
return impl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks