package com.poyee.utils; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.ObjectUtil; import cn.hutool.json.JSONUtil; import com.poyee.res.Result; import lombok.extern.slf4j.Slf4j; import java.util.function.Function; import java.util.function.Supplier; /** * @author Hr */ @Slf4j public class ApiUtils { public static R httpSuccess(Function> function, T request, R defaultValue, String msg) { Assert.notNull(request, msg); Result response = function.apply(request); if (response != null && response.successFlag()) { return ObjectUtil.defaultIfNull(response.getData(), defaultValue); } else { log.error("{} error,request={}, response={}", msg, JSONUtil.toJsonStr(request), JSONUtil.toJsonStr(response)); assert response != null; throw new RuntimeException(response.getMessage()); } } public static R httpSuccess(Function> function, T request, String msg) { return httpSuccess(function, request, null, msg); } public static R httpSuccessNoParams(Supplier> function, R defaultValue, String msg) { Assert.notNull(function, "Function must not be null"); Result response = function.get(); if (response != null && response.successFlag()) { return ObjectUtil.defaultIfNull(response.getData(), defaultValue); } else { log.error("{} error, response={}", msg, JSONUtil.toJsonStr(response)); throw new RuntimeException(msg); } } }