From 2c40431c9fe322ad9413a605c12bf36b78f43792 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=82=85=E5=93=A5?= <184172133@qq.com> Date: Tue, 10 Oct 2023 22:03:41 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E6=B7=BB=E5=8A=A0=20HTTP=20?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E6=8B=A6=E6=88=AA=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interceptor/OpenAiHTTPInterceptor.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/java/cn/bugstack/chatglm/interceptor/OpenAiHTTPInterceptor.java diff --git a/src/main/java/cn/bugstack/chatglm/interceptor/OpenAiHTTPInterceptor.java b/src/main/java/cn/bugstack/chatglm/interceptor/OpenAiHTTPInterceptor.java new file mode 100644 index 0000000..27c96e0 --- /dev/null +++ b/src/main/java/cn/bugstack/chatglm/interceptor/OpenAiHTTPInterceptor.java @@ -0,0 +1,48 @@ +package cn.bugstack.chatglm.interceptor; + +import cn.bugstack.chatglm.session.Configuration; +import cn.bugstack.chatglm.utils.BearerTokenUtils; +import okhttp3.Interceptor; +import okhttp3.Request; +import okhttp3.Response; +import org.jetbrains.annotations.NotNull; + +import java.io.IOException; + +/** + * @author 小傅哥,微信:fustack + * @description 接口拦截器 + * @github https://github.com/fuzhengwei + * @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! + */ +public class OpenAiHTTPInterceptor implements Interceptor { + + /** + * 智普Ai,Jwt加密Token + */ + private final Configuration configuration; + + public OpenAiHTTPInterceptor(Configuration configuration) { + this.configuration = configuration; + } + + @Override + public @NotNull Response intercept(Chain chain) throws IOException { + // 1. 获取原始 Request + Request original = chain.request(); + + // 2. 构建请求 + Request request = original.newBuilder() + .url(original.url()) + .header("Authorization", "Bearer " + BearerTokenUtils.getToken(configuration.getApiKey(), configuration.getApiSecret())) + .header("Content-Type", Configuration.JSON_CONTENT_TYPE) + .header("User-Agent", Configuration.DEFAULT_USER_AGENT) + .header("Accept", Configuration.SSE_CONTENT_TYPE) + .method(original.method(), original.body()) + .build(); + + // 3. 返回执行结果 + return chain.proceed(request); + } + +} -- GitLab