Skip to content

Commit

Permalink
Merge pull request #5 from Hyq0719/feature/hyq/2022/01/init
Browse files Browse the repository at this point in the history
Feature/hyq/2022/01/init
  • Loading branch information
黄悦麒 authored Feb 10, 2022
2 parents eab885f + 03f7f91 commit 3decaf8
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 80 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Marketing API Java SDKs 旨在对国内主流的Marketing API进行封装,帮

```xml
<dependency>
<groupId>com.github.hyq0719</groupId>
<groupId>io.github.hyq0719</groupId>
<artifactId>(不同模块参考下文)</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,13 @@ public RequestParam constructParameters(T t, String token) {
List<Pair> localVarCollectionQueryParams = new ArrayList<>();
setRequestParam(localVarQueryParams, localVarCollectionQueryParams, t);

Map<String, Object> localVarMultipartTextMap = new HashMap<>();
Map<String, File> localVarMultipartFileMap = new HashMap<>();
setMultipartParam(localVarMultipartTextMap, localVarMultipartFileMap, t);

Map<String, String> localVarHeaderParams = new HashMap<>();
final String localVarAccept = selectHeaderAccept(getRequestAccept());
if (localVarAccept != null) {
localVarHeaderParams.put("Accept", localVarAccept);
}
Map<String, Object> localVarFormParams = new HashMap<>();
setFormParam(localVarFormParams, t);
final String localVarContentType = selectHeaderContentType(getRequestContentTypes());
localVarHeaderParams.put("Content-Type", localVarContentType);

Expand All @@ -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();
Expand All @@ -123,7 +118,7 @@ public String[] getRequestAccept() {
public void setRequestParam(List<Pair> localVarQueryParams, List<Pair> localVarCollectionQueryParams, T t) {
}

public void setMultipartParam(Map<String, Object> multipartTextMap, Map<String, File> multipartFileMap, T t) {
public void setFormParam(Map<String, Object> formParamMap, T t) {
}

public void paramValidate(T t) throws ApiException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, File> fileMap = param.getMultipartFileMap();
if (fileMap != null) {
for (Entry<String, File> entry : fileMap.entrySet()) {
builder.addPart(entry.getKey(), new FileBody(entry.getValue()));
}
}
Map<String, Object> textMap = param.getMultipartTextMap();
if (textMap != null) {
for (Entry<String, Object> 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) {
Expand All @@ -109,6 +92,22 @@ public void setPostEntity(HttpPost httpPost, RequestParam param) {
}
}

public HttpEntity buildRequestEntityMultipart(Map<String, Object> formParams) {
MultipartEntityBuilder mpBuilder = MultipartEntityBuilder.create();
if (formParams != null) {
for (Entry<String, Object> 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> T handleResponse(HttpResponse response, Type returnType) throws ApiException {
int code = response.getStatusLine().getStatusCode();
if (code >= 200 && code < 300) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,36 @@

import java.lang.reflect.Type;

/**
* 网络请求处理器
*
* @author hyq0719
*/
public interface HttpHandler {

/**
* 执行网络请求
*
* @param param 请求参数
* @param returnType 请求返回Type
* @param <T> 请求返回类型
* @return ApiResponse
* @throws ApiException
*/
<T> ApiResponse<T> execute(RequestParam param, Type returnType) throws ApiException;

/**
* 构造请求url
*
* @param param 请求参数
* @return 请求url
*/
String buildUrl(RequestParam param);

/**
* 获取JSON序列化工具
*
* @return JSON序列化工具
*/
JSON getJSON();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
public class OkhttpHttpHandler extends BaseHttpHandler {

private final OkHttpClient httpsClient;
private Map<String, String> defaultHeaderMap = new HashMap<>();
private String tempFolderPath = null;
private final Map<String, String> defaultHeaderMap = new HashMap<>();
private final String tempFolderPath = null;

public OkhttpHttpHandler(OkHttpClient httpsClient) {
this.httpsClient = httpsClient;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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<String, Object> textMap, Map<String, File> fileMap) {
Builder builder = new Builder().setType(MultipartBody.FORM);
for (Entry<String, Object> 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<String, Object> formParams) {
FormBody.Builder formBuilder = new FormBody.Builder();
for (Entry<String, Object> param : formParams.entrySet()) {
formBuilder.add(param.getKey(), parameterToString(param.getValue()));
}
for (Entry<String, File> 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<String, Object> formParams) {
Builder mpBuilder = new Builder().setType(MultipartBody.FORM);
for (Entry<String, Object> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pair> queryParams;
/**
* url中的请求集合参数
*/
private List<Pair> collectionQueryParams;
/**
* 是否使用请求体
*/
private Boolean usePostBody;
/**
* 请求体
*/
private Object postBody;
/**
* 请求头
*/
private Map<String, String> headerParams;
/**
* 请求表参数
*/
private Map<String, Object> formParams;
/**
* 请求授权参数名
*/
private String[] authNames;
/**
* access token
*/
private String accessToken;
private Map<String, Object> multipartTextMap;
private Map<String, File> multipartFileMap;

public RequestParam() {
method = RequestConstants.GET;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,18 @@ public DmpCustomAudienceDelete dmpCustomAudienceDelete() {
public class DmpDataSourceFileUpload extends OceanApiRequest<DmpDataSourceFileUploadRequest,
OceanResponse<DmpDataSourceFileUploadResponseData>> {
@Override
public void setMultipartParam(Map<String, Object> textMap, Map<String, File> fileMap,
DmpDataSourceFileUploadRequest request) {
public void setFormParam(Map<String, Object> 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);
}
}
}
Expand Down
Loading

0 comments on commit 3decaf8

Please sign in to comment.