Skip to content

Commit

Permalink
🆕 binarywang#1866 【小程序】 提审素材上传接口添加一个请求执行器
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyh22 committed Nov 14, 2020
1 parent a2074a4 commit fdd1c63
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.chanjar.weixin.common.bean.result;

import java.io.Serializable;

import lombok.Data;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;

/**
* 小程序 提审素材上传接口
*
* @author yangyh22
* @since 2020/11/14
*/
@Data
public class WxAuditMediaUploadResult implements Serializable {

private static final long serialVersionUID = 1L;

private String type;
private String mediaid;

public static WxAuditMediaUploadResult fromJson(String json) {
return WxGsonBuilder.create().fromJson(json, WxAuditMediaUploadResult.class);
}

@Override
public String toString() {
return WxGsonBuilder.create().toJson(this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package me.chanjar.weixin.common.util.http;

import java.io.File;
import java.io.IOException;

import me.chanjar.weixin.common.bean.result.WxAuditMediaUploadResult;
import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.apache.ApacheAuditMediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.jodd.JoddHttpAuditMediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.okhttp.OkHttpAuditMediaUploadRequestExecutor;

/**
* 小程序 提审素材上传接口
* 上传媒体文件请求执行器.
* 请求的参数是File, 返回的结果是String
*
* @author yangyh22
* @since 2020/11/14
*/
public abstract class AuditMediaUploadRequestExecutor<H, P> implements RequestExecutor<WxAuditMediaUploadResult, File> {

protected RequestHttp<H, P> requestHttp;

public AuditMediaUploadRequestExecutor(RequestHttp requestHttp) {
this.requestHttp = requestHttp;
}

@Override
public void execute(String uri, File data, ResponseHandler<WxAuditMediaUploadResult> handler, WxType wxType) throws WxErrorException, IOException {
handler.handle(this.execute(uri, data, wxType));
}

public static RequestExecutor<WxAuditMediaUploadResult, File> create(RequestHttp requestHttp) {
switch (requestHttp.getRequestType()) {
case APACHE_HTTP:
return new ApacheAuditMediaUploadRequestExecutor(requestHttp);
case JODD_HTTP:
return new JoddHttpAuditMediaUploadRequestExecutor(requestHttp);
case OK_HTTP:
return new OkHttpAuditMediaUploadRequestExecutor(requestHttp);
default:
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package me.chanjar.weixin.common.util.http.apache;

import java.io.File;
import java.io.IOException;

import me.chanjar.weixin.common.bean.result.WxAuditMediaUploadResult;
import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.AuditMediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;

/**
* @author yangyh22
* @since 2020/11/14
*/
public class ApacheAuditMediaUploadRequestExecutor extends AuditMediaUploadRequestExecutor<CloseableHttpClient, HttpHost> {

public ApacheAuditMediaUploadRequestExecutor(RequestHttp requestHttp) {
super(requestHttp);
}

@Override
public WxAuditMediaUploadResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (requestHttp.getRequestHttpProxy() != null) {
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
httpPost.setConfig(config);
}
if (file != null) {
HttpEntity entity = MultipartEntityBuilder
.create()
.addBinaryBody("media", file)
.setMode(HttpMultipartMode.RFC6532)
.build();
httpPost.setEntity(entity);
}
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return WxAuditMediaUploadResult.fromJson(responseContent);
} finally {
httpPost.releaseConnection();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package me.chanjar.weixin.common.util.http.jodd;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.bean.result.WxAuditMediaUploadResult;
import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.AuditMediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;

/**
* @author yangyh22
* @since 2020/11/14
*/
public class JoddHttpAuditMediaUploadRequestExecutor extends AuditMediaUploadRequestExecutor<HttpConnectionProvider, ProxyInfo> {

public JoddHttpAuditMediaUploadRequestExecutor(RequestHttp requestHttp) {
super(requestHttp);
}

@Override
public WxAuditMediaUploadResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (requestHttp.getRequestHttpProxy() != null) {
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
}
request.withConnectionProvider(requestHttp.getRequestHttpClient());
request.form("media", file);
HttpResponse response = request.send();
response.charset(StandardCharsets.UTF_8.name());

String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return WxAuditMediaUploadResult.fromJson(responseContent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package me.chanjar.weixin.common.util.http.okhttp;

import java.io.File;
import java.io.IOException;

import me.chanjar.weixin.common.bean.result.WxAuditMediaUploadResult;
import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.AuditMediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
* @author yangyh22
* @since 2020/11/14
*/
public class OkHttpAuditMediaUploadRequestExecutor extends AuditMediaUploadRequestExecutor<OkHttpClient, OkHttpProxyInfo> {

public OkHttpAuditMediaUploadRequestExecutor(RequestHttp requestHttp) {
super(requestHttp);
}

@Override
public WxAuditMediaUploadResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException {

RequestBody body = new MultipartBody.Builder()
.setType(MediaType.parse("multipart/form-data"))
.addFormDataPart("media",
file.getName(),
RequestBody.create(MediaType.parse("application/octet-stream"), file))
.build();
Request request = new Request.Builder().url(uri).post(body).build();

Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
String responseContent = response.body().string();
WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return WxAuditMediaUploadResult.fromJson(responseContent);
}

}

0 comments on commit fdd1c63

Please sign in to comment.