package com.yutou.okhttp;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public abstract class HttpCallback<T> implements Callback<HttpBody<T>> {

    public abstract void onResponse(int code, String status, T response, String rawResponse);

    public abstract void onFailure(Throwable throwable);

    @Override
    public void onResponse(Call<HttpBody<T>> call, Response<HttpBody<T>> response) {
        if (response.body() != null) {
            onResponse(
                    response.body().getRetcode(),
                    response.body().getStatus(),
                    response.body().getData(),
                    response.body().getSrc()
            );
        } else {
            onFailure(new NullPointerException("response body is null"));
        }
    }

    @Override
    public void onFailure(Call<HttpBody<T>> call, Throwable throwable) {
        onFailure(throwable);
    }
}
