类 AbstractDifyClient

java.lang.Object
io.github.imfangs.dify.client.impl.AbstractDifyClient
直接已知子类:
DefaultDifyDatasetsClient, DifyBaseClientImpl

public abstract class AbstractDifyClient extends Object
Dify API 客户端抽象基类 提供通用的 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

      protected final String baseUrl
    • apiKey

      protected final String apiKey
  • 构造器详细资料

    • AbstractDifyClient

      public AbstractDifyClient(String baseUrl, String apiKey)
      构造函数
      参数:
      baseUrl - API基础URL
      apiKey - API密钥
    • AbstractDifyClient

      public AbstractDifyClient(String baseUrl, String apiKey, okhttp3.OkHttpClient httpClient)
      构造函数
      参数:
      baseUrl - API基础URL
      apiKey - 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

      protected okhttp3.Request createGetRequest(String path)
      创建GET请求
      参数:
      path - 请求路径
      返回:
      请求对象
    • createPostRequest

      protected okhttp3.Request createPostRequest(String path, okhttp3.RequestBody body)
      创建POST请求
      参数:
      path - 请求路径
      body - 请求体
      返回:
      请求对象
    • createPatchRequest

      protected okhttp3.Request createPatchRequest(String path, okhttp3.RequestBody body)
      创建PATCH请求
      参数:
      path - 请求路径
      body - 请求体
      返回:
      请求对象
    • createPutRequest

      protected okhttp3.Request createPutRequest(String path, okhttp3.RequestBody body)
      创建PUT请求
      参数:
      path - 请求路径
      body - 请求体
      返回:
      请求对象
    • createDeleteRequest

      protected okhttp3.Request createDeleteRequest(String path, okhttp3.RequestBody body)
      创建DELETE请求
      参数:
      path - 请求路径
      body - 请求体
      返回:
      请求对象
    • createJsonRequestBody

      protected okhttp3.RequestBody createJsonRequestBody(Object body)
      创建JSON请求体
      参数:
      body - 请求体对象
      返回:
      请求体
    • createApiException

      protected DifyApiException createApiException(int code, String message)
      创建API异常
      参数:
      code - HTTP状态码
      message - 错误消息
      返回:
      API异常
    • buildUrlWithParams

      protected String buildUrlWithParams(String path, Map<String,Object> params)
      构建URL查询参数
      参数:
      path - 请求路径
      params - 参数映射
      返回:
      完整URL
    • addIfNotEmpty

      protected void addIfNotEmpty(Map<String,Object> params, String key, String value)
      添加非空字符串参数
      参数:
      params - 参数映射
      key - 键
      value - 值
    • addIfNotNull

      protected void addIfNotNull(Map<String,Object> params, String key, Object value)
      添加非空参数
      参数:
      params - 参数映射
      key - 键
      value - 值
    • buildUrlWithMultiValueParams

      protected String buildUrlWithMultiValueParams(String path, Map<String,Object> params)
      构建支持多值参数的URL查询参数 特别处理List类型的参数,生成多个同名参数 例如:tag_ids=[id1,id2] -> ?tag_ids=id1无效输入:'&tag_ids'=id2
      参数:
      path - 请求路径
      params - 参数映射
      返回:
      完整URL