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 0000000000000000000000000000000000000000..27c96e06f94d1b106b2ba9e244eba7ef07b75cfb --- /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); + } + +}