package top.octopusyan.http.request;

import okhttp3.Request;
import okhttp3.Response;
import top.octopusyan.http.EasyUtils;
import top.octopusyan.http.api.RequestApi;

import java.lang.reflect.Type;

public interface IRequestHandler {

    /**
     * 请求开始
     *
     * @param request       请求对象
     * @return              返回新的请求对象
     */
    default <Param, Result> Request requestStart(RequestApi<Param, Result> api, Request request) {
        return request;
    }

    /**
     * 请求成功时回调
     *
     * @param response      响应对象
     * @param type          解析类型
     * @return              返回结果
     *
     * @throws Exception    如果抛出则回调失败
     */
    <Param, Result> Result requestSucceed(RequestApi<Param, Result> api, Response response, Type type) throws Exception;

    /**
     * 请求失败
     *
     * @param e             错误对象
     * @return              错误对象
     */
    <Param, Result> Exception requestFail(RequestApi<Param, Result> api, Exception e);

    /**
     * 解析泛型
     */
    default Type getType(Object object) {
        return EasyUtils.getGenericType(object);
    }
}