抽取数据统计相关接口请求URL到其接口类中,并重构重复代码 #195

This commit is contained in:
Binary Wang 2017-05-29 15:07:23 +08:00
parent a436e62103
commit 931533c13a
2 changed files with 75 additions and 130 deletions

View File

@ -13,6 +13,24 @@ import java.util.List;
* @author binarywang (https://github.com/binarywang)
*/
public interface WxMpDataCubeService {
String GET_USER_SUMMARY = "https://api.weixin.qq.com/datacube/getusersummary";
String GET_USER_CUMULATE = "https://api.weixin.qq.com/datacube/getusercumulate";
String GET_ARTICLE_SUMMARY = "https://api.weixin.qq.com/datacube/getarticlesummary";
String GET_ARTICLE_TOTAL = "https://api.weixin.qq.com/datacube/getarticletotal";
String GET_USER_READ = "https://api.weixin.qq.com/datacube/getuserread";
String GET_USER_READ_HOUR = "https://api.weixin.qq.com/datacube/getuserreadhour";
String GET_USER_SHARE = "https://api.weixin.qq.com/datacube/getusershare";
String GET_USER_SHARE_HOUR = "https://api.weixin.qq.com/datacube/getusersharehour";
String GET_UPSTREAM_MSG = "https://api.weixin.qq.com/datacube/getupstreammsg";
String GET_UPSTREAM_MSG_HOUR = "https://api.weixin.qq.com/datacube/getupstreammsghour";
String GET_UPSTREAM_MSG_WEEK = "https://api.weixin.qq.com/datacube/getupstreammsgweek";
String GET_UPSTREAM_MSG_MONTH = "https://api.weixin.qq.com/datacube/getupstreammsgmonth";
String GET_UPSTREAM_MSG_DIST = "https://api.weixin.qq.com/datacube/getupstreammsgdist";
String GET_UPSTREAM_MSG_DIST_WEEK = "https://api.weixin.qq.com/datacube/getupstreammsgdistweek";
String GET_UPSTREAM_MSG_DIST_MONTH = "https://api.weixin.qq.com/datacube/getupstreammsgdistmonth";
String GET_INTERFACE_SUMMARY = "https://api.weixin.qq.com/datacube/getinterfacesummary";
String GET_INTERFACE_SUMMARY_HOUR = "https://api.weixin.qq.com/datacube/getinterfacesummaryhour";
//*******************用户分析数据接口***********************//
/**

View File

@ -17,7 +17,6 @@ import java.util.List;
* @author binarywang (https://github.com/binarywang)
*/
public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/datacube";
private final Format dateFormat = FastDateFormat.getInstance("yyyy-MM-dd");
@ -29,180 +28,108 @@ public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
@Override
public List<WxDataCubeUserSummary> getUserSummary(Date beginDate, Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getusersummary";
JsonObject param = new JsonObject();
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString());
String responseContent = this.wxMpService.post(GET_USER_SUMMARY, buildParams(beginDate, endDate));
return WxDataCubeUserSummary.fromJson(responseContent);
}
@Override
public List<WxDataCubeUserCumulate> getUserCumulate(Date beginDate, Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getusercumulate";
JsonObject param = new JsonObject();
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString());
String responseContent = this.wxMpService.post(GET_USER_CUMULATE, buildParams(beginDate, endDate));
return WxDataCubeUserCumulate.fromJson(responseContent);
}
@Override
public List<WxDataCubeArticleResult> getArticleSummary(Date beginDate, Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getarticlesummary";
JsonObject param = new JsonObject();
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString());
return WxDataCubeArticleResult.fromJson(responseContent);
return this.getArticleResults(GET_ARTICLE_SUMMARY, beginDate, endDate);
}
@Override
public List<WxDataCubeArticleTotal> getArticleTotal(Date beginDate, Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getarticletotal";
JsonObject param = new JsonObject();
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString());
String responseContent = this.wxMpService.post(GET_ARTICLE_TOTAL, buildParams(beginDate, endDate));
return WxDataCubeArticleTotal.fromJson(responseContent);
}
@Override
public List<WxDataCubeArticleResult> getUserRead(Date beginDate, Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getuserread";
JsonObject param = new JsonObject();
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString());
return WxDataCubeArticleResult.fromJson(responseContent);
return this.getArticleResults(GET_USER_READ, beginDate, endDate);
}
@Override
public List<WxDataCubeArticleResult> getUserReadHour(Date beginDate, Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getuserreadhour";
JsonObject param = new JsonObject();
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString());
return WxDataCubeArticleResult.fromJson(responseContent);
return this.getArticleResults(GET_USER_READ_HOUR, beginDate, endDate);
}
@Override
public List<WxDataCubeArticleResult> getUserShare(Date beginDate, Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getusershare";
JsonObject param = new JsonObject();
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString());
return WxDataCubeArticleResult.fromJson(responseContent);
return this.getArticleResults(GET_USER_SHARE, beginDate, endDate);
}
@Override
public List<WxDataCubeArticleResult> getUserShareHour(Date beginDate, Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getusersharehour";
JsonObject param = new JsonObject();
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString());
return this.getArticleResults(GET_USER_SHARE_HOUR, beginDate, endDate);
}
private List<WxDataCubeArticleResult> getArticleResults(String url, Date beginDate, Date endDate) throws WxErrorException {
String responseContent = this.wxMpService.post(url, buildParams(beginDate, endDate));
return WxDataCubeArticleResult.fromJson(responseContent);
}
@Override
public List<WxDataCubeMsgResult> getUpstreamMsg(Date beginDate, Date endDate)
throws WxErrorException {
String url = API_URL_PREFIX + "/getupstreammsg";
JsonObject param = new JsonObject();
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString());
public List<WxDataCubeMsgResult> getUpstreamMsg(Date beginDate, Date endDate) throws WxErrorException {
return this.getUpstreamMsg(GET_UPSTREAM_MSG, beginDate, endDate);
}
@Override
public List<WxDataCubeMsgResult> getUpstreamMsgHour(Date beginDate, Date endDate) throws WxErrorException {
return this.getUpstreamMsg(GET_UPSTREAM_MSG_HOUR, beginDate, endDate);
}
@Override
public List<WxDataCubeMsgResult> getUpstreamMsgWeek(Date beginDate, Date endDate) throws WxErrorException {
return this.getUpstreamMsg(GET_UPSTREAM_MSG_WEEK, beginDate, endDate);
}
@Override
public List<WxDataCubeMsgResult> getUpstreamMsgMonth(Date beginDate, Date endDate) throws WxErrorException {
return this.getUpstreamMsg(GET_UPSTREAM_MSG_MONTH, beginDate, endDate);
}
@Override
public List<WxDataCubeMsgResult> getUpstreamMsgDist(Date beginDate, Date endDate) throws WxErrorException {
return this.getUpstreamMsg(GET_UPSTREAM_MSG_DIST, beginDate, endDate);
}
@Override
public List<WxDataCubeMsgResult> getUpstreamMsgDistWeek(Date beginDate, Date endDate) throws WxErrorException {
return this.getUpstreamMsg(GET_UPSTREAM_MSG_DIST_WEEK, beginDate, endDate);
}
@Override
public List<WxDataCubeMsgResult> getUpstreamMsgDistMonth(Date beginDate, Date endDate) throws WxErrorException {
return this.getUpstreamMsg(GET_UPSTREAM_MSG_DIST_MONTH, beginDate, endDate);
}
private List<WxDataCubeMsgResult> getUpstreamMsg(String url, Date beginDate, Date endDate) throws WxErrorException {
String responseContent = this.wxMpService.post(url, buildParams(beginDate, endDate));
return WxDataCubeMsgResult.fromJson(responseContent);
}
@Override
public List<WxDataCubeMsgResult> getUpstreamMsgHour(Date beginDate,
Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getupstreammsghour";
JsonObject param = new JsonObject();
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString());
return WxDataCubeMsgResult.fromJson(responseContent);
}
@Override
public List<WxDataCubeMsgResult> getUpstreamMsgWeek(Date beginDate,
Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getupstreammsgweek";
JsonObject param = new JsonObject();
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString());
return WxDataCubeMsgResult.fromJson(responseContent);
}
@Override
public List<WxDataCubeMsgResult> getUpstreamMsgMonth(Date beginDate,
Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getupstreammsgmonth";
JsonObject param = new JsonObject();
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString());
return WxDataCubeMsgResult.fromJson(responseContent);
}
@Override
public List<WxDataCubeMsgResult> getUpstreamMsgDist(Date beginDate,
Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getupstreammsgdist";
JsonObject param = new JsonObject();
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString());
return WxDataCubeMsgResult.fromJson(responseContent);
}
@Override
public List<WxDataCubeMsgResult> getUpstreamMsgDistWeek(Date beginDate,
Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getupstreammsgdistweek";
JsonObject param = new JsonObject();
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString());
return WxDataCubeMsgResult.fromJson(responseContent);
}
@Override
public List<WxDataCubeMsgResult> getUpstreamMsgDistMonth(Date beginDate,
Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getupstreammsgdistmonth";
JsonObject param = new JsonObject();
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString());
return WxDataCubeMsgResult.fromJson(responseContent);
}
@Override
public List<WxDataCubeInterfaceResult> getInterfaceSummary(Date beginDate,
Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getinterfacesummary";
JsonObject param = new JsonObject();
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString());
public List<WxDataCubeInterfaceResult> getInterfaceSummary(Date beginDate, Date endDate) throws WxErrorException {
String responseContent = this.wxMpService.post(GET_INTERFACE_SUMMARY, buildParams(beginDate, endDate));
return WxDataCubeInterfaceResult.fromJson(responseContent);
}
@Override
public List<WxDataCubeInterfaceResult> getInterfaceSummaryHour(Date beginDate,
Date endDate) throws WxErrorException {
String url = API_URL_PREFIX + "/getinterfacesummaryhour";
private String buildParams(Date beginDate, Date endDate) {
JsonObject param = new JsonObject();
param.addProperty("begin_date", this.dateFormat.format(beginDate));
param.addProperty("end_date", this.dateFormat.format(endDate));
String responseContent = this.wxMpService.post(url, param.toString());
return param.toString();
}
@Override
public List<WxDataCubeInterfaceResult> getInterfaceSummaryHour(Date beginDate, Date endDate) throws WxErrorException {
String responseContent = this.wxMpService.post(GET_INTERFACE_SUMMARY_HOUR, buildParams(beginDate, endDate));
return WxDataCubeInterfaceResult.fromJson(responseContent);
}
}