DocApiClient.java 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package com.qmth.ops.biz.ai.client;
  2. import com.qmth.boot.core.ai.model.AiConstants;
  3. import com.qmth.boot.core.ai.model.doc.ParseDocTask;
  4. import com.qmth.boot.core.ai.model.doc.ParseDocTaskResult;
  5. import com.qmth.boot.core.exception.StatusException;
  6. import com.qmth.boot.core.rateLimit.service.RateLimiter;
  7. import com.qmth.boot.core.rateLimit.service.impl.MemoryRateLimiter;
  8. import com.qmth.boot.tools.codec.CodecUtils;
  9. import okhttp3.*;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import java.nio.charset.StandardCharsets;
  13. import java.time.Duration;
  14. /**
  15. * 文档解析类接口基础实现
  16. */
  17. public abstract class DocApiClient {
  18. private static final Logger log = LoggerFactory.getLogger(DocApiClient.class);
  19. private final DocApiConfig config;
  20. private final OkHttpClient client;
  21. private RateLimiter queryRateLimiter;
  22. public DocApiClient(DocApiConfig config) {
  23. this.config = config;
  24. OkHttpClient.Builder builder = new OkHttpClient.Builder().connectionPool(new ConnectionPool())
  25. .connectTimeout(Duration.ofSeconds(10)).readTimeout(Duration.ofSeconds(50));
  26. Interceptor interceptor = getInterceptor();
  27. if (interceptor != null) {
  28. builder.addInterceptor(interceptor);
  29. }
  30. this.client = builder.build();
  31. if (config.getQps() > 0) {
  32. this.queryRateLimiter = new MemoryRateLimiter(config.getQps(), 1000);
  33. }
  34. }
  35. public abstract ParseDocTask parseDocTask(byte[] fileData, String fileName) throws Exception;
  36. public abstract ParseDocTaskResult parseDocTaskQuery(String taskId) throws Exception;
  37. protected DocApiConfig getConfig() {
  38. return config;
  39. }
  40. protected OkHttpClient getClient() {
  41. return client;
  42. }
  43. protected RateLimiter getQueryRateLimiter() {
  44. return queryRateLimiter;
  45. }
  46. protected Interceptor getInterceptor() {
  47. return null;
  48. }
  49. public String encodeTaskId(String taskId) {
  50. String str = getConfig().getSupplier() + AiConstants.PARAM_SPLIT + taskId;
  51. return CodecUtils.toBase64(str.getBytes(StandardCharsets.UTF_8));
  52. }
  53. public String[] decodeTaskId(String encodeStr) {
  54. String str = new String(CodecUtils.fromBase64(encodeStr), StandardCharsets.UTF_8);
  55. String[] values = str.split(AiConstants.PARAM_SPLIT);
  56. if (values.length != 3) {
  57. throw new StatusException("taskId值无效!");
  58. }
  59. return values;
  60. }
  61. protected byte[] download(String url) {
  62. Request request = new Request.Builder().url(url).get().build();
  63. try (Response response = this.getClient().newCall(request).execute();) {
  64. ResponseBody respBody = response.body();
  65. if (response.isSuccessful() && respBody != null) {
  66. return respBody.bytes();
  67. }
  68. log.error("获取文件内容失败!responseCode:{}", response.code());
  69. throw new StatusException("获取文件内容失败!");
  70. } catch (Exception e) {
  71. throw new StatusException("获取文件内容失败!", e);
  72. }
  73. }
  74. }