Skip to content

Commit

Permalink
🐛 #1220 修复公众号永久素材相关的部分okhttp实现有问题的接口
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Sep 22, 2019
1 parent df1aa5c commit 779f1d0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package me.chanjar.weixin.mp.util.requestexecuter.material;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableMap;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.ResponseHandler;
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
* Created by ecoolper on 2017/5/5.
* .
*
* @author ecoolper
* @date 2017/5/5
*/
public class MaterialDeleteOkhttpRequestExecutor extends MaterialDeleteRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
Expand All @@ -28,25 +28,25 @@ public MaterialDeleteOkhttpRequestExecutor(RequestHttp requestHttp) {
}

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

@Override
public Boolean execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
logger.debug("MaterialDeleteOkhttpRequestExecutor is running");
//得到httpClient
OkHttpClient client = requestHttp.getRequestHttpClient();

RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build();
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),
WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)));
Request request = new Request.Builder().url(uri).post(requestBody).build();
Response response = client.newCall(request).execute();
Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
String responseContent = response.body().string();
WxError error = WxError.fromJson(responseContent, WxType.MP);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return true;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ public MaterialNewsInfoOkhttpRequestExecutor(RequestHttp requestHttp) {

@Override
public WxMpMaterialNews execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
OkHttpClient client = requestHttp.getRequestHttpClient();

RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),
WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)));
Request request = new Request.Builder().url(uri).post(requestBody).build();

Response response = client.newCall(request).execute();
Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
String responseContent = response.body().string();
log.debug("响应原始数据:{}", responseContent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public WxMpMaterialUploadResult execute(String uri, WxMpMaterial material, WxTyp
throw new FileNotFoundException();
}

//得到httpClient

OkHttpClient client = requestHttp.getRequestHttpClient();

MultipartBody.Builder bodyBuilder = new MultipartBody.Builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package me.chanjar.weixin.mp.util.requestexecuter.material;

import com.google.common.collect.ImmutableMap;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult;
import okhttp3.*;
import org.slf4j.Logger;
Expand All @@ -25,12 +27,11 @@ public MaterialVideoInfoOkhttpRequestExecutor(RequestHttp requestHttp) {
@Override
public WxMpMaterialVideoInfoResult execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
logger.debug("MaterialVideoInfoOkhttpRequestExecutor is running");
//得到httpClient
OkHttpClient client = requestHttp.getRequestHttpClient();

RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build();
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),
WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)));
Request request = new Request.Builder().url(uri).post(requestBody).build();
Response response = client.newCall(request).execute();
Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
String responseContent = response.body().string();
WxError error = WxError.fromJson(responseContent, WxType.MP);
if (error.getErrorCode() != 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package me.chanjar.weixin.mp.util.requestexecuter.material;

import com.google.common.collect.ImmutableMap;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import okhttp3.*;
import okio.BufferedSink;
import okio.Okio;
Expand All @@ -27,14 +29,17 @@ public MaterialVoiceAndImageDownloadOkhttpRequestExecutor(RequestHttp requestHtt
public InputStream execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
logger.debug("MaterialVoiceAndImageDownloadOkhttpRequestExecutor is running");
OkHttpClient client = requestHttp.getRequestHttpClient();
RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build();

RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),
WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)));
Request request = new Request.Builder().url(uri).get().post(requestBody).build();
Response response = client.newCall(request).execute();
String contentTypeHeader = response.header("Content-Type");
if ("text/plain".equals(contentTypeHeader)) {
String responseContent = response.body().string();
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
}

try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); BufferedSink sink = Okio.buffer(Okio.sink(outputStream))) {
sink.writeAll(response.body().source());
return new ByteArrayInputStream(outputStream.toByteArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
import me.chanjar.weixin.mp.api.test.ApiTestModule;
import me.chanjar.weixin.mp.api.test.TestConstants;
import me.chanjar.weixin.mp.bean.material.*;
import org.testng.annotations.*;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.*;

/**
* 素材管理相关接口的测试
Expand All @@ -27,7 +27,7 @@
* @author codepiano
* @author Binary Wang
*/
@Test(groups = "materialAPI")
@Test
@Guice(modules = ApiTestModule.class)
public class WxMpMaterialServiceImplTest {
@Inject
Expand Down Expand Up @@ -175,7 +175,7 @@ public void testDownloadMaterial(String mediaId) throws WxErrorException, IOExce
}
}

@Test(dependsOnMethods = {"testAddNews","testUploadMaterial"})
@Test(dependsOnMethods = {"testAddNews", "testUploadMaterial"})
public void testGetNewsInfo() throws WxErrorException {
WxMpMaterialNews wxMpMaterialNewsSingle = this.wxService
.getMaterialService().materialNewsInfo(this.singleNewsMediaId);
Expand Down Expand Up @@ -243,6 +243,15 @@ public void testMaterialFileList() throws WxErrorException {

@Test(dependsOnMethods = {"testMaterialFileList"}, dataProvider = "allTestMaterial")
public void testDeleteMaterial(String mediaId) throws WxErrorException {
this.delete(mediaId);
}

@Test
public void testDeleteMaterialDirectly() throws WxErrorException {
this.delete("abc");
}

public void delete(String mediaId) throws WxErrorException {
boolean result = this.wxService.getMaterialService().materialDelete(mediaId);
assertTrue(result);
}
Expand Down

0 comments on commit 779f1d0

Please sign in to comment.