类 AbstractDifyClient
java.lang.Object
io.github.imfangs.dify.client.impl.AbstractDifyClient
Dify API 客户端抽象基类
提供通用的 HTTP 请求处理方法
-
字段概要
字段 -
构造器概要
构造器构造器说明AbstractDifyClient(String baseUrl, String apiKey) 构造函数AbstractDifyClient(String baseUrl, String apiKey, okhttp3.OkHttpClient httpClient) 构造函数 -
方法概要
修饰符和类型方法说明protected void添加非空字符串参数protected void添加非空参数protected StringbuildUrlWithMultiValueParams(String path, Map<String, Object> params) 构建支持多值参数的URL查询参数 特别处理List类型的参数,生成多个同名参数 例如:tag_ids=[id1,id2] -> ? protected StringbuildUrlWithParams(String path, Map<String, Object> params) 构建URL查询参数protected DifyApiExceptioncreateApiException(int code, String message) 创建API异常protected okhttp3.RequestcreateDeleteRequest(String path, okhttp3.RequestBody body) 创建DELETE请求protected okhttp3.RequestcreateGetRequest(String path) 创建GET请求protected okhttp3.RequestBodycreateJsonRequestBody(Object body) 创建JSON请求体protected okhttp3.RequestcreatePatchRequest(String path, okhttp3.RequestBody body) 创建PATCH请求protected okhttp3.RequestcreatePostRequest(String path, okhttp3.RequestBody body) 创建POST请求protected okhttp3.RequestcreatePutRequest(String path, okhttp3.RequestBody body) 创建PUT请求protected <T> TexecuteDelete(String path, Object body, Class<T> responseClass) 执行DELETE请求protected <T> TexecuteGet(String path, Class<T> responseClass) 执行GET请求protected <T> TexecutePatch(String path, Object body, Class<T> responseClass) 执行Patch请求protected <T> TexecutePost(String path, Object body, Class<T> responseClass) 执行POST请求protected <T> TexecutePut(String path, Object body, Class<T> responseClass) 执行PUT请求protected <T> TexecuteRequest(okhttp3.Request request, Class<T> responseClass) 执行请求并处理响应protected byte[]executeRequestForBytes(okhttp3.Request request) 执行请求并返回字节数组protected <T> ThandleResponse(okhttp3.Response response, Class<T> clazz) 处理HTTP响应
-
字段详细资料
-
JSON
protected static final okhttp3.MediaType JSON -
OCTET_STREAM
protected static final okhttp3.MediaType OCTET_STREAM -
AUDIO
protected static final okhttp3.MediaType AUDIO -
httpClient
protected final okhttp3.OkHttpClient httpClient -
baseUrl
-
apiKey
-
-
构造器详细资料
-
AbstractDifyClient
构造函数- 参数:
baseUrl- API基础URLapiKey- API密钥
-
AbstractDifyClient
构造函数- 参数:
baseUrl- API基础URLapiKey- API密钥httpClient- HTTP客户端
-
-
方法详细资料
-
executeGet
protected <T> T executeGet(String path, Class<T> responseClass) throws IOException, DifyApiException 执行GET请求- 类型参数:
T- 响应类型- 参数:
path- 请求路径responseClass- 响应类型- 返回:
- 响应对象
- 抛出:
IOException- IO异常DifyApiException- API异常
-
executePost
protected <T> T executePost(String path, Object body, Class<T> responseClass) throws IOException, DifyApiException 执行POST请求- 类型参数:
T- 响应类型- 参数:
path- 请求路径body- 请求体responseClass- 响应类型- 返回:
- 响应对象
- 抛出:
IOException- IO异常DifyApiException- API异常
-
executePatch
protected <T> T executePatch(String path, Object body, Class<T> responseClass) throws IOException, DifyApiException 执行Patch请求- 类型参数:
T- 响应类型- 参数:
path- 请求路径body- 请求体responseClass- 响应类型- 返回:
- 响应对象
- 抛出:
IOException- IO异常DifyApiException- API异常
-
executePut
protected <T> T executePut(String path, Object body, Class<T> responseClass) throws IOException, DifyApiException 执行PUT请求- 类型参数:
T- 响应类型- 参数:
path- 请求路径body- 请求体responseClass- 响应类型- 返回:
- 响应对象
- 抛出:
IOException- IO异常DifyApiException- API异常
-
executeDelete
protected <T> T executeDelete(String path, Object body, Class<T> responseClass) throws IOException, DifyApiException 执行DELETE请求- 类型参数:
T- 响应类型- 参数:
path- 请求路径body- 请求体responseClass- 响应类型- 返回:
- 响应对象
- 抛出:
IOException- IO异常DifyApiException- API异常
-
executeRequest
protected <T> T executeRequest(okhttp3.Request request, Class<T> responseClass) throws IOException, DifyApiException 执行请求并处理响应- 类型参数:
T- 响应类型- 参数:
request- 请求对象responseClass- 响应类型- 返回:
- 响应对象
- 抛出:
IOException- IO异常DifyApiException- API异常
-
handleResponse
protected <T> T handleResponse(okhttp3.Response response, Class<T> clazz) throws IOException, DifyApiException 处理HTTP响应- 类型参数:
T- 目标类型- 参数:
response- HTTP响应clazz- 目标类型- 返回:
- 响应对象
- 抛出:
IOException- IO异常DifyApiException- API异常
-
executeRequestForBytes
protected byte[] executeRequestForBytes(okhttp3.Request request) throws IOException, DifyApiException 执行请求并返回字节数组- 参数:
request- 请求对象- 返回:
- 字节数组
- 抛出:
IOException- IO异常DifyApiException- API异常
-
createGetRequest
创建GET请求- 参数:
path- 请求路径- 返回:
- 请求对象
-
createPostRequest
创建POST请求- 参数:
path- 请求路径body- 请求体- 返回:
- 请求对象
-
createPatchRequest
创建PATCH请求- 参数:
path- 请求路径body- 请求体- 返回:
- 请求对象
-
createPutRequest
创建PUT请求- 参数:
path- 请求路径body- 请求体- 返回:
- 请求对象
-
createDeleteRequest
创建DELETE请求- 参数:
path- 请求路径body- 请求体- 返回:
- 请求对象
-
createJsonRequestBody
创建JSON请求体- 参数:
body- 请求体对象- 返回:
- 请求体
-
createApiException
创建API异常- 参数:
code- HTTP状态码message- 错误消息- 返回:
- API异常
-
buildUrlWithParams
构建URL查询参数- 参数:
path- 请求路径params- 参数映射- 返回:
- 完整URL
-
addIfNotEmpty
添加非空字符串参数- 参数:
params- 参数映射key- 键value- 值
-
addIfNotNull
添加非空参数- 参数:
params- 参数映射key- 键value- 值
-
buildUrlWithMultiValueParams
构建支持多值参数的URL查询参数 特别处理List类型的参数,生成多个同名参数 例如:tag_ids=[id1,id2] -> ?tag_ids=id1无效输入:'&tag_ids'=id2 - 参数:
path- 请求路径params- 参数映射- 返回:
- 完整URL
-