package com.zztrapp.zztrapp.http;

import com.google.gson.Gson;
import com.zztrapp.zztrapp.bean.HttpResult;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Converter;

/**
 * Describe GsonResponseBodyConverter
 * Created by Bby on 2021/10/2
 * Email bby0856@163.com
 */

class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private final Gson gson;
    private final Type type;

    GsonResponseBodyConverter(Gson gson, Type type) {
        this.gson = gson;
        this.type = type;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        String response = value.string();
//        KLog.e("Networkresponse>>" + response);
        //httpResult 只解析result字段
        HttpResult httpResult = gson.fromJson(response, HttpResult.class);

        if (httpResult.getCode() == 200 || httpResult.getCode() == 0) {
//            KLog.e("-------200");
        } else {
//            KLog.e("-------200else");

            throw new ApiException(httpResult.getCode(), httpResult.getMessage());
        }
        return gson.fromJson(response, type);


    }
}
