Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS-1089 sms add seq #581

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 102 additions & 64 deletions src/main/java/com/qiniu/sms/SmsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.qiniu.common.Constants;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.MethodType;
import com.qiniu.http.Response;
import com.qiniu.sms.model.MessageInfo;
import com.qiniu.sms.model.SignatureInfo;
import com.qiniu.sms.model.TemplateInfo;
import com.qiniu.util.Auth;
Expand All @@ -31,6 +31,7 @@ public class SmsManager {
* 该类相关的域名配置,解析配置,HTTP请求超时时间设置等
*/
private Configuration configuration;
private SmsRequestHelper smsRequestHelper;

/**
* 构建一个新的 SmsManager 对象
Expand All @@ -41,6 +42,7 @@ public SmsManager(Auth auth) {
this.auth = auth;
this.configuration = new Configuration();
client = new Client(this.configuration);
smsRequestHelper = new SmsRequestHelper(configuration, client, auth);
}

/**
Expand All @@ -53,14 +55,15 @@ public SmsManager(Auth auth, Configuration cfg) {
this.auth = auth;
this.configuration = cfg.clone();
client = new Client(this.configuration);
smsRequestHelper = new SmsRequestHelper(configuration, client, auth);
}

/**
* 发送短信
*
* @param templateId 模板Id,必填
* @param mobiles 手机号码数组,必填
* @param parameters 参数,必填
* @param parameters 参数,选填
* @return Response
* @throws QiniuException 异常
*/
Expand All @@ -70,16 +73,37 @@ public Response sendMessage(String templateId, String[] mobiles, Map<String, Str
StringMap bodyMap = new StringMap();
bodyMap.put("template_id", templateId);
bodyMap.put("mobiles", mobiles);
bodyMap.put("parameters", parameters);
return post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
bodyMap.putNotNull("parameters", parameters);
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}

/**
* 发送短信
*
* @param signatureId 签名Id,选填
* @param templateId 模板Id,必填
* @param mobiles 手机号码数组,必填
* @param parameters 参数,选填
* @return Response
* @throws QiniuException 异常
*/
public Response sendMessage(String signatureId, String templateId, String[] mobiles, Map<String, String> parameters)
throws QiniuException {
String requestUrl = String.format("%s/v1/message", configuration.smsHost());
StringMap bodyMap = new StringMap();
bodyMap.putNotEmpty("signature_id", signatureId);
bodyMap.put("template_id", templateId);
bodyMap.put("mobiles", mobiles);
bodyMap.putNotNull("parameters", parameters);
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}

/**
* 发送单条短信
*
* @param templateId 模板Id,必填
* @param mobile 手机号码,必填
* @param parameters 参数,必填
* @param parameters 参数,选填
* @return Response
* @throws QiniuException 异常
*/
Expand All @@ -89,16 +113,28 @@ public Response sendSingleMessage(String templateId, String mobile, Map<String,
StringMap bodyMap = new StringMap();
bodyMap.put("template_id", templateId);
bodyMap.put("mobile", mobile);
bodyMap.put("parameters", parameters);
return post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
bodyMap.putNotNull("parameters", parameters);
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}

/**
* 发送单条短信
*
* @param messageInfo@return Response
* @throws QiniuException 异常
*/
public Response sendSingleMessage(MessageInfo messageInfo) throws QiniuException {
String requestUrl = String.format("%s/v1/message/single", configuration.smsHost());
StringMap bodyMap = SmsMap.createSingleMessageMap(messageInfo);
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}

/**
* 发送国际短信
*
* @param templateId 模板Id,必填
* @param mobile 手机号码,必填
* @param parameters 参数,必填
* @param parameters 参数,选填
* @return Response
* @throws QiniuException 异常
*/
Expand All @@ -108,8 +144,29 @@ public Response sendOverseaMessage(String templateId, String mobile, Map<String,
StringMap bodyMap = new StringMap();
bodyMap.put("template_id", templateId);
bodyMap.put("mobile", mobile);
bodyMap.putNotNull("parameters", parameters);
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}

/**
* 发送国际短信
*
* @param signatureId 签名Id,选填
* @param templateId 模板Id,必填
* @param mobile 手机号码,必填
* @param parameters 参数,选填
* @return Response
* @throws QiniuException 异常
*/
public Response sendOverseaMessage(String signatureId, String templateId, String mobile, Map<String, String> parameters)
throws QiniuException {
String requestUrl = String.format("%s/v1/message/oversea", configuration.smsHost());
StringMap bodyMap = new StringMap();
bodyMap.putNotEmpty("signature_id", signatureId);
bodyMap.put("template_id", templateId);
bodyMap.put("mobile", mobile);
bodyMap.put("parameters", parameters);
return post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}

/**
Expand All @@ -123,10 +180,33 @@ public Response sendOverseaMessage(String templateId, String mobile, Map<String,
*/
public Response sendFulltextMessage(String[] mobiles, String content) throws QiniuException {
String requestUrl = String.format("%s/v1/message/fulltext", configuration.smsHost());
StringMap bodyMap = new StringMap();
bodyMap.put("mobiles", mobiles);
bodyMap.put("content", content);
return post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
StringMap bodyMap = SmsMap.createFulltextMessageMap(mobiles, content);
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}

/**
* 发送全文本短信(不需要传模版 ID)
*
* @param mobiles 手机号码数组,必填
* @param content 短信内容,必须是已经审核通过的签名和模版,必填。例如:【七牛云】您的验证码是 287712,5分钟内有效
* @param templateType 短信类型,支持的值:notification(通知类)、verification(验证码类)、marketing(营销推广类)、voice(语音类),必填
* @return Response
* @throws QiniuException 异常
*/
public Response sendFulltextMessage(String[] mobiles, String content, String templateType) throws QiniuException {
String requestUrl = String.format("%s/v1/message/fulltext", configuration.smsHost());
StringMap bodyMap = SmsMap.createFulltextMessageMap(mobiles, content, templateType);
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}

public Response describeResource(String resourceType, String auditStatus, int page, int pageSize) throws QiniuException {
String requestUrl = String.format("%s/v1/%s", configuration.smsHost(), resourceType);
StringMap queryMap = new StringMap();
queryMap.putNotEmpty("audit_status", auditStatus);
queryMap.putWhen("page", page, page > 0);
queryMap.putWhen("page_size", pageSize, pageSize > 0);
requestUrl = UrlUtils.composeUrlWithQueries(requestUrl, queryMap);
return smsRequestHelper.get(requestUrl);
}

/**
Expand All @@ -140,13 +220,7 @@ public Response sendFulltextMessage(String[] mobiles, String content) throws Qin
* @throws QiniuException 异常
*/
public Response describeSignature(String auditStatus, int page, int pageSize) throws QiniuException {
String requestUrl = String.format("%s/v1/signature", configuration.smsHost());
StringMap queryMap = new StringMap();
queryMap.putNotEmpty("audit_status", auditStatus);
queryMap.putWhen("page", page, page > 0);
queryMap.putWhen("page_size", pageSize, pageSize > 0);
requestUrl = UrlUtils.composeUrlWithQueries(requestUrl, queryMap);
return get(requestUrl);
return describeResource("signature", auditStatus, page, pageSize);
}

public SignatureInfo describeSignatureItems(String auditStatus, int page, int pageSize) throws QiniuException {
Expand Down Expand Up @@ -177,7 +251,7 @@ public Response createSignature(String signature, String source, String[] pics)
bodyMap.put("signature", signature);
bodyMap.put("source", source);
bodyMap.putWhen("pics", pics, pics.length > 0);
return post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}

/**
Expand All @@ -192,7 +266,7 @@ public Response modifySignature(String signatureId, String signature) throws Qin
String reqUrl = String.format("%s/v1/signature/%s", configuration.smsHost(), signatureId);
StringMap bodyMap = new StringMap();
bodyMap.put("signature", signature);
return put(reqUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
return smsRequestHelper.put(reqUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}

/**
Expand All @@ -204,7 +278,7 @@ public Response modifySignature(String signatureId, String signature) throws Qin
*/
public Response deleteSignature(String signatureId) throws QiniuException {
String requestUrl = String.format("%s/v1/signature/%s", configuration.smsHost(), signatureId);
return delete(requestUrl);
return smsRequestHelper.delete(requestUrl);
}

/**
Expand All @@ -218,13 +292,7 @@ public Response deleteSignature(String signatureId) throws QiniuException {
* @throws QiniuException 异常
*/
public Response describeTemplate(String auditStatus, int page, int pageSize) throws QiniuException {
String requestUrl = String.format("%s/v1/template", configuration.smsHost());
StringMap queryMap = new StringMap();
queryMap.putNotEmpty("audit_status", auditStatus);
queryMap.putWhen("page", page, page > 0);
queryMap.putWhen("page_size", pageSize, pageSize > 0);
requestUrl = UrlUtils.composeUrlWithQueries(requestUrl, queryMap);
return get(requestUrl);
return describeResource("template", auditStatus, page, pageSize);
}

public TemplateInfo describeTemplateItems(String auditStatus, int page, int pageSize) throws QiniuException {
Expand All @@ -242,7 +310,7 @@ public TemplateInfo describeTemplateItems(String auditStatus, int page, int page
*/
public Response describeTemplate(String templateId) throws QiniuException {
String requestUrl = String.format("%s/v1/template/%s", configuration.smsHost(), templateId);
return get(requestUrl);
return smsRequestHelper.get(requestUrl);
}

/**
Expand Down Expand Up @@ -279,7 +347,7 @@ public Response createTemplate(String name, String template, String type, String
bodyMap.put("type", type);
bodyMap.put("description", description);
bodyMap.put("signature_id", signatureId);
return post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}

/**
Expand All @@ -301,7 +369,7 @@ public Response modifyTemplate(String templateId, String name, String template,
bodyMap.put("template", template);
bodyMap.put("description", description);
bodyMap.put("signature_id", signatureId);
return put(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
return smsRequestHelper.put(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}

/**
Expand All @@ -313,36 +381,6 @@ public Response modifyTemplate(String templateId, String name, String template,
*/
public Response deleteTemplate(String templateId) throws QiniuException {
String requestUrl = String.format("%s/v1/template/%s", configuration.smsHost(), templateId);
return delete(requestUrl);
}

/*
* 相关请求的方法列表
* */
private Response get(String url) throws QiniuException {
StringMap headers = composeHeader(url, MethodType.GET.toString(), null, Client.FormMime);
return client.get(url, headers);
return smsRequestHelper.delete(requestUrl);
}

private Response post(String url, byte[] body) throws QiniuException {
StringMap headers = composeHeader(url, MethodType.POST.toString(), body, Client.JsonMime);
return client.post(url, body, headers, Client.JsonMime);
}

private Response put(String url, byte[] body) throws QiniuException {
StringMap headers = composeHeader(url, MethodType.PUT.toString(), body, Client.JsonMime);
return client.put(url, body, headers, Client.JsonMime);
}

private Response delete(String url) throws QiniuException {
StringMap headers = composeHeader(url, MethodType.DELETE.toString(), null, Client.DefaultMime);
return client.delete(url, headers);
}

private StringMap composeHeader(String url, String method, byte[] body, String contentType) {
StringMap headers = auth.authorizationV2(url, method, body, contentType);
headers.put("Content-Type", contentType);
return headers;
}

}
38 changes: 38 additions & 0 deletions src/main/java/com/qiniu/sms/SmsMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.qiniu.sms;

import com.qiniu.sms.model.MessageInfo;
import com.qiniu.util.StringMap;

/**
* @author hugo
* @date 2023-06-07 21:37
*/
public class SmsMap {
private SmsMap() {
}

public static StringMap createSingleMessageMap(MessageInfo messageInfo) {
StringMap bodyMap = new StringMap();
bodyMap.putNotEmpty("signature_id", messageInfo.getSignatureId());
bodyMap.put("template_id", messageInfo.getTemplateId());
bodyMap.put("mobile", messageInfo.getMobile());
bodyMap.putNotNull("parameters", messageInfo.getParameters());
bodyMap.putNotEmpty("seq", messageInfo.getSeq());
return bodyMap;
}

public static StringMap createFulltextMessageMap(String[] mobiles, String content) {
StringMap bodyMap = new StringMap();
bodyMap.put("mobiles", mobiles);
bodyMap.put("content", content);
return bodyMap;
}

public static StringMap createFulltextMessageMap(String[] mobiles, String content, String templateType) {
StringMap bodyMap = new StringMap();
bodyMap.put("mobiles", mobiles);
bodyMap.put("content", content);
bodyMap.put("template_type", templateType);
return bodyMap;
}
}
50 changes: 50 additions & 0 deletions src/main/java/com/qiniu/sms/SmsRequestHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.qiniu.sms;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.MethodType;
import com.qiniu.http.Response;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

/**
* @author hugo
* @date 2023-06-07 21:25
*/
public class SmsRequestHelper {
private Configuration configuration;
private Client client;
private Auth auth;

public SmsRequestHelper(Configuration configuration, Client client, Auth auth) {
this.configuration = configuration;
this.client = client;
this.auth = auth;
}

public Response get(String url) throws QiniuException {
StringMap headers = composeHeader(url, MethodType.GET.toString(), null, Client.FormMime);
return client.get(url, headers);
}

public Response post(String url, byte[] body) throws QiniuException {
StringMap headers = composeHeader(url, MethodType.POST.toString(), body, Client.JsonMime);
return client.post(url, body, headers, Client.JsonMime);
}

public Response put(String url, byte[] body) throws QiniuException {
StringMap headers = composeHeader(url, MethodType.PUT.toString(), body, Client.JsonMime);
return client.put(url, body, headers, Client.JsonMime);
}

public Response delete(String url) throws QiniuException {
StringMap headers = composeHeader(url, MethodType.DELETE.toString(), null, Client.DefaultMime);
return client.delete(url, headers);
}

private StringMap composeHeader(String url, String method, byte[] body, String contentType) {
StringMap headers = auth.authorizationV2(url, method, body, contentType);
headers.put("Content-Type", contentType);
return headers;
}
}
Loading