抽取公共函数式接口并简化部分变量命名

This commit is contained in:
Charles7c 2023-04-20 23:50:00 +08:00
parent a1589da809
commit 97c0213731
3 changed files with 15 additions and 14 deletions

View File

@ -25,7 +25,7 @@ import java.util.concurrent.Executor;
@Slf4j @Slf4j
public class CloopenSmsImpl implements SmsBlend { public class CloopenSmsImpl implements SmsBlend {
private final CloopenRestApi cloopenRestApi; private final CloopenRestApi restApi;
private final CloopenConfig config; private final CloopenConfig config;
@ -37,7 +37,7 @@ public class CloopenSmsImpl implements SmsBlend {
this.config = config; this.config = config;
this.pool = pool; this.pool = pool;
this.delayed = delayed; this.delayed = delayed;
cloopenRestApi = Forest.client(CloopenRestApi.class); restApi = Forest.client(CloopenRestApi.class);
} }
@Override @Override
@ -69,7 +69,7 @@ public class CloopenSmsImpl implements SmsBlend {
paramMap.put("appId", config.getAppId()); paramMap.put("appId", config.getAppId());
paramMap.put("templateId", templateId); paramMap.put("templateId", templateId);
paramMap.put("datas", messages.keySet().stream().map(messages::get).toArray(String[]::new)); paramMap.put("datas", messages.keySet().stream().map(messages::get).toArray(String[]::new));
return helper.request(cloopenRestApi::sendSms, paramMap); return helper.request(restApi::sendSms, paramMap);
} }
@Override @Override

View File

@ -11,6 +11,7 @@ import com.dtflys.forest.config.ForestConfiguration;
import org.dromara.sms4j.api.entity.SmsResponse; import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.cloopen.config.CloopenConfig; import org.dromara.sms4j.cloopen.config.CloopenConfig;
import org.dromara.sms4j.comm.exception.SmsBlendException; import org.dromara.sms4j.comm.exception.SmsBlendException;
import org.dromara.sms4j.comm.utils.RestApiFunction;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
@ -25,10 +26,10 @@ import java.util.Optional;
*/ */
public class CloopenHelper { public class CloopenHelper {
private final CloopenConfig cloopenConfig; private final CloopenConfig config;
public CloopenHelper(CloopenConfig cloopenConfig) { public CloopenHelper(CloopenConfig config) {
this.cloopenConfig = cloopenConfig; this.config = config;
} }
/** /**
@ -39,16 +40,16 @@ public class CloopenHelper {
* @param <R> 响应类型 * @param <R> 响应类型
* @return 响应信息 * @return 响应信息
*/ */
public <R> SmsResponse request(CloopenRestApiFunction<Map<String, Object>, R> restApiFunction, Map<String, Object> paramMap) { public <R> SmsResponse request(RestApiFunction<Map<String, Object>, R> restApiFunction, Map<String, Object> paramMap) {
SmsResponse smsResponse = new SmsResponse(); SmsResponse smsResponse = new SmsResponse();
try { try {
String timestamp = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN); String timestamp = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
// 设置全局变量 // 设置全局变量
ForestConfiguration forestConfiguration = Forest.config(); ForestConfiguration forestConfiguration = Forest.config();
forestConfiguration.setVariableValue("baseUrl", (method) -> cloopenConfig.getBaseUrl()); forestConfiguration.setVariableValue("baseUrl", (method) -> config.getBaseUrl());
forestConfiguration.setVariableValue("accessKeyId", (method) -> cloopenConfig.getAccessKeyId()); forestConfiguration.setVariableValue("accessKeyId", (method) -> config.getAccessKeyId());
forestConfiguration.setVariableValue("sign", this.generateSign(cloopenConfig.getAccessKeyId(), cloopenConfig.getAccessKeySecret(), timestamp)); forestConfiguration.setVariableValue("sign", this.generateSign(config.getAccessKeyId(), config.getAccessKeySecret(), timestamp));
forestConfiguration.setVariableValue("authorization", this.generateAuthorization(cloopenConfig.getAccessKeyId(), timestamp)); forestConfiguration.setVariableValue("authorization", this.generateAuthorization(config.getAccessKeyId(), timestamp));
// 调用请求 // 调用请求
R response = restApiFunction.apply(paramMap); R response = restApiFunction.apply(paramMap);

View File

@ -1,9 +1,9 @@
package org.dromara.sms4j.cloopen.util; package org.dromara.sms4j.comm.utils;
import java.io.Serializable; import java.io.Serializable;
/** /**
* 容联云 REST API 函数式接口 * REST API 函数式接口
* *
* @param <P> 请求参数 * @param <P> 请求参数
* @param <R> 响应 * @param <R> 响应
@ -11,6 +11,6 @@ import java.io.Serializable;
* @since 2023/4/17 20:57 * @since 2023/4/17 20:57
*/ */
@FunctionalInterface @FunctionalInterface
public interface CloopenRestApiFunction<P, R> extends Serializable { public interface RestApiFunction<P, R> extends Serializable {
R apply(P param); R apply(P param);
} }