提交 2c40431c 编写于 作者: 小傅哥's avatar 小傅哥

feat:添加 HTTP 请求拦截器

上级 df40b5eb
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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册