package com.gs.xky.service;
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
import com.gs.xky.config.ApiResponse;
|
import com.gs.xky.config.DingTalkParam;
|
import com.gs.xky.config.DingTalkResponse;
|
import com.gs.xky.config.XkyCommonParam;
|
import okhttp3.*;
|
import org.springframework.stereotype.Service;
|
|
import java.io.IOException;
|
import java.text.SimpleDateFormat;
|
import java.util.concurrent.TimeUnit;
|
|
@Service
|
public class ApiService {
|
private final OkHttpClient client;
|
private final ObjectMapper objectMapper;
|
|
|
public ApiService() {
|
this.client = new OkHttpClient.Builder().connectTimeout(90, TimeUnit.SECONDS) // Set connection timeout
|
.readTimeout(90, TimeUnit.SECONDS) // Set read timeout
|
.build();
|
this.objectMapper = new ObjectMapper();
|
|
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
|
|
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
}
|
|
public <T> ApiResponse<T> sendListRequest(XkyCommonParam requestBody, Class<T> responseType, String url) throws IOException {
|
// 设置请求体的媒体类型为 application/json
|
MediaType mediaType = MediaType.parse("application/json");
|
|
// 将 ApiCommonParam 对象转换为 JSON 字符串
|
String jsonBody = objectMapper.writeValueAsString(requestBody);
|
|
// 创建请求体
|
RequestBody body = RequestBody.create(mediaType, jsonBody);
|
|
Request request = new Request.Builder()
|
.url(url)
|
.method("POST", body)
|
.addHeader("Content-Type", "application/json")
|
.addHeader("Accept", "*/*")
|
.addHeader("Connection", "keep-alive")
|
.build();
|
|
try (Response response = client.newCall(request).execute()) {
|
if (response.isSuccessful()) {
|
return objectMapper.readValue(response.body().string(), objectMapper.getTypeFactory().constructParametricType(ApiResponse.class, responseType));
|
} else {
|
return handleErrorResponse(response);
|
}
|
}
|
}
|
|
private <T> ApiResponse<T> handleErrorResponse(Response response) throws IOException {
|
ApiResponse<T> errorResponse = new ApiResponse<>();
|
errorResponse.setResult(response.code());
|
// errorResponse.setError(response.message());
|
// Optionally set other fields like list or total
|
return errorResponse;
|
}
|
|
public <T> DingTalkResponse<T> sendListRequest(DingTalkParam requestBody, Class<T> responseType, String url) throws IOException {
|
|
// 设置请求体的媒体类型为 application/json
|
MediaType mediaType = MediaType.parse("application/json");
|
|
// 将 ApiCommonParam 对象转换为 JSON 字符串
|
String jsonBody = objectMapper.writeValueAsString(requestBody);
|
|
// 创建请求体
|
RequestBody body = RequestBody.create(mediaType, jsonBody);
|
|
Request request = new Request.Builder()
|
.url(url)
|
.method("POST", body)
|
.addHeader("Content-Type", "application/json")
|
.addHeader("Accept", "*/*")
|
.addHeader("Connection", "keep-alive")
|
.build();
|
|
try (Response response = client.newCall(request).execute()) {
|
if (response.isSuccessful()) {
|
return objectMapper.readValue(response.body().string(), objectMapper.getTypeFactory().constructParametricType(DingTalkResponse.class, responseType));
|
} else {
|
return handleErrorResponse1(response);
|
}
|
}
|
}
|
|
private <T> DingTalkResponse<T> handleErrorResponse1(Response response) throws IOException {
|
DingTalkResponse<T> errorResponse = new DingTalkResponse<>();
|
errorResponse.setIsSuccess(String.valueOf(response.code()));
|
// errorResponse.setError(response.message());
|
// Optionally set other fields like list or total
|
return errorResponse;
|
}
|
}
|