|
|
@@ -0,0 +1,54 @@
|
|
|
+package com.zhongshu.iot.server.core.service.openApi;
|
|
|
+
|
|
|
+import com.github.microservice.models.openAPI.OpenApiInfo;
|
|
|
+import com.github.microservice.models.openAPI.RefreshApiParam;
|
|
|
+import com.github.microservice.net.ResultContent;
|
|
|
+import com.github.microservice.utils.OpenAPIScan;
|
|
|
+import com.zhongshu.gateway.client.type.ApiType;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 注册平台所有的API
|
|
|
+ *
|
|
|
+ * @author TRX
|
|
|
+ * @date 2024/9/29
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class OpenAPIRegisterService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
+ @Value("${spring.application.name}")
|
|
|
+ private String serviceId;
|
|
|
+
|
|
|
+ public ResultContent initRegisterPAIS() {
|
|
|
+ String str = restTemplate.getForObject("http://" + serviceId + "/v3/api-docs", String.class);
|
|
|
+ List<OpenApiInfo> openAPIS = OpenAPIScan.scanAPI(str, "物联网");
|
|
|
+ RefreshApiParam param = new RefreshApiParam();
|
|
|
+ param.setServerName(serviceId.toLowerCase());
|
|
|
+ param.setPredicateArgs("/openAPI/**");
|
|
|
+ param.setOpenApiInfo(openAPIS);
|
|
|
+ param.setApiType(ApiType.IOT.name());
|
|
|
+
|
|
|
+ ResultContent content = restTemplate.postForObject("http://openapiserver/openapi/manager/api/refresh", param, ResultContent.class);
|
|
|
+ if (content == null) {
|
|
|
+ content = ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+ if (content.isFailed()) {
|
|
|
+ log.info("openApi注册错误: {}", content.getMsg());
|
|
|
+ } else {
|
|
|
+ log.info("openApi注册成功");
|
|
|
+ }
|
|
|
+ str = null;
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|