| @@ -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()); | |||
| @@ -28,25 +28,25 @@ public class MaterialDeleteOkhttpRequestExecutor extends MaterialDeleteRequestEx | |||
| } | |||
| @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; | |||
| } | |||
| } | |||
| @@ -28,13 +28,11 @@ public class MaterialNewsInfoOkhttpRequestExecutor extends MaterialNewsInfoReque | |||
| @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); | |||
| @@ -38,8 +38,6 @@ public class MaterialUploadOkhttpRequestExecutor extends MaterialUploadRequestEx | |||
| throw new FileNotFoundException(); | |||
| } | |||
| //得到httpClient | |||
| OkHttpClient client = requestHttp.getRequestHttpClient(); | |||
| MultipartBody.Builder bodyBuilder = new MultipartBody.Builder() | |||
| @@ -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; | |||
| @@ -25,12 +27,11 @@ public class MaterialVideoInfoOkhttpRequestExecutor extends MaterialVideoInfoReq | |||
| @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) { | |||
| @@ -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; | |||
| @@ -27,7 +29,9 @@ public class MaterialVoiceAndImageDownloadOkhttpRequestExecutor extends Material | |||
| 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"); | |||
| @@ -35,6 +39,7 @@ public class MaterialVoiceAndImageDownloadOkhttpRequestExecutor extends Material | |||
| 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()); | |||
| @@ -9,16 +9,16 @@ import me.chanjar.weixin.mp.api.WxMpService; | |||
| 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.*; | |||
| /** | |||
| * 素材管理相关接口的测试 | |||
| @@ -27,7 +27,7 @@ import static org.testng.Assert.assertTrue; | |||
| * @author codepiano | |||
| * @author Binary Wang | |||
| */ | |||
| @Test(groups = "materialAPI") | |||
| @Test | |||
| @Guice(modules = ApiTestModule.class) | |||
| public class WxMpMaterialServiceImplTest { | |||
| @Inject | |||
| @@ -175,7 +175,7 @@ public class WxMpMaterialServiceImplTest { | |||
| } | |||
| } | |||
| @Test(dependsOnMethods = {"testAddNews","testUploadMaterial"}) | |||
| @Test(dependsOnMethods = {"testAddNews", "testUploadMaterial"}) | |||
| public void testGetNewsInfo() throws WxErrorException { | |||
| WxMpMaterialNews wxMpMaterialNewsSingle = this.wxService | |||
| .getMaterialService().materialNewsInfo(this.singleNewsMediaId); | |||
| @@ -243,6 +243,15 @@ public class WxMpMaterialServiceImplTest { | |||
| @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); | |||
| } | |||