Explorar el Código

增加vectorserver代理转发接口

wangyunpeng hace 2 horas
padre
commit
d672dfb0df

+ 54 - 0
api-module/src/main/java/com/tzld/piaoquan/api/component/VideoVectorProxyService.java

@@ -0,0 +1,54 @@
+package com.tzld.piaoquan.api.component;
+
+import com.tzld.piaoquan.api.common.enums.ExceptionEnum;
+import com.tzld.piaoquan.api.common.exception.CommonException;
+import com.tzld.piaoquan.growth.common.component.HttpPoolClient;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+
+@Slf4j
+@Component
+public class VideoVectorProxyService {
+
+    @Autowired
+    private HttpPoolClient httpPoolClient;
+
+    @Value("${video.vector.api.host:http://api-internal.piaoquantv.com/videoVector}")
+    private String videoVectorApiHost;
+
+    public String get(String path) {
+        String url = buildUrl(path);
+        try {
+            return requireResponse(httpPoolClient.get(url), path);
+        } catch (IOException e) {
+            log.error("VideoVectorProxyService GET failed, path={}", path, e);
+            throw new CommonException(ExceptionEnum.SYSTEM_ERROR, e);
+        }
+    }
+
+    public String post(String path, String requestBody) {
+        String url = buildUrl(path);
+        try {
+            return requireResponse(httpPoolClient.post(url, requestBody), path);
+        } catch (IOException e) {
+            log.error("VideoVectorProxyService POST failed, path={}", path, e);
+            throw new CommonException(ExceptionEnum.SYSTEM_ERROR, e);
+        }
+    }
+
+    private String buildUrl(String path) {
+        return videoVectorApiHost.replaceAll("/+$", "") + "/" + path;
+    }
+
+    private String requireResponse(String response, String path) {
+        if (response == null) {
+            log.error("VideoVectorProxyService upstream returned no response, path={}", path);
+            throw new CommonException(ExceptionEnum.SYSTEM_ERROR);
+        }
+        return response;
+    }
+}

+ 54 - 0
api-module/src/main/java/com/tzld/piaoquan/api/controller/contentplatform/ContentPlatformVideoVectorController.java

@@ -0,0 +1,54 @@
+package com.tzld.piaoquan.api.controller.contentplatform;
+
+import com.tzld.piaoquan.api.annotation.RequireSign;
+import com.tzld.piaoquan.api.component.VideoVectorProxyService;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.CrossOrigin;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("/contentPlatform/videoVector")
+@CrossOrigin(origins = "*")
+public class ContentPlatformVideoVectorController {
+
+    private static final String MATCH_BY_TEXT_PATH = "recallTest/matchByText";
+    private static final String BATCH_BY_TEXT_PATH = "recallTest/batchByText";
+    private static final String ALL_CONFIG_CODES_PATH = "videoSearch/getAllConfigCodes";
+
+    @Autowired
+    private VideoVectorProxyService proxyService;
+
+    @ApiOperation(value = "转发向量素材单路召回请求")
+    @PostMapping(value = "/recallTest/matchByText", produces = MediaType.APPLICATION_JSON_VALUE)
+    @RequireSign
+    public ResponseEntity<String> matchByText(@RequestBody String requestBody) {
+        return jsonResponse(proxyService.post(MATCH_BY_TEXT_PATH, requestBody));
+    }
+
+    @ApiOperation(value = "转发向量素材批量召回请求")
+    @PostMapping(value = "/recallTest/batchByText", produces = MediaType.APPLICATION_JSON_VALUE)
+    @RequireSign
+    public ResponseEntity<String> batchByText(@RequestBody String requestBody) {
+        return jsonResponse(proxyService.post(BATCH_BY_TEXT_PATH, requestBody));
+    }
+
+    @ApiOperation(value = "转发向量配置列表请求")
+    @GetMapping(value = "/videoSearch/getAllConfigCodes", produces = MediaType.APPLICATION_JSON_VALUE)
+    @RequireSign
+    public ResponseEntity<String> getAllConfigCodes() {
+        return jsonResponse(proxyService.get(ALL_CONFIG_CODES_PATH));
+    }
+
+    private ResponseEntity<String> jsonResponse(String body) {
+        return ResponseEntity.ok()
+                .contentType(MediaType.APPLICATION_JSON)
+                .body(body);
+    }
+}