| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- 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 <T, R> R httpSuccess(Function<T, Result<R>> function, T request, R defaultValue, String msg) {
- Assert.notNull(request, msg);
- Result<R> 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 <T, R> R httpSuccess(Function<T, Result<R>> function, T request, String msg) {
- return httpSuccess(function, request, null, msg);
- }
- public static <R> R httpSuccessNoParams(Supplier<Result<R>> function, R defaultValue, String msg) {
- Assert.notNull(function, "Function must not be null");
- Result<R> 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);
- }
- }
- }
|