2020-09-01 10:34:11 +08:00
|
|
|
|
package httpclient
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"io"
|
2023-09-23 22:52:05 +08:00
|
|
|
|
"mayfly-go/pkg/logx"
|
|
|
|
|
|
"mayfly-go/pkg/utils/stringx"
|
2021-07-28 18:03:19 +08:00
|
|
|
|
"mime/multipart"
|
2020-09-01 10:34:11 +08:00
|
|
|
|
"net/http"
|
2021-07-28 18:03:19 +08:00
|
|
|
|
"os"
|
2020-09-01 10:34:11 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 默认超时
|
|
|
|
|
|
const DefTimeout = 60
|
|
|
|
|
|
|
|
|
|
|
|
type RequestWrapper struct {
|
2023-09-23 22:52:05 +08:00
|
|
|
|
client http.Client
|
2020-09-01 10:34:11 +08:00
|
|
|
|
url string
|
|
|
|
|
|
method string
|
|
|
|
|
|
timeout int
|
|
|
|
|
|
body io.Reader
|
|
|
|
|
|
header map[string]string
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
type MultipartFile struct {
|
|
|
|
|
|
FieldName string // 字段名
|
|
|
|
|
|
FileName string // 文件名
|
|
|
|
|
|
FilePath string // 文件路径,文件路径不为空,则优先读取文件路径的内容
|
|
|
|
|
|
Bytes []byte // 文件内容
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-01 10:34:11 +08:00
|
|
|
|
// 创建一个请求
|
|
|
|
|
|
func NewRequest(url string) *RequestWrapper {
|
2023-09-23 22:52:05 +08:00
|
|
|
|
return &RequestWrapper{url: url, client: http.Client{}}
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *RequestWrapper) Url(url string) *RequestWrapper {
|
|
|
|
|
|
r.url = url
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
func (r *RequestWrapper) Header(name, value string) *RequestWrapper {
|
|
|
|
|
|
if r.header == nil {
|
|
|
|
|
|
r.header = make(map[string]string)
|
|
|
|
|
|
}
|
|
|
|
|
|
r.header[name] = value
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-01 10:34:11 +08:00
|
|
|
|
func (r *RequestWrapper) Timeout(timeout int) *RequestWrapper {
|
|
|
|
|
|
r.timeout = timeout
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-23 22:52:05 +08:00
|
|
|
|
func (r *RequestWrapper) GetByQuery(queryMap map[string]any) *ResponseWrapper {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
var params string
|
2023-09-23 22:52:05 +08:00
|
|
|
|
for k, v := range queryMap {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
if params != "" {
|
|
|
|
|
|
params += "&"
|
|
|
|
|
|
}
|
2023-09-23 22:52:05 +08:00
|
|
|
|
params += k + "=" + stringx.AnyToStr(v)
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
r.url += "?" + params
|
|
|
|
|
|
return r.Get()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
func (r *RequestWrapper) Get() *ResponseWrapper {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
r.method = "GET"
|
|
|
|
|
|
r.body = nil
|
2023-09-23 22:52:05 +08:00
|
|
|
|
return sendRequest(r)
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
func (r *RequestWrapper) PostJson(body string) *ResponseWrapper {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
buf := bytes.NewBufferString(body)
|
|
|
|
|
|
r.method = "POST"
|
|
|
|
|
|
r.body = buf
|
|
|
|
|
|
if r.header == nil {
|
|
|
|
|
|
r.header = make(map[string]string)
|
|
|
|
|
|
}
|
|
|
|
|
|
r.header["Content-type"] = "application/json"
|
2023-09-23 22:52:05 +08:00
|
|
|
|
return sendRequest(r)
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
func (r *RequestWrapper) PostObj(body any) *ResponseWrapper {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
marshal, err := json.Marshal(body)
|
|
|
|
|
|
if err != nil {
|
2023-09-23 22:52:05 +08:00
|
|
|
|
return &ResponseWrapper{err: errors.New("解析json obj错误")}
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
return r.PostJson(string(marshal))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-23 22:52:05 +08:00
|
|
|
|
func (r *RequestWrapper) PostForm(params string) *ResponseWrapper {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
buf := bytes.NewBufferString(params)
|
|
|
|
|
|
r.method = "POST"
|
|
|
|
|
|
r.body = buf
|
|
|
|
|
|
if r.header == nil {
|
|
|
|
|
|
r.header = make(map[string]string)
|
|
|
|
|
|
}
|
|
|
|
|
|
r.header["Content-type"] = "application/x-www-form-urlencoded"
|
2023-09-23 22:52:05 +08:00
|
|
|
|
return sendRequest(r)
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
func (r *RequestWrapper) PostMulipart(files []MultipartFile, reqParams map[string]string) *ResponseWrapper {
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
|
|
// 文件写入 buf
|
|
|
|
|
|
writer := multipart.NewWriter(buf)
|
|
|
|
|
|
for _, uploadFile := range files {
|
|
|
|
|
|
var reader io.Reader
|
|
|
|
|
|
// 如果文件路径不为空,则读取该路径文件,否则使用bytes
|
|
|
|
|
|
if uploadFile.FilePath != "" {
|
|
|
|
|
|
file, err := os.Open(uploadFile.FilePath)
|
|
|
|
|
|
if err != nil {
|
2023-09-23 22:52:05 +08:00
|
|
|
|
return &ResponseWrapper{err: err}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
reader = file
|
|
|
|
|
|
} else {
|
|
|
|
|
|
reader = bytes.NewBuffer(uploadFile.Bytes)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
part, err := writer.CreateFormFile(uploadFile.FieldName, uploadFile.FileName)
|
|
|
|
|
|
if err != nil {
|
2023-09-23 22:52:05 +08:00
|
|
|
|
return &ResponseWrapper{err: err}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
2023-09-23 22:52:05 +08:00
|
|
|
|
io.Copy(part, reader)
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 如果有其他参数,则写入body
|
2021-08-18 17:57:33 +08:00
|
|
|
|
for k, v := range reqParams {
|
|
|
|
|
|
if err := writer.WriteField(k, v); err != nil {
|
2023-09-23 22:52:05 +08:00
|
|
|
|
return &ResponseWrapper{err: err}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := writer.Close(); err != nil {
|
2023-09-23 22:52:05 +08:00
|
|
|
|
return &ResponseWrapper{err: err}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
r.method = "POST"
|
|
|
|
|
|
r.body = buf
|
|
|
|
|
|
if r.header == nil {
|
|
|
|
|
|
r.header = make(map[string]string)
|
|
|
|
|
|
}
|
|
|
|
|
|
r.header["Content-type"] = writer.FormDataContentType()
|
2023-09-23 22:52:05 +08:00
|
|
|
|
return sendRequest(r)
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-23 22:52:05 +08:00
|
|
|
|
func sendRequest(rw *RequestWrapper) *ResponseWrapper {
|
|
|
|
|
|
respWrapper := &ResponseWrapper{}
|
|
|
|
|
|
timeout := rw.timeout
|
|
|
|
|
|
if timeout > 0 {
|
|
|
|
|
|
rw.client.Timeout = time.Duration(timeout) * time.Second
|
|
|
|
|
|
} else {
|
|
|
|
|
|
timeout = DefTimeout
|
|
|
|
|
|
}
|
2020-09-01 10:34:11 +08:00
|
|
|
|
|
2023-09-23 22:52:05 +08:00
|
|
|
|
req, err := http.NewRequest(rw.method, rw.url, rw.body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
respWrapper.err = fmt.Errorf("创建请求错误-%s", err.Error())
|
|
|
|
|
|
return respWrapper
|
|
|
|
|
|
}
|
|
|
|
|
|
setRequestHeader(req, rw.header)
|
|
|
|
|
|
resp, err := rw.client.Do(req)
|
|
|
|
|
|
return &ResponseWrapper{resp: resp, err: err}
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-23 22:52:05 +08:00
|
|
|
|
func setRequestHeader(req *http.Request, header map[string]string) {
|
|
|
|
|
|
req.Header.Set("User-Agent", "golang/mayfly")
|
|
|
|
|
|
for k, v := range header {
|
|
|
|
|
|
req.Header.Set(k, v)
|
|
|
|
|
|
}
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-23 22:52:05 +08:00
|
|
|
|
type ResponseWrapper struct {
|
|
|
|
|
|
resp *http.Response
|
|
|
|
|
|
err error
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-23 22:52:05 +08:00
|
|
|
|
// 将响应体通过json解析转为指定结构体
|
|
|
|
|
|
func (r *ResponseWrapper) BodyToObj(objPtr any) error {
|
|
|
|
|
|
bodyBytes, err := r.BodyBytes()
|
2020-09-01 10:34:11 +08:00
|
|
|
|
if err != nil {
|
2023-09-23 22:52:05 +08:00
|
|
|
|
return err
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-23 22:52:05 +08:00
|
|
|
|
err = json.Unmarshal(bodyBytes, &objPtr)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Errorf("解析响应体-json解析失败-%s", err.Error())
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
return nil
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-23 22:52:05 +08:00
|
|
|
|
// 将响应体转为strings
|
|
|
|
|
|
func (r *ResponseWrapper) BodyToString() (string, error) {
|
|
|
|
|
|
bodyBytes, err := r.BodyBytes()
|
2020-09-01 10:34:11 +08:00
|
|
|
|
if err != nil {
|
2023-09-23 22:52:05 +08:00
|
|
|
|
return "", err
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
2023-09-23 22:52:05 +08:00
|
|
|
|
return string(bodyBytes), nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 将响应体通过json解析转为map
|
|
|
|
|
|
func (r *ResponseWrapper) BodyToMap() (map[string]any, error) {
|
|
|
|
|
|
var res map[string]any
|
|
|
|
|
|
return res, r.BodyToObj(&res)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取响应体的字节数组
|
|
|
|
|
|
func (r *ResponseWrapper) BodyBytes() ([]byte, error) {
|
|
|
|
|
|
resp, err := r.GetHttpResp()
|
2020-09-01 10:34:11 +08:00
|
|
|
|
if err != nil {
|
2023-09-23 22:52:05 +08:00
|
|
|
|
return nil, err
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
2023-09-23 22:52:05 +08:00
|
|
|
|
|
2022-08-29 21:43:24 +08:00
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
2023-09-23 22:52:05 +08:00
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
2020-09-01 10:34:11 +08:00
|
|
|
|
if err != nil {
|
2023-09-23 22:52:05 +08:00
|
|
|
|
return nil, fmt.Errorf("读取响应体数据失败-%s", err.Error())
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
2023-09-23 22:52:05 +08:00
|
|
|
|
return body, err
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-23 22:52:05 +08:00
|
|
|
|
// 获取http响应结果结构体
|
|
|
|
|
|
func (r *ResponseWrapper) GetHttpResp() (*http.Response, error) {
|
|
|
|
|
|
if r.err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("请求失败-%s", r.err.Error())
|
|
|
|
|
|
}
|
|
|
|
|
|
if r.resp == nil {
|
|
|
|
|
|
return nil, errors.New("请求失败-响应结构体为空,请检查请求url等信息")
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
2023-09-23 22:52:05 +08:00
|
|
|
|
|
|
|
|
|
|
statusCode := r.resp.StatusCode
|
|
|
|
|
|
if isFailureStatusCode(statusCode) {
|
|
|
|
|
|
logx.Warnf("请求响应状态码为为失败状态: %v", statusCode)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return r.resp, nil
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-23 22:52:05 +08:00
|
|
|
|
func isFailureStatusCode(statusCode int) bool {
|
|
|
|
|
|
return statusCode < http.StatusOK || statusCode >= http.StatusBadRequest
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|