optimize codes
This commit is contained in:
@@ -5,6 +5,7 @@ import com.mgm.mcrm.common.dtos.HttpResult;
|
||||
import com.mgm.mcrm.common.enums.HttpMethods;
|
||||
import com.mgm.mcrm.common.httpclient.HttpClientExecutor;
|
||||
import com.mgm.mcrm.common.httpclient.HttpRequestOptions;
|
||||
import com.mgm.mcrm.notification.dtos.request.SendNotificationRequest;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
@@ -39,10 +40,13 @@ public class HttpService {
|
||||
@Value("${daylight.url}")
|
||||
private String daylightUrl;
|
||||
|
||||
@Value("${notification_proxy.url}")
|
||||
private String notificationProxyUrl;
|
||||
|
||||
public String sslGet(String url, Header[] headers, Map<String, Object> map) throws Exception {
|
||||
|
||||
// todo 在這裏需要把請求頭的信息插入到數據庫裏
|
||||
log.info("======sslGet======url:{}===========headers:{} =============== map:{}", daylightUrl + url, JSON.toJSONString(headers), JSON.toJSONString(map));
|
||||
log.info("GET method, url={}, headers={}, map={}", daylightUrl + url, JSON.toJSONString(headers), JSON.toJSONString(map));
|
||||
// 使用 HttpConfig 配置 GET 请求(带参数)
|
||||
HttpRequestOptions getConfig = HttpRequestOptions.create()
|
||||
.url(daylightUrl + url)
|
||||
@@ -52,25 +56,7 @@ public class HttpService {
|
||||
.client(httpClient);
|
||||
|
||||
String result = HttpClientExecutor.get(getConfig, headers);
|
||||
log.info("====== GET request result:{}", result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public String sslGetNoLog(String url, Header[] headers, Map<String, Object> map) throws Exception {
|
||||
|
||||
// todo 在這裏需要把請求頭的信息插入到數據庫裏
|
||||
log.info("======sslGet======url:{}===========headers:{} =============== map:{}", daylightUrl + url, JSON.toJSONString(headers), JSON.toJSONString(map));
|
||||
// 使用 HttpConfig 配置 GET 请求(带参数)
|
||||
HttpRequestOptions getConfig = HttpRequestOptions.create()
|
||||
.url(daylightUrl + url)
|
||||
.timeout(requestConfig)
|
||||
.map(map)
|
||||
.encoding("utf-8")
|
||||
.client(httpClient);
|
||||
|
||||
String result = HttpClientExecutor.get(getConfig, headers);
|
||||
log.info("====== GET request result size={}", result.length());
|
||||
log.info("GET request result size={}", result);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -78,7 +64,7 @@ public class HttpService {
|
||||
public HttpResult sslGetHttpResult(String url, Header[] headers, Map<String, Object> map) throws Exception {
|
||||
|
||||
// todo 在這裏需要把請求頭的信息插入到數據庫裏
|
||||
log.info("======sslGet======url:{}===========headers:{} =============== map:{}", daylightUrl + url, JSON.toJSONString(headers), JSON.toJSONString(map));
|
||||
log.info("GET method, url={}, headers={}, map={}", daylightUrl + url, JSON.toJSONString(headers), JSON.toJSONString(map));
|
||||
// 使用 HttpConfig 配置 GET 请求(带参数)
|
||||
HttpRequestOptions getConfig = HttpRequestOptions.create()
|
||||
.url(daylightUrl + url)
|
||||
@@ -88,7 +74,7 @@ public class HttpService {
|
||||
.client(httpClient);
|
||||
|
||||
HttpResult result = HttpClientExecutor.sendAndGetResp(getConfig, headers);
|
||||
log.info("====== GET request result:{}", result.getResult());
|
||||
log.info("GET request result size={}", result.getResult().length());
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -103,7 +89,7 @@ public class HttpService {
|
||||
* @throws Exception 異常
|
||||
*/
|
||||
public String sslPut(String url, Header[] headers, Map<String, Object> map) throws Exception {
|
||||
log.info("======sslPut======url:{}===========headers:{} =============== map:{}", daylightUrl + url, JSON.toJSONString(headers), JSON.toJSONString(map));
|
||||
log.info("PUT method, url={}, headers={}, map={}", daylightUrl + url, JSON.toJSONString(headers), JSON.toJSONString(map));
|
||||
HttpRequestOptions putConfig = HttpRequestOptions.create()
|
||||
.url(daylightUrl + url)
|
||||
.timeout(requestConfig)
|
||||
@@ -112,7 +98,7 @@ public class HttpService {
|
||||
.encoding("utf-8")
|
||||
.client(httpClient);
|
||||
String result = HttpClientExecutor.send(putConfig, headers);
|
||||
log.info("====== PUT request result:{}", result);
|
||||
log.info("PUT request result={}", result);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -127,7 +113,7 @@ public class HttpService {
|
||||
* @throws Exception 異常
|
||||
*/
|
||||
public String sslDelete(String url, Header[] headers, Object requestBody) throws Exception {
|
||||
log.info("======sslDelete======url:{}===========headers:{} =============== map:{}", daylightUrl + url, JSON.toJSONString(headers), JSON.toJSONString(requestBody));
|
||||
log.info("DELETE method, url={}, headers={}, map={}", daylightUrl + url, JSON.toJSONString(headers), JSON.toJSONString(requestBody));
|
||||
HttpRequestOptions deleteConfig = HttpRequestOptions.create()
|
||||
.url(daylightUrl + url)
|
||||
.timeout(requestConfig)
|
||||
@@ -135,7 +121,7 @@ public class HttpService {
|
||||
.method(HttpMethods.DELETE)
|
||||
.client(httpClient);
|
||||
String result = HttpClientExecutor.send(deleteConfig, headers);
|
||||
log.info("====== DELETE request result:{}", result);
|
||||
log.info("DELETE request result={}", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -149,7 +135,7 @@ public class HttpService {
|
||||
* @throws Exception 異常
|
||||
*/
|
||||
public HttpResult sslPost(String url, Header[] headers, Object requestBody) throws Exception {
|
||||
log.info("======sslCreatePost======url:{}===========headers:{}", daylightUrl + url, JSON.toJSONString(headers));
|
||||
log.info("POST method, url={}, headers={}, map={}", daylightUrl + url, JSON.toJSONString(headers), JSON.toJSONString(requestBody));
|
||||
HttpRequestOptions postConfig = HttpRequestOptions.create()
|
||||
.url(daylightUrl + url)
|
||||
.timeout(requestConfig)
|
||||
@@ -158,7 +144,7 @@ public class HttpService {
|
||||
.client(httpClient);
|
||||
HttpResult result = HttpClientExecutor.sendAndPostResp(postConfig, headers);
|
||||
// System.out.println("POST 请求结果:\n" + result);
|
||||
log.info("====== POST request result:{}", result);
|
||||
log.info("POST request result={}", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -172,7 +158,7 @@ public class HttpService {
|
||||
* @throws Exception 異常
|
||||
*/
|
||||
public HttpResult sslPatch(String url, Header[] headers, Object requestBody) throws Exception {
|
||||
log.info("======sslCreatePatch======url:{}===========headers:{}", daylightUrl + url, JSON.toJSONString(headers));
|
||||
log.info("PATCH method, url={}, headers={}, map={}", daylightUrl + url, JSON.toJSONString(headers), JSON.toJSONString(requestBody));
|
||||
HttpRequestOptions patchConfig = HttpRequestOptions.create()
|
||||
.url(daylightUrl + url)
|
||||
.timeout(requestConfig)
|
||||
@@ -181,30 +167,20 @@ public class HttpService {
|
||||
.encoding("utf-8")
|
||||
.client(httpClient);
|
||||
HttpResult result = HttpClientExecutor.sendAndPatchResp(patchConfig, headers);
|
||||
log.info("====== PATCH request result:{}", result);
|
||||
log.info("PATCH request result={}", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用 HttpConfig 配置 Delete 请求
|
||||
*
|
||||
* @param url 請求路徑
|
||||
* @param headers 請求頭
|
||||
* @param map Cancel数据
|
||||
* @return 接口結果的json數據
|
||||
* @throws Exception 異常
|
||||
*/
|
||||
public HttpResult sslCancelPost(String url, Header[] headers, Map<String, Object> map) throws Exception {
|
||||
log.info("======sslCancelPost======url:{}===========headers:{}", daylightUrl + url, JSON.toJSONString(headers));
|
||||
public HttpResult sslSendNotification(String url, Header[] headers, SendNotificationRequest request) throws Exception {
|
||||
log.info("POST method, send notification, url={}, headers={}, map={}", notificationProxyUrl + url, JSON.toJSONString(headers), JSON.toJSONString(request));
|
||||
HttpRequestOptions postConfig = HttpRequestOptions.create()
|
||||
.url(daylightUrl + url)
|
||||
.url(notificationProxyUrl + url)
|
||||
.timeout(requestConfig)
|
||||
.json(JSON.toJSONString(map))
|
||||
.json(JSON.toJSONString(request))
|
||||
.method(HttpMethods.POST)
|
||||
.client(httpClient);
|
||||
HttpResult result = HttpClientExecutor.sendAndPostResp(postConfig, headers);
|
||||
// System.out.println("POST 请求结果:\n" + result);
|
||||
log.info("====== POST request result:{}", result);
|
||||
log.info("POST request result={}", result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -287,4 +287,21 @@ public class DateTimeUtil {
|
||||
}
|
||||
return returnDate;
|
||||
}
|
||||
|
||||
public static String getDateByFormatPlusOneDay(Date dtDateTime, String strFormat) {
|
||||
|
||||
if (dtDateTime == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(dtDateTime);
|
||||
calendar.add(Calendar.DAY_OF_MONTH, 1);
|
||||
|
||||
return new SimpleDateFormat(strFormat).format(calendar.getTime());
|
||||
} catch (Exception err) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user