2024-04-30 17:57:27 +08:00
|
|
|
package client
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
|
2024-07-29 23:52:23 +08:00
|
|
|
"repository.lenntc.com/lenntc/third-platform-sdk/util"
|
2024-04-30 17:57:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// HttpRequest http请求参数
|
|
|
|
|
type HttpRequest struct {
|
|
|
|
|
Headers map[string]string // 请求header参数
|
|
|
|
|
QueryArgs map[string]any // 请求query参数
|
|
|
|
|
BodyArgs map[string]any // 请求body参数
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HttpResponse http响应结果
|
|
|
|
|
type HttpResponse struct {
|
|
|
|
|
Result any // 响应的body数据结构, 必须为指针类型
|
|
|
|
|
RespHeader *http.Header // 响应header
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 16:57:40 +08:00
|
|
|
// HttpClient 第三方平台的http请求client
|
|
|
|
|
type HttpClient interface {
|
2024-04-30 17:57:27 +08:00
|
|
|
// HttpGet GET请求
|
|
|
|
|
HttpGet(url string, req *HttpRequest, resp *HttpResponse) error
|
|
|
|
|
// HttpPost POST请求
|
|
|
|
|
HttpPost(url string, req *HttpRequest, resp *HttpResponse) error
|
|
|
|
|
// HttpPut PUT请求
|
|
|
|
|
HttpPut(url string, req *HttpRequest, resp *HttpResponse) error
|
|
|
|
|
// HttpDelete DELETE请求
|
|
|
|
|
HttpDelete(url string, req *HttpRequest, resp *HttpResponse) error
|
|
|
|
|
// DoHttp 发起http请求
|
|
|
|
|
DoHttp(method string, url string, req *HttpRequest, resp *HttpResponse) error
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 16:57:40 +08:00
|
|
|
type httpClientImpl struct {
|
2024-04-30 17:57:27 +08:00
|
|
|
log logx.Logger
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 16:57:40 +08:00
|
|
|
func NewHttpClient(log logx.Logger) HttpClient {
|
|
|
|
|
return &httpClientImpl{
|
2024-04-30 17:57:27 +08:00
|
|
|
log: log,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 16:57:40 +08:00
|
|
|
func (c *httpClientImpl) HttpGet(url string, req *HttpRequest, resp *HttpResponse) error {
|
2024-04-30 17:57:27 +08:00
|
|
|
return c.DoHttp(http.MethodGet, url, req, resp)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 16:57:40 +08:00
|
|
|
func (c *httpClientImpl) HttpPost(url string, req *HttpRequest, resp *HttpResponse) error {
|
2024-04-30 17:57:27 +08:00
|
|
|
return c.DoHttp(http.MethodPost, url, req, resp)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 16:57:40 +08:00
|
|
|
func (c *httpClientImpl) HttpPut(url string, req *HttpRequest, resp *HttpResponse) error {
|
2024-04-30 17:57:27 +08:00
|
|
|
return c.DoHttp(http.MethodPut, url, req, resp)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 16:57:40 +08:00
|
|
|
func (c *httpClientImpl) HttpDelete(url string, req *HttpRequest, resp *HttpResponse) error {
|
2024-04-30 17:57:27 +08:00
|
|
|
return c.DoHttp(http.MethodDelete, url, req, resp)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 16:57:40 +08:00
|
|
|
func (c *httpClientImpl) DoHttp(method string, url string, req *HttpRequest, resp *HttpResponse) error {
|
2024-04-30 17:57:27 +08:00
|
|
|
// 发起请求
|
|
|
|
|
reqConfig := &util.ReqConfig{
|
|
|
|
|
Headers: req.Headers,
|
|
|
|
|
QueryArgs: req.QueryArgs,
|
|
|
|
|
BodyArgs: req.BodyArgs,
|
|
|
|
|
}
|
2024-05-04 16:57:40 +08:00
|
|
|
hc := util.NewHttpUtil(c.log).NewRequest(method, url, reqConfig).Do()
|
2024-04-30 17:57:27 +08:00
|
|
|
if hc.Error != nil {
|
|
|
|
|
return hc.Error
|
|
|
|
|
}
|
|
|
|
|
var responseHeader *http.Header
|
|
|
|
|
if hc.Response != nil {
|
|
|
|
|
responseHeader = &hc.Response.Header
|
|
|
|
|
}
|
|
|
|
|
// 检查http响应错误
|
|
|
|
|
if err := c.checkResponseError(hc.Response); err != nil {
|
|
|
|
|
c.log.WithFields([]logx.LogField{{Key: "method", Value: method}, {Key: "url", Value: url}, {Key: "reqConfig", Value: reqConfig}}...).
|
2024-05-04 16:57:40 +08:00
|
|
|
Errorf("[httpClientImpl][DoHttp] checkResponseError err:%s", err)
|
2024-04-30 17:57:27 +08:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
// 获取响应结果
|
|
|
|
|
if resp == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
result := resp.Result
|
|
|
|
|
if err := hc.Result(result).Error; err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
resp.Result = result
|
|
|
|
|
resp.RespHeader = responseHeader
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查http响应错误
|
2024-05-04 16:57:40 +08:00
|
|
|
func (c *httpClientImpl) checkResponseError(r *util.Response) error {
|
2024-04-30 17:57:27 +08:00
|
|
|
if r.StatusCode >= 200 && r.StatusCode < 300 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return errors.New(fmt.Sprintf("http response error, status code: %d, status: %s", r.StatusCode, r.Status))
|
|
|
|
|
}
|