150 lines
4.2 KiB
Go
150 lines
4.2 KiB
Go
package utils
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"github.com/go-resty/resty/v2"
|
||
"github.com/zeromicro/go-zero/core/logx"
|
||
"gitlink.org.cn/JointCloud/pcm-ac/internal/pkg/utils/httputils"
|
||
"runtime"
|
||
)
|
||
|
||
// NewRequest 构造一个请求
|
||
func NewRequest(token string, endpoint string, cli *resty.Client) (*resty.Request, error) {
|
||
|
||
cli.SetHeader("token", token).SetBaseURL(endpoint)
|
||
return cli.R(), nil
|
||
}
|
||
|
||
var baseUrl string
|
||
|
||
// Get 方法
|
||
func Get(token, endpoint, url string, pathParams *map[string]string, queryParams *map[string]string, resultData interface{}) (*resty.Response, error) {
|
||
// 获取上层调用者PC,文件名,所在行
|
||
funcName := GetCallFunc()
|
||
ctx := context.TODO()
|
||
client := httputils.NewHttpsClient()
|
||
req, err := NewRequest(token, endpoint, client)
|
||
if err != nil {
|
||
logx.WithContext(ctx).Errorf("AC", "Func: %s, Get Request URL : %s, Init Request Client err: %s", funcName, url, err.Error())
|
||
return nil, err
|
||
}
|
||
|
||
req = req.
|
||
SetHeader(httputils.ContentType, httputils.ApplicationJson).
|
||
SetResult(resultData)
|
||
if pathParams != nil {
|
||
req.SetPathParams(*pathParams)
|
||
}
|
||
if queryParams != nil {
|
||
req.SetQueryParams(*queryParams)
|
||
}
|
||
|
||
result, err := req.Get(url)
|
||
|
||
return ResponseWithLog(&ctx, funcName, req, result, err)
|
||
}
|
||
|
||
// Delete 方法
|
||
func Delete(token, endpoint, url string, pathParams *map[string]string, body *string, resultData interface{}) (*resty.Response, error) {
|
||
|
||
// 获取上层调用者PC,文件名,所在行
|
||
funcName := GetCallFunc()
|
||
ctx := context.TODO()
|
||
|
||
client := httputils.NewHttpsClient()
|
||
req, err := NewRequest(token, endpoint, client)
|
||
if err != nil {
|
||
logx.WithContext(ctx).Errorf("AC", "Func: %s, Delete Request URL : %s, Init Request Client err: %s", funcName, url, err.Error())
|
||
return nil, err
|
||
}
|
||
|
||
req = req.
|
||
SetHeader(httputils.ContentType, httputils.ApplicationJson).
|
||
SetResult(resultData)
|
||
if body != nil {
|
||
req.SetBody(*body)
|
||
}
|
||
if pathParams != nil {
|
||
req.SetPathParams(*pathParams)
|
||
}
|
||
|
||
result, err := req.Delete(url)
|
||
|
||
return ResponseWithLog(&ctx, funcName, req, result, err)
|
||
}
|
||
|
||
// Post
|
||
func Post(token, endpoint, url string, contentType string,
|
||
pathParams *map[string]string, fileParams *map[string]string, queryParams *map[string]string,
|
||
body *string, formData *map[string]string, resultData interface{}) (*resty.Response, error) {
|
||
|
||
// 获取上层调用者PC,文件名,所在行
|
||
funcName := GetCallFunc()
|
||
ctx := context.TODO()
|
||
|
||
client := httputils.NewHttpsClient()
|
||
req, err := NewRequest(token, endpoint, client)
|
||
if err != nil {
|
||
logx.WithContext(ctx).Errorf("AC Func: %s, Post Request URL : %s, Init Request Client err: %s", funcName, url, err.Error())
|
||
return nil, err
|
||
}
|
||
|
||
req = req.
|
||
SetHeader(httputils.ContentType, contentType).
|
||
SetResult(resultData)
|
||
if body != nil {
|
||
req.SetBody(*body)
|
||
}
|
||
if pathParams != nil {
|
||
req.SetPathParams(*pathParams)
|
||
}
|
||
if fileParams != nil {
|
||
req.SetFiles(*fileParams)
|
||
}
|
||
if queryParams != nil {
|
||
req.SetQueryParams(*queryParams)
|
||
}
|
||
if formData != nil {
|
||
req.SetFormData(*formData)
|
||
}
|
||
|
||
result, err := req.Post(url)
|
||
|
||
return ResponseWithLog(&ctx, funcName, req, result, err)
|
||
}
|
||
|
||
// 获取调用方法
|
||
func GetCallFunc() string {
|
||
|
||
// 获取上层调用者PC,文件名,所在行
|
||
var funcName string
|
||
pc, _, _, ok := runtime.Caller(2)
|
||
if !ok {
|
||
// 不ok,函数栈用尽了
|
||
funcName = "-"
|
||
} else {
|
||
// 根据PC获取函数名
|
||
funcName = runtime.FuncForPC(pc).Name()
|
||
}
|
||
|
||
return funcName
|
||
}
|
||
|
||
// 提取错误信息并记录日志
|
||
func ResponseWithLog(ctx *context.Context, funcName string, req *resty.Request, result *resty.Response, err error) (*resty.Response, error) {
|
||
// 请求错误
|
||
if err != nil {
|
||
logx.WithContext(*ctx).Errorf("AC Request Error. Func: %s , URL: %s , Request Error: %+v , Response Body : %+v", funcName, req.RawRequest.URL, err.Error(), string(result.Body()))
|
||
return result, err
|
||
}
|
||
|
||
// 接口报错
|
||
if 200 != result.StatusCode() {
|
||
logx.WithContext(*ctx).Errorf("AC Request Error. Func: %s, URL: %s , Response: %s", funcName, req.RawRequest.URL, string(result.Body()))
|
||
return result, errors.New("response StatusCode != 200")
|
||
}
|
||
logx.WithContext(*ctx).Debugf("AC Request Error. Func: %s , URL: %s , Response: %s", funcName, req.RawRequest.URL, string(result.Body()))
|
||
return result, nil
|
||
}
|