Search in sources :

Example 1 with MenigaWebException

use of com.meniga.sdk.webservices.MenigaWebException in project mobile-sdk-android by meniga.

the class TaskCompletionSource method createTask.

public <E> Task<E> createTask(Call<E> call) {
    final TaskCompletionSource<E> src = new TaskCompletionSource<>(call);
    final Task<E> task = src.getTask();
    call.enqueue(new Callback<E>() {

        @Override
        public void onResponse(Call<E> call, Response<E> response) {
            if (call.isCanceled()) {
                setCancelled();
            } else if (response.isSuccessful()) {
                src.setResult(response.body());
            } else {
                setError(new MenigaWebException(response.message(), response.code()));
            }
        }

        @Override
        public void onFailure(Call<E> call, Throwable t) {
            if (t == null && !task.isCancelled()) {
                setError(new Exception("[No error message provided]"));
            } else {
                if (call.isCanceled() && !task.isCompleted()) {
                    setCancelled();
                } else if (!task.isCompleted() && t != null) {
                    setError(new MenigaWebException(t.getMessage(), -1));
                }
            }
        }
    });
    return task;
}
Also used : MenigaWebException(com.meniga.sdk.webservices.MenigaWebException) MenigaWebException(com.meniga.sdk.webservices.MenigaWebException)

Example 2 with MenigaWebException

use of com.meniga.sdk.webservices.MenigaWebException in project mobile-sdk-android by meniga.

the class BasicTaskAdapter method adapt.

@Override
public <T> Result<T> adapt(Call<T> call, Callback<T> callBack) {
    if (callBack == null) {
        callBack = new Callback<T>() {

            @Override
            public void onResponse(Call<T> call, Response<T> response) {
            }

            @Override
            public void onFailure(Call<T> call, Throwable t) {
            }
        };
    }
    final Callback<T> finalCall = callBack;
    final TaskCompletionSource<T> tcs = new TaskCompletionSource<>(call);
    call.enqueue(new Callback<T>() {

        @Override
        public void onResponse(Call<T> call, Response<T> response) {
            if (call.isCanceled()) {
                tcs.setCancelled();
            } else if (response.isSuccessful()) {
                tcs.setResult(response.body());
                finalCall.onResponse(call, response);
            } else {
                ErrorHandler.reportAndHandle(new Exception(response.message()));
                String msg = response.message();
                try {
                    msg = response.errorBody().string();
                } catch (Exception ex) {
                // Do not crash
                }
                tcs.setError(new MenigaWebException(msg, response.code()));
            }
        }

        @Override
        public void onFailure(Call<T> call, Throwable error) {
            ErrorHandler.reportAndHandle(error);
            if (error == null && !tcs.getTask().isCancelled()) {
                tcs.setError(new Exception("[No error message provided]"));
            } else {
                if (call.isCanceled() && !tcs.getTask().isCompleted()) {
                    tcs.setCancelled();
                } else if (!tcs.getTask().isCompleted()) {
                    if (error != null) {
                        tcs.setError(new MenigaWebException(error.getMessage(), -1));
                    } else {
                        tcs.setError(new MenigaWebException("[No error message provided]", -1));
                    }
                }
            }
        }
    });
    return new MTask<>(tcs.getTask(), tcs);
}
Also used : TaskCompletionSource(com.meniga.sdk.providers.tasks.TaskCompletionSource) MenigaWebException(com.meniga.sdk.webservices.MenigaWebException) MTask(com.meniga.sdk.helpers.MTask) MenigaWebException(com.meniga.sdk.webservices.MenigaWebException)

Aggregations

MenigaWebException (com.meniga.sdk.webservices.MenigaWebException)2 MTask (com.meniga.sdk.helpers.MTask)1 TaskCompletionSource (com.meniga.sdk.providers.tasks.TaskCompletionSource)1