package com.ys.bdtp.adm.app.converter

import okhttp3.ResponseBody
import retrofit2.Converter
import retrofit2.Retrofit
import java.lang.reflect.Type

/**
 * 空字符串转换器
 */
class NullOrEmptyConverter : Converter.Factory() {
    override fun responseBodyConverter(
        type: Type,
        annotations: Array<out Annotation>,
        retrofit: Retrofit
    ): Converter<ResponseBody, *> {
        val delegate: Converter<ResponseBody, Any> =
            retrofit.nextResponseBodyConverter(this, type, annotations)
        return Converter<ResponseBody, Any?> { value ->
            val length = value.contentLength().toInt()
            if (length == 0) {
                return@Converter ""
            }
            delegate.convert(value)
        }
    }
}