httpclient/validate/request_config.go
2024-06-01 18:41:26 +08:00

132 lines
3.2 KiB
Go

// Package validate ...
//
// Description : validate ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-05-31 16:18
package validate
import (
"git.zhangdeman.cn/gateway/httpclient/define"
"net/http"
"strings"
)
// RequestConfig 验证请求配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:18 2024/5/31
func RequestConfig(reqConfig *define.Request) error {
if nil == reqConfig {
return define.ErrRequestConfigNil
}
rc := &requestConfig{}
if err := rc.validateFullUrl(reqConfig); nil != err {
return err
}
return nil
}
type requestConfig struct {
}
// initDefaultConfig 初始化默认配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:25 2024/5/31
func (rc *requestConfig) initDefaultConfig(reqConfig *define.Request) {
if nil == reqConfig.RetryRule {
reqConfig.RetryRule = &define.RequestRetryRule{
RetryCount: 0,
RetryTimeInterval: 5,
RetryHttpCodeList: make([]int64, 0),
RetryBusinessCodeList: make([]string, 0),
}
}
if nil == reqConfig.Header {
reqConfig.Header = map[string]string{}
}
if len(reqConfig.ContentType) > 0 {
reqConfig.Header["Content-Type"] = reqConfig.ContentType
}
if nil == reqConfig.Cookie {
reqConfig.Cookie = map[string]string{}
}
if reqConfig.ConnectTimeout <= 0 {
reqConfig.ConnectTimeout = define.DefaultConnectTimeout
}
if reqConfig.ReadTimeout <= 0 {
reqConfig.ReadTimeout = define.DefaultReadTimeout
}
reqConfig.CodeField = strings.TrimSpace(reqConfig.CodeField)
reqConfig.MessageField = strings.TrimSpace(reqConfig.MessageField)
reqConfig.DataField = strings.TrimSpace(reqConfig.DataField)
if len(reqConfig.CodeField) == 0 {
reqConfig.CodeField = define.DefaultCodeField
}
if len(reqConfig.MessageField) == 0 {
reqConfig.MessageField = define.DefaultMessageField
}
if len(reqConfig.DataField) == 0 {
reqConfig.DataField = define.DefaultDataField
}
if len(reqConfig.SuccessCodeList) == 0 {
reqConfig.SuccessCodeList = define.DefaultSuccessCodeList
}
}
// validateFullUrl 验证full url
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:17 2024/5/31
func (rc *requestConfig) validateFullUrl(reqConfig *define.Request) error {
// 验证 full url
reqConfig.FullUrl = strings.TrimSpace(reqConfig.FullUrl)
if len(reqConfig.FullUrl) == 0 {
return define.ErrFullUrlEmpty
}
if !strings.HasPrefix(reqConfig.FullUrl, "http://") && !strings.HasPrefix(reqConfig.FullUrl, "https://") {
return define.ErrFullUrlInvalid
}
return nil
}
// validateMethod 验证method
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:22 2024/5/31
func (rc *requestConfig) validateMethod(reqConfig *define.Request) error {
// 验证Method
reqConfig.Method = strings.ToUpper(reqConfig.Method)
if len(reqConfig.Method) == 0 {
return define.ErrFullUrlEmpty
}
supportMethodList := []string{
http.MethodGet,
http.MethodHead,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
http.MethodConnect,
http.MethodOptions,
http.MethodTrace,
}
isSupportMethod := false
for _, item := range supportMethodList {
if item == reqConfig.Method {
isSupportMethod = true
break
}
}
if !isSupportMethod {
return define.ErrMethodIsNotSupport
}
return nil
}