diff --git a/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/pom.xml b/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/pom.xml new file mode 100644 index 00000000..30c6bdaf --- /dev/null +++ b/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/pom.xml @@ -0,0 +1,61 @@ + + + + 4.0.0 + + com.alibaba.cloud.ai + spring-ai-alibaba + ${revision} + ../../../pom.xml + + + spring-ai-alibaba-starter-function-calling-kuaidi100 + spring-ai-alibaba-starter-function-calling-kuaidi100 + Sina News tool for Spring AI Alibaba + https://github.com/alibaba/spring-ai-alibaba + + + + + org.springframework.ai + spring-ai-spring-boot-autoconfigure + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + com.fasterxml.jackson.core + jackson-annotations + + + + com.github.kuaidi100-api + sdk + 1.0.15 + + + + + + spring-ai-alibaba-starter-function-calling-kuaidi100 + + diff --git a/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/src/main/java/com/alibaba/cloud/ai/functioncalling/kuaidi100/Kuaidi100AutoConfiguration.java b/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/src/main/java/com/alibaba/cloud/ai/functioncalling/kuaidi100/Kuaidi100AutoConfiguration.java new file mode 100644 index 00000000..07368e02 --- /dev/null +++ b/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/src/main/java/com/alibaba/cloud/ai/functioncalling/kuaidi100/Kuaidi100AutoConfiguration.java @@ -0,0 +1,43 @@ +/* + * Copyright 2024-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.cloud.ai.functioncalling.kuaidi100; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Description; + +/** + * @Author: XiaoYunTao + * @Date: 2024/12/18 + */ +@Configuration +@ConditionalOnClass(Kuaidi100AutoConfiguration.class) +@ConditionalOnProperty(prefix = "spring.ai.alibaba.functioncalling.kuaidi100", name = "enabled", havingValue = "true") +@EnableConfigurationProperties(Kuaidi100Properties.class) +public class Kuaidi100AutoConfiguration { + + @Bean + @ConditionalOnMissingBean + @Description("Query courier tracking information") + public Kuaidi100Service queryTrackFunction(Kuaidi100Properties kuaidi100Properties) { + return new Kuaidi100Service(kuaidi100Properties); + } + +} diff --git a/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/src/main/java/com/alibaba/cloud/ai/functioncalling/kuaidi100/Kuaidi100Exception.java b/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/src/main/java/com/alibaba/cloud/ai/functioncalling/kuaidi100/Kuaidi100Exception.java new file mode 100644 index 00000000..897f2bc6 --- /dev/null +++ b/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/src/main/java/com/alibaba/cloud/ai/functioncalling/kuaidi100/Kuaidi100Exception.java @@ -0,0 +1,28 @@ +/* + * Copyright 2024-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.cloud.ai.functioncalling.kuaidi100; + +/** + * @Author: XiaoYunTao + * @Date: 2024/12/26 + */ +public class Kuaidi100Exception extends RuntimeException { + + public Kuaidi100Exception(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/src/main/java/com/alibaba/cloud/ai/functioncalling/kuaidi100/Kuaidi100Properties.java b/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/src/main/java/com/alibaba/cloud/ai/functioncalling/kuaidi100/Kuaidi100Properties.java new file mode 100644 index 00000000..2b44f4cb --- /dev/null +++ b/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/src/main/java/com/alibaba/cloud/ai/functioncalling/kuaidi100/Kuaidi100Properties.java @@ -0,0 +1,59 @@ +/* + * Copyright 2024-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.cloud.ai.functioncalling.kuaidi100; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * @Author: XiaoYunTao + * @Date: 2024/12/25 + */ +@ConfigurationProperties(prefix = "spring.ai.alibaba.functioncalling.kuaidi100") +public class Kuaidi100Properties { + + /** + * 授权key 获取授权key + */ + private String key; + + /** + * customer + * 获取customer + */ + private String customer; + + public Kuaidi100Properties(String key, String customer) { + this.key = key; + this.customer = customer; + } + + public void setKey(String key) { + this.key = key; + } + + public void setCustomer(String customer) { + this.customer = customer; + } + + public String getKey() { + return key; + } + + public String getCustomer() { + return customer; + } + +} \ No newline at end of file diff --git a/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/src/main/java/com/alibaba/cloud/ai/functioncalling/kuaidi100/Kuaidi100Service.java b/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/src/main/java/com/alibaba/cloud/ai/functioncalling/kuaidi100/Kuaidi100Service.java new file mode 100644 index 00000000..c954fbf8 --- /dev/null +++ b/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/src/main/java/com/alibaba/cloud/ai/functioncalling/kuaidi100/Kuaidi100Service.java @@ -0,0 +1,104 @@ +/* + * Copyright 2024-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.cloud.ai.functioncalling.kuaidi100; + +import com.fasterxml.jackson.annotation.JsonClassDescription; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyDescription; +import com.google.gson.Gson; +import com.kuaidi100.sdk.api.AutoNum; +import com.kuaidi100.sdk.api.QueryTrack; +import com.kuaidi100.sdk.request.AutoNumReq; +import com.kuaidi100.sdk.request.QueryTrackParam; +import com.kuaidi100.sdk.request.QueryTrackReq; +import com.kuaidi100.sdk.response.QueryTrackResp; +import com.kuaidi100.sdk.utils.SignUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.function.Function; + +/** + * @Author: XiaoYunTao + * @Date: 2024/12/25 + */ +public class Kuaidi100Service implements Function { + + private static final Logger logger = LoggerFactory.getLogger(Kuaidi100Service.class); + + private final Gson gson = new Gson(); + + private final AutoNum autoNum = new AutoNum(); + + final Kuaidi100Properties kuaidi100Properties; + + public Kuaidi100Service(Kuaidi100Properties kuaidi100Properties) { + this.kuaidi100Properties = kuaidi100Properties; + } + + @Override + public QueryTrackResp apply(Kuaidi100Service.Request request) { + QueryTrackResp queryTrackResp; + try { + queryTrackResp = queryTrack(request.num()); + logger.debug("queryTrackResp: {}", queryTrackResp); + } + catch (Exception e) { + logger.error("Error occurred while querying track!", e); + throw new Kuaidi100Exception("Error querying track.", e); + } + return queryTrackResp; + } + + private QueryTrackResp queryTrack(String num) throws Exception { + String key = kuaidi100Properties.getKey(); + String customer = kuaidi100Properties.getCustomer(); + + QueryTrackParam queryTrackParam = createQueryTrackParam(num, key); + String param = gson.toJson(queryTrackParam); + + QueryTrackReq queryTrackReq = createQueryTrackReq(customer, param, key); + return new QueryTrack().queryTrack(queryTrackReq); + } + + private QueryTrackParam createQueryTrackParam(String num, String key) throws Exception { + AutoNumReq autoNumReq = new AutoNumReq(); + autoNumReq.setNum(num); + autoNumReq.setKey(key); + String company = autoNum.getFirstComByNum(autoNumReq); + + QueryTrackParam queryTrackParam = new QueryTrackParam(); + queryTrackParam.setCom(company); + queryTrackParam.setNum(num); + return queryTrackParam; + } + + private QueryTrackReq createQueryTrackReq(String customer, String param, String key) { + QueryTrackReq queryTrackReq = new QueryTrackReq(); + queryTrackReq.setParam(param); + queryTrackReq.setCustomer(customer); + queryTrackReq.setSign(SignUtils.querySign(param, key, customer)); + return queryTrackReq; + } + + @JsonInclude(JsonInclude.Include.NON_NULL) + @JsonClassDescription("Methods for real-time courier tracking") + public record Request( + @JsonProperty(required = true, value = "num") @JsonPropertyDescription("tracking number") String num) { + } + +} \ No newline at end of file diff --git a/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..29dd9a1c --- /dev/null +++ b/community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.alibaba.cloud.ai.functioncalling.kuaidi100.Kuaidi100AutoConfiguration \ No newline at end of file diff --git a/pom.xml b/pom.xml index d2791820..4e3606b9 100644 --- a/pom.xml +++ b/pom.xml @@ -52,6 +52,7 @@ community/function-calling/spring-ai-alibaba-starter-function-calling-sinanews community/function-calling/spring-ai-alibaba-starter-function-calling-toutiaonews community/function-calling/spring-ai-alibaba-starter-function-calling-yuque + community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100 community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate community/function-calling/spring-ai-alibaba-starter-function-calling-alitranslate