From 6def7431ed1c101cf12f3154aaf1ebddaca47c22 Mon Sep 17 00:00:00 2001 From: Hyq0719 <2418011730@qq.com> Date: Thu, 10 Feb 2022 14:53:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E3=80=90common=E3=80=91fix:=20=E5=A4=84?= =?UTF-8?q?=E7=90=86multipart=E8=AF=B7=E6=B1=82=E5=8F=82=E6=95=B0=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E5=88=B7=E6=96=B0token=E5=88=A4=E7=A9=BA;=20?= =?UTF-8?q?=E3=80=90=E5=B7=A8=E9=87=8F=E5=BC=95=E6=93=8E=E3=80=91fix:=20DM?= =?UTF-8?q?P=E4=BA=BA=E7=BE=A4=E3=80=81=E7=B4=A0=E6=9D=90=E7=AE=A1?= =?UTF-8?q?=E7=90=86api;=20=E3=80=90vivo=E3=80=91fix:=20=E4=BA=BA=E7=BE=A4?= =?UTF-8?q?=E7=AE=A1=E7=90=86api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/hyq0719/mktapi/common/ApiClient.java | 4 ++ .../com/hyq0719/mktapi/common/ApiRequest.java | 9 +--- .../executor/http/ApacheHttpHandler.java | 37 +++++++------ .../common/executor/http/HttpHandler.java | 25 +++++++++ .../executor/http/OkhttpHttpHandler.java | 53 +++++++++++++------ .../executor/parameter/RequestParam.java | 48 +++++++++++++++-- .../oceanengine/api/DmpAudienceApi.java | 9 ++-- .../mktapi/oceanengine/api/MaterialApi.java | 37 ++++++------- .../hyq0719/mktapi/vivo/api/DmpTagApi.java | 7 ++- 9 files changed, 154 insertions(+), 75 deletions(-) diff --git a/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/ApiClient.java b/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/ApiClient.java index fc8d0fa..ae872d2 100644 --- a/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/ApiClient.java +++ b/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/ApiClient.java @@ -88,6 +88,10 @@ public String getToken(String accountId) { * @param tokenKey */ public String refreshSingleToken(String tokenKey) { + //tokenKey判空 + if (StringUtils.isEmpty(tokenKey)) { + return ""; + } IToken iToken = externalTokenService.refreshToken(tokenKey); iTokenLocalCache.put(tokenKey, iToken); return iToken.getToken(); diff --git a/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/ApiRequest.java b/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/ApiRequest.java index 0c6fad0..6fe658c 100644 --- a/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/ApiRequest.java +++ b/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/ApiRequest.java @@ -76,16 +76,13 @@ public RequestParam constructParameters(T t, String token) { List localVarCollectionQueryParams = new ArrayList<>(); setRequestParam(localVarQueryParams, localVarCollectionQueryParams, t); - Map localVarMultipartTextMap = new HashMap<>(); - Map localVarMultipartFileMap = new HashMap<>(); - setMultipartParam(localVarMultipartTextMap, localVarMultipartFileMap, t); - Map localVarHeaderParams = new HashMap<>(); final String localVarAccept = selectHeaderAccept(getRequestAccept()); if (localVarAccept != null) { localVarHeaderParams.put("Accept", localVarAccept); } Map localVarFormParams = new HashMap<>(); + setFormParam(localVarFormParams, t); final String localVarContentType = selectHeaderContentType(getRequestContentTypes()); localVarHeaderParams.put("Content-Type", localVarContentType); @@ -103,8 +100,6 @@ public RequestParam constructParameters(T t, String token) { .collectionQueryParams(localVarCollectionQueryParams) .headerParams(localVarHeaderParams) .formParams(localVarFormParams) - .multipartTextMap(localVarMultipartTextMap) - .multipartFileMap(localVarMultipartFileMap) .authNames(getLocalVarAuthNames()) .accessToken(localVarToken) .build(); @@ -123,7 +118,7 @@ public String[] getRequestAccept() { public void setRequestParam(List localVarQueryParams, List localVarCollectionQueryParams, T t) { } - public void setMultipartParam(Map multipartTextMap, Map multipartFileMap, T t) { + public void setFormParam(Map formParamMap, T t) { } public void paramValidate(T t) throws ApiException { diff --git a/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/executor/http/ApacheHttpHandler.java b/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/executor/http/ApacheHttpHandler.java index e7e7ef7..bd83234 100644 --- a/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/executor/http/ApacheHttpHandler.java +++ b/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/executor/http/ApacheHttpHandler.java @@ -6,10 +6,7 @@ import com.hyq0719.mktapi.common.exception.ApiException; import com.hyq0719.mktapi.common.executor.parameter.RequestParam; import lombok.extern.slf4j.Slf4j; -import org.apache.http.Consts; -import org.apache.http.Header; -import org.apache.http.HttpResponse; -import org.apache.http.HttpVersion; +import org.apache.http.*; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; @@ -84,21 +81,7 @@ public void setPostEntity(HttpPost httpPost, RequestParam param) { contentType = "application/json"; } if ("multipart/form-data".equals(contentType)) { - MultipartEntityBuilder builder = MultipartEntityBuilder.create(); - Map fileMap = param.getMultipartFileMap(); - if (fileMap != null) { - for (Entry entry : fileMap.entrySet()) { - builder.addPart(entry.getKey(), new FileBody(entry.getValue())); - } - } - Map textMap = param.getMultipartTextMap(); - if (textMap != null) { - for (Entry entry : textMap.entrySet()) { - builder.addTextBody(entry.getKey(), String.valueOf(entry.getValue()), - ContentType.TEXT_PLAIN.withCharset(Consts.UTF_8)); - } - } - httpPost.setEntity(builder.build()); + httpPost.setEntity(buildRequestEntityMultipart(param.getFormParams())); } else { String postEntity = parameterToString(param.getPostBody()); if (postEntity != null) { @@ -109,6 +92,22 @@ public void setPostEntity(HttpPost httpPost, RequestParam param) { } } + public HttpEntity buildRequestEntityMultipart(Map formParams) { + MultipartEntityBuilder mpBuilder = MultipartEntityBuilder.create(); + if (formParams != null) { + for (Entry entry : formParams.entrySet()) { + if (entry.getValue() instanceof File) { + File file = (File) entry.getValue(); + mpBuilder.addPart(entry.getKey(), new FileBody(file)); + } else { + mpBuilder.addTextBody(entry.getKey(), String.valueOf(entry.getValue()), + ContentType.TEXT_PLAIN.withCharset(Consts.UTF_8)); + } + } + } + return mpBuilder.build(); + } + public T handleResponse(HttpResponse response, Type returnType) throws ApiException { int code = response.getStatusLine().getStatusCode(); if (code >= 200 && code < 300) { diff --git a/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/executor/http/HttpHandler.java b/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/executor/http/HttpHandler.java index cc06a8b..07c8273 100644 --- a/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/executor/http/HttpHandler.java +++ b/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/executor/http/HttpHandler.java @@ -7,11 +7,36 @@ import java.lang.reflect.Type; +/** + * 网络请求处理器 + * + * @author hyq0719 + */ public interface HttpHandler { + /** + * 执行网络请求 + * + * @param param 请求参数 + * @param returnType 请求返回Type + * @param 请求返回类型 + * @return ApiResponse + * @throws ApiException + */ ApiResponse execute(RequestParam param, Type returnType) throws ApiException; + /** + * 构造请求url + * + * @param param 请求参数 + * @return 请求url + */ String buildUrl(RequestParam param); + /** + * 获取JSON序列化工具 + * + * @return JSON序列化工具 + */ JSON getJSON(); } diff --git a/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/executor/http/OkhttpHttpHandler.java b/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/executor/http/OkhttpHttpHandler.java index 0f96946..59c8983 100644 --- a/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/executor/http/OkhttpHttpHandler.java +++ b/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/executor/http/OkhttpHttpHandler.java @@ -23,8 +23,8 @@ public class OkhttpHttpHandler extends BaseHttpHandler { private final OkHttpClient httpsClient; - private Map defaultHeaderMap = new HashMap<>(); - private String tempFolderPath = null; + private final Map defaultHeaderMap = new HashMap<>(); + private final String tempFolderPath = null; public OkhttpHttpHandler(OkHttpClient httpsClient) { this.httpsClient = httpsClient; @@ -52,8 +52,6 @@ public Call buildCall(RequestParam param) public Request buildRequest(RequestParam param) throws ApiException { final String url = buildUrl(param); final String method = param.getMethod(); - final Request.Builder reqBuilder = new Request.Builder().url(url); - processHeaderParams(param.getHeaderParams(), reqBuilder); String contentType = param.getHeaderParams().get("Content-Type"); // ensuring a default content type @@ -66,10 +64,9 @@ public Request buildRequest(RequestParam param) throws ApiException { if (!HttpMethod.permitsRequestBody(param.getMethod())) { reqBody = null; } else if ("application/x-www-form-urlencoded".equals(contentType)) { - reqBody = serialize(param.getPostBody(), contentType); + reqBody = buildRequestBodyFormEncoding(param.getFormParams()); } else if ("multipart/form-data".equals(contentType)) { - MultipartBody multipartBody = multiSerialize(param.getMultipartTextMap(), param.getMultipartFileMap()); - return reqBuilder.method("POST", multipartBody).build(); + reqBody = buildRequestBodyMultipart(param.getFormParams()); } else if (body == null) { if ("DELETE".equals(method)) { // allow calling DELETE without sending a request body @@ -81,20 +78,44 @@ public Request buildRequest(RequestParam param) throws ApiException { } else { reqBody = serialize(body, contentType); } - + Request.Builder reqBuilder = new Request.Builder().url(url); + processHeaderParams(param.getHeaderParams(), reqBuilder); return reqBuilder.method(method, reqBody).build(); } - public MultipartBody multiSerialize(Map textMap, Map fileMap) { - Builder builder = new Builder().setType(MultipartBody.FORM); - for (Entry entry : textMap.entrySet()) { - builder.addFormDataPart(entry.getKey(), String.valueOf(entry.getValue())); + /** + * Build a form-encoding request body with the given form parameters. + * + * @param formParams Form parameters in the form of Map + * @return RequestBody + */ + public RequestBody buildRequestBodyFormEncoding(Map formParams) { + FormBody.Builder formBuilder = new FormBody.Builder(); + for (Entry param : formParams.entrySet()) { + formBuilder.add(param.getKey(), parameterToString(param.getValue())); } - for (Entry entry : fileMap.entrySet()) { - builder.addFormDataPart(entry.getKey(), entry.getValue().getName(), - RequestBody.create(MediaType.parse("multipart/form-data"), entry.getValue())); + return formBuilder.build(); + } + + /** + * Build a multipart (file uploading) request body with the given form parameters, which could + * contain text fields and file fields. + * + * @param formParams Form parameters in the form of Map + * @return RequestBody + */ + public MultipartBody buildRequestBodyMultipart(Map formParams) { + Builder mpBuilder = new Builder().setType(MultipartBody.FORM); + for (Entry param : formParams.entrySet()) { + if (param.getValue() instanceof File) { + File file = (File) param.getValue(); + mpBuilder.addFormDataPart(param.getKey(), file.getName() + , RequestBody.create(MediaType.parse("multipart/form-data"), file)); + } else { + mpBuilder.addFormDataPart(param.getKey(), String.valueOf(param.getValue())); + } } - return builder.build(); + return mpBuilder.build(); } public RequestBody serialize(Object obj, String contentType) throws ApiException { diff --git a/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/executor/parameter/RequestParam.java b/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/executor/parameter/RequestParam.java index 4e83c42..4a69818 100644 --- a/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/executor/parameter/RequestParam.java +++ b/marketing-api-common/src/main/java/com/hyq0719/mktapi/common/executor/parameter/RequestParam.java @@ -6,30 +6,70 @@ import lombok.Builder; import lombok.Data; -import java.io.File; import java.util.List; import java.util.Map; +/** + * api请求参数 + * + * @author hyq0719 + */ @Data @Builder @AllArgsConstructor public class RequestParam { + /** + * 每个api的唯一路径 + */ private String path; + /** + * 协议:http,https + */ private String scheme; + /** + * host地址 + */ private String host; + /** + * api版本 + */ private String version; - private BaseUrl baseUrl; + /** + * 请求方法:GET,POST等 + */ private String method; + /** + * url中的请求参数 + */ private List queryParams; + /** + * url中的请求集合参数 + */ private List collectionQueryParams; + /** + * 是否使用请求体 + */ private Boolean usePostBody; + /** + * 请求体 + */ private Object postBody; + /** + * 请求头 + */ private Map headerParams; + /** + * 请求表参数 + */ private Map formParams; + /** + * 请求授权参数名 + */ private String[] authNames; + /** + * access token + */ private String accessToken; - private Map multipartTextMap; - private Map multipartFileMap; public RequestParam() { method = RequestConstants.GET; diff --git a/marketing-api-oceanengine/src/main/java/com/hyq0719/mktapi/oceanengine/api/DmpAudienceApi.java b/marketing-api-oceanengine/src/main/java/com/hyq0719/mktapi/oceanengine/api/DmpAudienceApi.java index 9730eab..4b57e80 100644 --- a/marketing-api-oceanengine/src/main/java/com/hyq0719/mktapi/oceanengine/api/DmpAudienceApi.java +++ b/marketing-api-oceanengine/src/main/java/com/hyq0719/mktapi/oceanengine/api/DmpAudienceApi.java @@ -132,19 +132,18 @@ public DmpCustomAudienceDelete dmpCustomAudienceDelete() { public class DmpDataSourceFileUpload extends OceanApiRequest> { @Override - public void setMultipartParam(Map textMap, Map fileMap, - DmpDataSourceFileUploadRequest request) { + public void setFormParam(Map formParamMap, DmpDataSourceFileUploadRequest request) { Long advertiserId = request.getAdvertiserId(); if (advertiserId != null) { - textMap.put(ADVERTISER_ID, advertiserId); + formParamMap.put(ADVERTISER_ID, advertiserId); } String fileSignature = request.getFileSignature(); if (fileSignature != null) { - textMap.put("image_signature", fileSignature); + formParamMap.put("image_signature", fileSignature); } File file = request.getFile(); if (file != null) { - fileMap.put("image_file", file); + formParamMap.put("image_file", file); } } } diff --git a/marketing-api-oceanengine/src/main/java/com/hyq0719/mktapi/oceanengine/api/MaterialApi.java b/marketing-api-oceanengine/src/main/java/com/hyq0719/mktapi/oceanengine/api/MaterialApi.java index fd9b57e..da88150 100644 --- a/marketing-api-oceanengine/src/main/java/com/hyq0719/mktapi/oceanengine/api/MaterialApi.java +++ b/marketing-api-oceanengine/src/main/java/com/hyq0719/mktapi/oceanengine/api/MaterialApi.java @@ -160,27 +160,26 @@ public FileVideoUpdate fileVideoUpdate() { public class FileImageAdvertiser extends OceanApiRequest> { @Override - public void setMultipartParam(Map textMap, Map fileMap, - FileImageAdvertiserRequest request) { + public void setFormParam(Map formParamMap, FileImageAdvertiserRequest request) { Long advertiserId = request.getAdvertiserId(); if (advertiserId != null) { - textMap.put(ADVERTISER_ID, advertiserId); + formParamMap.put(ADVERTISER_ID, advertiserId); } String uploadType = request.getUploadType(); if (uploadType != null) { - textMap.put("upload_type", uploadType); + formParamMap.put("upload_type", uploadType); } String imageSignature = request.getImageSignature(); if (imageSignature != null) { - textMap.put("image_signature", imageSignature); + formParamMap.put("image_signature", imageSignature); } String imageUrl = request.getImageUrl(); if (imageUrl != null) { - textMap.put("image_url", imageUrl); + formParamMap.put("image_url", imageUrl); } File imageFile = request.getImageFile(); if (imageFile != null) { - fileMap.put("image_file", imageFile); + formParamMap.put("image_file", imageFile); } } } @@ -189,27 +188,26 @@ public void setMultipartParam(Map textMap, Map fil RequestConstants.CONTENT_TYPE_MULTIPART_FORM_DATA}) public class FileImageAd extends OceanApiRequest> { @Override - public void setMultipartParam(Map textMap, Map fileMap, - FileImageAdRequest request) { + public void setFormParam(Map formParamMap, FileImageAdRequest request) { Long advertiserId = request.getAdvertiserId(); if (advertiserId != null) { - textMap.put(ADVERTISER_ID, advertiserId); + formParamMap.put(ADVERTISER_ID, advertiserId); } String uploadType = request.getUploadType(); if (uploadType != null) { - textMap.put("upload_type", uploadType); + formParamMap.put("upload_type", uploadType); } String imageSignature = request.getImageSignature(); if (imageSignature != null) { - textMap.put("image_signature", imageSignature); + formParamMap.put("image_signature", imageSignature); } String imageUrl = request.getImageUrl(); if (imageUrl != null) { - textMap.put("image_url", imageUrl); + formParamMap.put("image_url", imageUrl); } File imageFile = request.getImageFile(); if (imageFile != null) { - fileMap.put("image_file", imageFile); + formParamMap.put("image_file", imageFile); } } } @@ -218,23 +216,22 @@ public void setMultipartParam(Map textMap, Map fil RequestConstants.CONTENT_TYPE_MULTIPART_FORM_DATA}) public class FileVideoAd extends OceanApiRequest> { @Override - public void setMultipartParam(Map textMap, Map fileMap, - FileVideoAdRequest request) { + public void setFormParam(Map formParamMap, FileVideoAdRequest request) { Long advertiserId = request.getAdvertiserId(); if (advertiserId != null) { - textMap.put(ADVERTISER_ID, advertiserId); + formParamMap.put(ADVERTISER_ID, advertiserId); } String filename = request.getFilename(); if (filename != null) { - textMap.put("filename", filename); + formParamMap.put("filename", filename); } String videoSignature = request.getVideoSignature(); if (videoSignature != null) { - textMap.put("video_signature", videoSignature); + formParamMap.put("video_signature", videoSignature); } File videoFile = request.getVideoFile(); if (videoFile != null) { - fileMap.put("video_file", videoFile); + formParamMap.put("video_file", videoFile); } } } diff --git a/marketing-api-vivo/src/main/java/com/hyq0719/mktapi/vivo/api/DmpTagApi.java b/marketing-api-vivo/src/main/java/com/hyq0719/mktapi/vivo/api/DmpTagApi.java index 2206b0d..8daf176 100644 --- a/marketing-api-vivo/src/main/java/com/hyq0719/mktapi/vivo/api/DmpTagApi.java +++ b/marketing-api-vivo/src/main/java/com/hyq0719/mktapi/vivo/api/DmpTagApi.java @@ -85,15 +85,14 @@ public void setRequestParam(List localVarQueryParams, List localVarC } @Override - public void setMultipartParam(Map textMap, Map fileMap, - AdDmpTagUploadRequest request) { + public void setFormParam(Map formParamMap, AdDmpTagUploadRequest request) { String md5 = request.getMd5(); if (md5 != null) { - textMap.put("md5", md5); + formParamMap.put("md5", md5); } File file = request.getFile(); if (file != null) { - fileMap.put("file", file); + formParamMap.put("file", file); } } } From 03f7f915a64557fcf501461d03d23ebd280603a2 Mon Sep 17 00:00:00 2001 From: Hyq0719 <2418011730@qq.com> Date: Thu, 10 Feb 2022 14:56:20 +0800 Subject: [PATCH 2/2] init root maven pom --- README.md | 2 +- pom.xml | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 105 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b694ce6..3c0db47 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Marketing API Java SDKs 旨在对国内主流的Marketing API进行封装,帮 ```xml - com.github.hyq0719 + io.github.hyq0719 (不同模块参考下文) 1.0.0-SNAPSHOT diff --git a/pom.xml b/pom.xml index dc13f40..6c7d06d 100644 --- a/pom.xml +++ b/pom.xml @@ -121,6 +121,8 @@ 1.8 ${java.version} ${java.version} + UTF-8 + UTF-8 1.0.0-SNAPSHOT 1.8.0 3.11.0 @@ -133,12 +135,110 @@ + + ossrh + https://s01.oss.sonatype.org/content/repositories/snapshots + + + ossrh + https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + release + + + + org.apache.maven.plugins + maven-source-plugin + 2.2.1 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-source-plugin + 2.2.1 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.9.1 + + + + -Xdoclint:none + + + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.6 + + + sign-artifacts + verify + + sign + + + + + + + + + - - - - + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.7 + true + + ossrh + https://s01.oss.sonatype.org/ + true + + + + + org.apache.maven.plugins + maven-release-plugin + 2.5.3 + + true + false + release + deploy + + + \ No newline at end of file