All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eu.vooo.commons.net.client.XConvertSupplier Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
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;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy