Use YDRequestBody in YDRequest for YDJob and CWJob

pull/8040/head
Qiuyi LI 2024-06-27 17:13:34 +02:00
parent 38a832eee4
commit ced711cb03
3 changed files with 19 additions and 25 deletions

View File

@ -1,7 +1,6 @@
package org.jeecg.modules.business.domain.api.yd;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
@ -23,18 +22,16 @@ import java.util.List;
@Slf4j
public class YDRequest {
private final static String URL = "http://oms.ydhex.com/webservice/PublicService.asmx/ServiceInterfaceUTF8";
private String appToken;
private String appKey;
private final String appToken;
private final String appKey;
private final YDRequestBody ydRequestBody;
private static final String SERVICE_METHOD = "gettrack";
private static final RequestConfig REQUEST_CONFIG = RequestConfig.custom().build();
private final List<String> billCodes;
public YDRequest(String appToken, String appKey, List<String> billCodes) {
public YDRequest(String appToken, String appKey, YDRequestBody ydRequestBody) {
this.appToken = appToken;
this.appKey = appKey;
this.billCodes = billCodes;
this.ydRequestBody = ydRequestBody;
}
/**
@ -53,7 +50,7 @@ public class YDRequest {
request.setEntity(new UrlEncodedFormEntity(generateFormData(), "UTF-8"));
return httpClient.execute(request);
} catch (Exception e) {
log.error("Request failed on attempt n°" + attempts);
log.error("Request failed on attempt n°{}", attempts);
}
}
return null;
@ -67,18 +64,11 @@ public class YDRequest {
*/
private List<NameValuePair> generateFormData() {
List<NameValuePair> pairs = new ArrayList<>();
String paramsJson = generateJsonString(billCodes);
pairs.add(new BasicNameValuePair("appToken", appToken));
pairs.add(new BasicNameValuePair("appKey", appKey));
pairs.add(new BasicNameValuePair("serviceMethod", SERVICE_METHOD));
pairs.add(new BasicNameValuePair("paramsJson", paramsJson));
pairs.add(new BasicNameValuePair("serviceMethod", ydRequestBody.getServiceMethod()));
pairs.add(new BasicNameValuePair("paramsJson", ydRequestBody.getParamsJson()));
return pairs;
}
private static String generateJsonString(List<String> billCodes) {
JSONObject param = new JSONObject();
String billCodesWithComas = String.join(",", billCodes);
param.put("tracking_number", billCodesWithComas);
return param.toJSONString();
}
}

View File

@ -9,8 +9,9 @@ import org.apache.http.util.EntityUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.jeecg.modules.business.domain.api.yd.YDParcelTraceRequestBody;
import org.jeecg.modules.business.domain.api.yd.YDParcelTraceResponse;
import org.jeecg.modules.business.domain.api.yd.YDRequest;
import org.jeecg.modules.business.domain.api.yd.YDResponse;
import org.jeecg.modules.business.domain.api.yd.YDTraceData;
import org.jeecg.modules.business.service.IParcelService;
import org.jeecg.modules.business.service.IPlatformOrderService;
@ -42,7 +43,7 @@ public class CWJob implements Job {
private static final Integer DEFAULT_NUMBER_OF_DAYS = 15;
private static final Integer DEFAULT_NUMBER_OF_THREADS = 10;
private static final Integer DEFAULT_MAXIMUM_NUMBER_OF_PARCELS_PER_TRANSACTION = 800;
private static final List<String> DEFAULT_TRANSPORTERS = Arrays.asList("诚稳法邮普货" , "诚稳法邮膏体");
private static final List<String> DEFAULT_TRANSPORTERS = Arrays.asList("诚稳法邮普货", "诚稳法邮膏体");
private final static String APP_TOKEN = "y7j1p5o4obncsdhbk1zgasunb2erpyzvh";
private final static String APP_KEY = "ym27kj0wy5wgx69f58pgd7crm60w07p0l15flj1bacrf5n0e38vqjtrjkkvosd61p";
@ -99,7 +100,8 @@ public class CWJob implements Job {
List<YDTraceData> parcelTraces = new ArrayList<>();
List<YDRequest> ydRequests = new ArrayList<>();
billCodeLists.forEach(billcodeList -> {
YDRequest ydRequest = new YDRequest(APP_TOKEN, APP_KEY, billcodeList);
YDParcelTraceRequestBody ydParcelTraceRequestBody = new YDParcelTraceRequestBody(billcodeList);
YDRequest ydRequest = new YDRequest(APP_TOKEN, APP_KEY, ydParcelTraceRequestBody);
ydRequests.add(ydRequest);
});
ExecutorService executor = Executors.newFixedThreadPool(DEFAULT_NUMBER_OF_THREADS);
@ -110,7 +112,7 @@ public class CWJob implements Job {
try {
// String of the response
String responseString = EntityUtils.toString(entity, "UTF-8");
YDResponse ydResponse = mapper.readValue(responseString, YDResponse.class);
YDParcelTraceResponse ydResponse = mapper.readValue(responseString, YDParcelTraceResponse.class);
parcelTraces.addAll(ydResponse.getTraceDataList());
success = true;
} catch (IOException e) {

View File

@ -9,8 +9,9 @@ import org.apache.http.util.EntityUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.jeecg.modules.business.domain.api.yd.YDParcelTraceRequestBody;
import org.jeecg.modules.business.domain.api.yd.YDParcelTraceResponse;
import org.jeecg.modules.business.domain.api.yd.YDRequest;
import org.jeecg.modules.business.domain.api.yd.YDResponse;
import org.jeecg.modules.business.domain.api.yd.YDTraceData;
import org.jeecg.modules.business.service.IParcelService;
import org.jeecg.modules.business.service.IPlatformOrderService;
@ -99,7 +100,8 @@ public class YDJob implements Job {
List<YDTraceData> parcelTraces = new ArrayList<>();
List<YDRequest> ydRequests = new ArrayList<>();
billCodeLists.forEach(billcodeList -> {
YDRequest ydRequest = new YDRequest(APP_TOKEN, APP_KEY, billcodeList);
YDParcelTraceRequestBody ydParcelTraceRequestBody = new YDParcelTraceRequestBody(billcodeList);
YDRequest ydRequest = new YDRequest(APP_TOKEN, APP_KEY, ydParcelTraceRequestBody);
ydRequests.add(ydRequest);
});
ExecutorService executor = Executors.newFixedThreadPool(DEFAULT_NUMBER_OF_THREADS);
@ -110,7 +112,7 @@ public class YDJob implements Job {
try {
// String of the response
String responseString = EntityUtils.toString(entity, "UTF-8");
YDResponse ydResponse = mapper.readValue(responseString, YDResponse.class);
YDParcelTraceResponse ydResponse = mapper.readValue(responseString, YDParcelTraceResponse.class);
parcelTraces.addAll(ydResponse.getTraceDataList());
success = true;
} catch (IOException e) {