package com.hk.NumericalCollection.service;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.hk.NumericalCollection.config.DataAcquisitionConfiguration;
|
import com.hk.NumericalCollection.dto.ApiDataResult;
|
import com.hk.NumericalCollection.dto.ApiRequestBody;
|
import com.hk.NumericalCollection.dto.ApiResponse;
|
import okhttp3.*;
|
import org.springframework.stereotype.Service;
|
|
import java.io.IOException;
|
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();
|
}
|
|
public ApiResponse<?> getSig(String publicKey, String appId) throws IOException {
|
MediaType mediaType = MediaType.parse("text/plain");
|
RequestBody body = RequestBody.create(mediaType, "{\"publicKey\":\"" + publicKey + "\"}");
|
|
Request request = new Request.Builder()
|
.url(String.format(DataAcquisitionConfiguration.ASK_SIGN_URL, appId))
|
.post(body)
|
.addHeader("Content-Type", "text/plain")
|
.addHeader("Accept", "*/*")
|
.addHeader("Connection", "keep-alive")
|
.build();
|
|
try (Response response = client.newCall(request).execute()) {
|
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
|
return objectMapper.readValue(response.body().string(), ApiResponse.class);
|
}
|
}
|
|
public <T> ApiResponse<T> sendListRequest(ApiRequestBody requestBody, Class<T> responseType) throws IOException {
|
MediaType mediaType = MediaType.parse("application/json");
|
String jsonBody = objectMapper.writeValueAsString(requestBody);
|
RequestBody body = RequestBody.create(mediaType, jsonBody);
|
|
Request request = new Request.Builder()
|
.url(String.format(DataAcquisitionConfiguration.ASK_DATA_URL, requestBody.getAppId()))
|
.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.setCode(String.valueOf(response.code()));
|
errorResponse.setError(response.message());
|
// Optionally set other fields like list or total
|
return errorResponse;
|
}
|
|
public <T> ApiDataResult<T> sendDataRequest(ApiRequestBody requestBody, Class<T> responseType) throws IOException {
|
MediaType mediaType = MediaType.parse("application/json");
|
String jsonBody = objectMapper.writeValueAsString(requestBody);
|
RequestBody body = RequestBody.create(mediaType, jsonBody);
|
|
Request request = new Request.Builder()
|
.url(String.format(DataAcquisitionConfiguration.ASK_DATA_URL, requestBody.getAppId()))
|
.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(ApiDataResult.class, responseType));
|
} else {
|
return handleErrorDataResponse(response);
|
}
|
}
|
}
|
|
private <T> ApiDataResult<T> handleErrorDataResponse(Response response) throws IOException {
|
ApiDataResult<T> errorResponse = new ApiDataResult<>();
|
errorResponse.setCode(String.valueOf(response.code()));
|
errorResponse.setError(response.message());
|
// Optionally set other fields like list or total
|
return errorResponse;
|
}
|
}
|