org.eu.vooo.commons.net.client.XConvertSupplier Maven / Gradle / Ivy
package org.eu.vooo.commons.net.client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import retrofit2.Call;
import org.eu.vooo.commons.lang.result.XResult;
import org.eu.vooo.commons.net.exception.NetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
/**
* @author [email protected]
*/
@FunctionalInterface
public interface XConvertSupplier {
Logger logger = LoggerFactory.getLogger(XConvertSupplier.class);
default T get() {
try {
XResult xResult = this.request().execute().body();
if (xResult.getStatusCode() == 200) {
return xResult.getReturnObj();
}
Type genericSuperclassType = this.getClass().getGenericSuperclass();
Type actualType = ((ParameterizedType) genericSuperclassType).getActualTypeArguments()[0];
return (T) actualType.getClass().newInstance();
} catch (Exception e) {
logger.error("发生异常", e);
throw new NetException("获取数据异常: " + e.getMessage());
}
}
Call> request() throws Exception;
}