请求配置验证, 默认值初始化 + full_url + method验证
This commit is contained in:
114
validate/request_config.go
Normal file
114
validate/request_config.go
Normal file
@ -0,0 +1,114 @@
|
||||
// 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 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
|
||||
}
|
Reference in New Issue
Block a user