LibWechat.Internal.RequestBuilder (lib_wechat v0.4.0)

View Source

请求构建器模块,负责统一处理微信 API 请求的构建。 该模块处理请求头、请求参数、请求体等的准备工作,以便简化 API 模块的实现。

Summary

Functions

构建带有 access_token 的 GET 请求。

处理二进制响应体(如图片数据)。

处理 JSON 响应体。

构建带有 access_token 的 POST 请求。

Functions

do_request(config, method, api, params, body, opts \\ [])

@spec do_request(
  LibWechat.Model.Config.t(),
  LibWechat.Typespecs.method(),
  binary(),
  LibWechat.Typespecs.params(),
  LibWechat.Typespecs.body(),
  Keyword.t()
) :: {:ok, LibWechat.Model.Http.Response.t()} | {:error, LibWechat.Error.t()}

执行 HTTP 请求。

参数

  • config - 配置信息
  • method - HTTP 方法 (:get, :post 等)
  • api - API 路径
  • params - URL 查询参数
  • body - 请求体
  • opts - 请求选项

返回值

  • {:ok, %Http.Response{}} - 请求成功
  • {:error, error} - 请求失败

get_with_token(config, api, token, params \\ %{})

@spec get_with_token(LibWechat.Model.Config.t(), binary(), binary(), map()) ::
  {:ok, LibWechat.Model.Http.Response.t()} | {:error, LibWechat.Error.t()}

构建带有 access_token 的 GET 请求。

参数

  • config - 配置信息
  • api - API 路径
  • token - access_token
  • params - 额外的查询参数

返回值

  • {:ok, %Http.Response{}} - 请求成功
  • {:error, error} - 请求失败

handle_binary_response(error)

@spec handle_binary_response(
  {:ok, LibWechat.Model.Http.Response.t()}
  | {:error, any()}
) ::
  {:ok, binary()} | {:error, any()}

处理二进制响应体(如图片数据)。

参数

  • response - HTTP 响应

返回值

  • {:ok, binary()} - 处理成功
  • {:error, error} - 处理失败

handle_json_response(error)

@spec handle_json_response({:ok, LibWechat.Model.Http.Response.t()} | {:error, any()}) ::
  {:ok, map()} | {:error, any()}

处理 JSON 响应体。

参数

  • response - HTTP 响应

返回值

  • {:ok, map()} - 解析成功
  • {:error, error} - 解析失败

post_with_token(config, api, token, payload)

@spec post_with_token(LibWechat.Model.Config.t(), binary(), binary(), map()) ::
  {:ok, LibWechat.Model.Http.Response.t()} | {:error, LibWechat.Error.t()}

构建带有 access_token 的 POST 请求。

参数

  • config - 配置信息
  • api - API 路径
  • token - access_token
  • payload - POST 请求体(将被转换为 JSON)

返回值

  • {:ok, %Http.Response{}} - 请求成功
  • {:error, error} - 请求失败