// Path: adal-utils/src/main/java/com/massivedisaster/adal/utils/LogUtils.java
// public final class LogUtils {
// 
//     /**
//      * Private constructor to avoid user implement as a single instance instead of a Singleton
//      */
//     private LogUtils() {
// 
//     }
// 
//     /**
//      * Logs a error exception
//      *
//      * @param enclosingClass the caller class
//      * @param exception      the exception
//      */
//     public static void logErrorException(@NonNull Class<?> enclosingClass, @NonNull Throwable exception) {
//         String message = exception.getMessage() == null ? "Exception error." : exception.getMessage();
//         Log.e(enclosingClass.getName(), message, exception);
//     }
// 
// }


import com.massivedisaster.adal.utils.LogUtils;
import java.io.IOException;
import java.lang.reflect.Field;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Converter;
import retrofit2.Response;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.util.Log;
/*
 * ADAL - A set of Android libraries to help speed up Android development.
 *
 * Copyright (c) 2017 ADAL
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.massivedisaster.adal.network;





/**
 * Base request API callbacks.
 *
 * @param <T> the type of the data in the requests.
 */
public abstract class APIRequestCallback<T extends APIErrorListener> implements Callback<T> {

    private final Context mContext;

    /**
     * Constructcs {@link APIRequestCallback}
     *
     * @param context the context.
     */
    public APIRequestCallback(Context context) {
        mContext = context;
    }

    /**
     * @param t response
     */
    public abstract void onSuccess(T t);

    /**
     * @param error         the error.
     * @param isServerError is a server error.
     */
    public abstract void onError(APIError error, boolean isServerError);

    /**
     * Called when response ends.
     *
     * @param call     the call.
     * @param response the response.
     */
    @Override
    public void onResponse(Call<T> call, Response<T> response) {

        if (call.isCanceled()) {
            return;
        }

        if (response == null) {
            processError(new APIError(mContext.getString(R.string.error_network_general)), true);
            return;
        }

        if (response.errorBody() != null) {
            try {
                Converter<ResponseBody, T> errorConverter = getRetrofitConverter(call);

                T error = errorConverter.convert(response.errorBody());

                if (error != null) {
                    processError(new APIError(error.getErrorCode(), error.getError()), true);
                    return;
                }
            } catch (IllegalAccessException e) {
// Path: adal-utils/src/main/java/com/massivedisaster/adal/utils/LogUtils.java
// public final class LogUtils {
// 
//     /**
//      * Private constructor to avoid user implement as a single instance instead of a Singleton
//      */
//     private LogUtils() {
// 
//     }
// 
//     /**
//      * Logs a error exception
//      *
//      * @param enclosingClass the caller class
//      * @param exception      the exception
//      */
//     public static void logErrorException(@NonNull Class<?> enclosingClass, @NonNull Throwable exception) {
//         String message = exception.getMessage() == null ? "Exception error." : exception.getMessage();
//         Log.e(enclosingClass.getName(), message, exception);
//     }
// 
// }


// Path: adal-network/src/main/java/com/massivedisaster/adal/network/APIRequestCallback.java
import com.massivedisaster.adal.utils.LogUtils;
import java.io.IOException;
import java.lang.reflect.Field;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Converter;
import retrofit2.Response;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.util.Log;

/*
 * ADAL - A set of Android libraries to help speed up Android development.
 *
 * Copyright (c) 2017 ADAL
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.massivedisaster.adal.network;





/**
 * Base request API callbacks.
 *
 * @param <T> the type of the data in the requests.
 */
public abstract class APIRequestCallback<T extends APIErrorListener> implements Callback<T> {

    private final Context mContext;

    /**
     * Constructcs {@link APIRequestCallback}
     *
     * @param context the context.
     */
    public APIRequestCallback(Context context) {
        mContext = context;
    }

    /**
     * @param t response
     */
    public abstract void onSuccess(T t);

    /**
     * @param error         the error.
     * @param isServerError is a server error.
     */
    public abstract void onError(APIError error, boolean isServerError);

    /**
     * Called when response ends.
     *
     * @param call     the call.
     * @param response the response.
     */
    @Override
    public void onResponse(Call<T> call, Response<T> response) {

        if (call.isCanceled()) {
            return;
        }

        if (response == null) {
            processError(new APIError(mContext.getString(R.string.error_network_general)), true);
            return;
        }

        if (response.errorBody() != null) {
            try {
                Converter<ResponseBody, T> errorConverter = getRetrofitConverter(call);

                T error = errorConverter.convert(response.errorBody());

                if (error != null) {
                    processError(new APIError(error.getErrorCode(), error.getError()), true);
                    return;
                }
            } catch (IllegalAccessException e) {
                LogUtils.logErrorException(APIRequestCallback.class, e);