LibWechat.Internal.RequestBuilder (lib_wechat v0.4.0)
View Source请求构建器模块,负责统一处理微信 API 请求的构建。 该模块处理请求头、请求参数、请求体等的准备工作,以便简化 API 模块的实现。
Summary
Functions
@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}- 请求失败
@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_tokenparams- 额外的查询参数
返回值
{:ok, %Http.Response{}}- 请求成功{:error, error}- 请求失败
@spec handle_binary_response( {:ok, LibWechat.Model.Http.Response.t()} | {:error, any()} ) :: {:ok, binary()} | {:error, any()}
处理二进制响应体(如图片数据)。
参数
response- HTTP 响应
返回值
{:ok, binary()}- 处理成功{:error, error}- 处理失败
@spec handle_json_response({:ok, LibWechat.Model.Http.Response.t()} | {:error, any()}) :: {:ok, map()} | {:error, any()}
处理 JSON 响应体。
参数
response- HTTP 响应
返回值
{:ok, map()}- 解析成功{:error, error}- 解析失败
@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_tokenpayload- POST 请求体(将被转换为 JSON)
返回值
{:ok, %Http.Response{}}- 请求成功{:error, error}- 请求失败