package com.swago.baseswago.http;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * @date 2021/8/14 14:04
 * description:
 */
class SwagoJsonFactor extends  Converter.Factory{

    private final Gson gson;

    public SwagoJsonFactor(Gson gson) {
        this.gson = gson;
    }

    public static SwagoJsonFactor create(Gson gson) {
        if (gson == null) throw new NullPointerException("gson == null");
        return new SwagoJsonFactor(gson);
    }


    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        return new JsonResponseBodyConverter<>(gson,type,annotations);
    }

    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        return new JsonRequestBodyConverter<>(gson,gson.getAdapter(TypeToken.get(type)));
    }
}
