Skip to content

Commit

Permalink
feat: update client (arextest#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
coryhh authored Apr 3, 2024
1 parent 7c524d3 commit 6c74252
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Response viewRecord(@RequestBody ViewRecordRequestType requestType) {
headers.put("downgrade", Boolean.toString(downgrade));

// original response from arex-storage
ViewRecordResponseType storageRes = httpWebServiceApiClient.post(true, viewRecordUrl,
ViewRecordResponseType storageRes = httpWebServiceApiClient.post(viewRecordUrl,
requestType, ViewRecordResponseType.class, headers);

CaseDetailResponse res = new CaseDetailResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public CountRecordResponseType countRecord(CountRecordRequestType requestType) {
queryCaseCountRequestType.setOperation(requestType.getOperationName());
queryCaseCountRequestType.setEndTime(requestType.getEndTime());
queryCaseCountRequestType.setBeginTime(requestType.getBeginTime());
QueryCaseCountResponseType queryCaseCountResponseType = httpWebServiceApiClient.post(true,
QueryCaseCountResponseType queryCaseCountResponseType = httpWebServiceApiClient.post(
countRecordUrl, queryCaseCountRequestType, QueryCaseCountResponseType.class);
CountRecordResponseType responseType = new CountRecordResponseType();
responseType.setRecordedCaseCount(
Expand All @@ -80,15 +80,15 @@ public ListRecordResponseType listRecord(ListRecordRequestType requestType) {
String operationType = requestType.getOperationType();
pagedRequestType.setCategory(MockCategoryType.createEntryPoint(operationType));

PagedResponseType listResponse = httpWebServiceApiClient.post(true, listRecordUrl,
PagedResponseType listResponse = httpWebServiceApiClient.post(listRecordUrl,
pagedRequestType, PagedResponseType.class);

if (listResponse != null) {
recordItemList.addAll(listResponse.getRecords().stream()
.map(arexMocker -> toRecordItem(arexMocker, operationType)).collect(Collectors.toList()));
}

QueryCaseCountResponseType countResponse = httpWebServiceApiClient.post(true,
QueryCaseCountResponseType countResponse = httpWebServiceApiClient.post(
countRecordUrl, pagedRequestType, QueryCaseCountResponseType.class);

if (countResponse != null) {
Expand All @@ -105,7 +105,7 @@ public AggCountRecordResponseType aggCountRecord(CountRecordRequestType requestT
countOperationCaseRequestType.setAppId(requestType.getAppId());
countOperationCaseRequestType.setEndTime(requestType.getEndTime());
countOperationCaseRequestType.setBeginTime(requestType.getBeginTime());
CountOperationCaseResponseType response = httpWebServiceApiClient.post(true,
CountOperationCaseResponseType response = httpWebServiceApiClient.post(
aggCountRecordUrl, countOperationCaseRequestType,
CountOperationCaseResponseType.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public String stopPlan(String planId) {
}

Map<String, String> stringStringMap = Collections.singletonMap("planId", planId);
String res = httpWebServiceApiClient.get(true, stopPlanUrl, stringStringMap,
String res = httpWebServiceApiClient.get(stopPlanUrl, stringStringMap,
String.class);
if (StringUtils.isEmpty(res)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public boolean sendEmail(String mailBox, String subject, String htmlMsg, int typ
}
try {
SendMailRequest request = new SendMailRequest(mailBox, subject, htmlMsg, CONTENT_TYPE, type);
SendMailResponse response = httpWebServiceApiClient.post(false,
SendMailResponse response = httpWebServiceApiClient.post(
sendEmailUrl, request,
SendMailResponse.class);
return response.getData().getSuccess();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@
*/
@Component
@Slf4j
@SuppressWarnings({"java:S119"})
public final class HttpWebServiceApiClient {

private RestTemplate restTemplate;

private RestTemplate outerRestTemplate;
@Resource
private ObjectMapper objectMapper;
private int connectTimeOut = 10000;
Expand Down Expand Up @@ -77,43 +76,37 @@ private void initRestTemplate() {
// Add interceptors (e.g. logging, metrics, etc
this.restTemplate.setInterceptors(clientHttpRequestInterceptors);
}

// set outer restTemplate
this.outerRestTemplate = new RestTemplate(httpMessageConverterList);
this.outerRestTemplate.setRequestFactory(requestFactory);
}

public <TResponse> TResponse get(boolean inner, String url,
public <TResponse> TResponse get(String url,
Map<String, ?> urlVariables,
Class<TResponse> responseType) {
try {
RestTemplate template = inner ? restTemplate : outerRestTemplate;
return template.getForObject(url, responseType, urlVariables);
return restTemplate.getForObject(url, responseType, urlVariables);
} catch (Exception e) {
LOGGER.error("Failed to get response from url: {}", url, e);
}
return null;
}

public <TResponse> ResponseEntity<TResponse> get(boolean inner, String url,
public <TResponse> ResponseEntity<TResponse> get(String url,
Map<String, ?> urlVariables,
ParameterizedTypeReference<TResponse> responseType) {
try {
RestTemplate template = inner ? restTemplate : outerRestTemplate;
return template.exchange(url, HttpMethod.GET, null, responseType, urlVariables);
return restTemplate.exchange(url, HttpMethod.GET, null, responseType, urlVariables);
} catch (Exception e) {
LOGGER.error("Failed to get response from url: {}", url, e);
}
return null;
}

public <TResponse> TResponse get(boolean inner, String url,
public <TResponse> TResponse get(String url,
Map<String, ?> urlVariables,
MultiValueMap<String, String> headers, Class<TResponse> responseType) {
try {
RestTemplate template = inner ? restTemplate : outerRestTemplate;

HttpEntity<?> request = new HttpEntity<>(headers);
return template.exchange(url, HttpMethod.GET, request, responseType, urlVariables)
return restTemplate.exchange(url, HttpMethod.GET, request, responseType, urlVariables)
.getBody();
} catch (Exception e) {
LOGGER.error("Failed to get response from url: {}", url, e);
Expand All @@ -130,6 +123,9 @@ public <TResponse> TResponse get(String url,
requestFactory.setReadTimeout(readTimeout);
RestTemplate template = new RestTemplate();
template.setRequestFactory(requestFactory);
if (CollectionUtils.isNotEmpty(clientHttpRequestInterceptors)) {
template.setInterceptors(clientHttpRequestInterceptors);
}
HttpEntity<?> request = new HttpEntity<>(headers);
return template.exchange(url, HttpMethod.GET, request, responseType, urlVariables)
.getBody();
Expand All @@ -139,25 +135,23 @@ public <TResponse> TResponse get(String url,
return null;
}

public <TRequest, TResponse> TResponse post(boolean inner, String url,
public <TRequest, TResponse> TResponse post(String url,
TRequest request,
Class<TResponse> responseType) {
try {
RestTemplate template = inner ? restTemplate : outerRestTemplate;
return template.postForObject(url, wrapJsonContentType(request), responseType);
return restTemplate.postForObject(url, wrapJsonContentType(request), responseType);
} catch (Exception e) {
LOGGER.error("Failed to post request to url: {}, request: {}", url, request, e);
}
return null;
}

public <TRequest, TResponse> TResponse post(boolean inner, String url,
public <TRequest, TResponse> TResponse post(String url,
TRequest request,
Class<TResponse> responseType,
Map<String, String> headers) {
try {
RestTemplate template = inner ? restTemplate : outerRestTemplate;
return template.postForObject(url, wrapJsonContentType(request, headers), responseType);
return restTemplate.postForObject(url, wrapJsonContentType(request, headers), responseType);
} catch (Exception e) {
LOGGER.error("Failed to post request to url: {}, request: {}", url, request, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public FSCaseDto getViewRecord(String recordId) {
ObjectNode request = objectMapper.createObjectNode();
request.put(RECORD_ID, recordId);

ViewRecordResponseType response = httpWebServiceApiClient.post(true,
ViewRecordResponseType response = httpWebServiceApiClient.post(
storageServiceUrl + STORAGE_VIEW_RECORD_URL, request.toString(),
ViewRecordResponseType.class);

Expand Down Expand Up @@ -88,7 +88,7 @@ public AREXMocker getPinnedArexEntryPointMocker(String recordId) {
ObjectNode request = objectMapper.createObjectNode();
request.put(RECORD_ID, recordId);
request.put(SOURCE_PROVIDER, PINNED);
ViewRecordResponseType response = httpWebServiceApiClient.post(true,
ViewRecordResponseType response = httpWebServiceApiClient.post(
storageServiceUrl + STORAGE_VIEW_RECORD_URL, request.toString(),
ViewRecordResponseType.class);

Expand All @@ -110,7 +110,7 @@ public String getNewRecordId(String recordId) {
public boolean pinnedCase(String recordId, String newRecordId) {
try {
String url = storageServiceUrl + STORAGE_PIN_CASE_URL + recordId + "/" + newRecordId + "/";
CopyResponseType response = httpWebServiceApiClient.get(true, url,
CopyResponseType response = httpWebServiceApiClient.get(url,
Collections.emptyMap(),
CopyResponseType.class);
if (response == null || response.getCopied() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public String planFinishedAction(String appId, String planId, Integer status) {
PlanStatistic planStatistic = queryPlanStatisticsResponseType.getPlanStatisticList().get(0);
try {
CallbackInformRequestType requestType = buildRequest(planStatistic);
httpWebServiceApiClient.post(false, systemConfig.getCallbackUrl(),
httpWebServiceApiClient.post(systemConfig.getCallbackUrl(),
requestType, Object.class);
} catch (Throwable e) {
LOGGER.error("callback inform failed, planId:{}", planId, e);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@

<properties>
<!-- Maven CI Friendly Versions https://maven.apache.org/maven-ci-friendly.html -->
<revision>0.6.4.1</revision>
<revision>0.6.4.2</revision>
<arex-common.version>0.1.24</arex-common.version>
<arex-extension-interface.version>0.0.1</arex-extension-interface.version>
<arex-storage-config.version>1.1.19</arex-storage-config.version>
Expand Down

0 comments on commit 6c74252

Please sign in to comment.