优化参数名
This commit is contained in:
+4
-4
@@ -10,8 +10,8 @@ import (
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
resp, code, err := NewCurlx().Send(context.Background(),
|
||||
SetUrl("https://www.baidu.com"),
|
||||
SetMethod(MethodGet),
|
||||
SetParamsUrl("https://www.baidu.com"),
|
||||
SetParamsMethod(MethodGet),
|
||||
)
|
||||
t.Log(resp, code, err)
|
||||
|
||||
@@ -39,12 +39,12 @@ func TestForm(t *testing.T) {
|
||||
p := ClientParams{
|
||||
Url: "http://tech-dev.sealmoo.com/api/material/upload",
|
||||
Method: "POST",
|
||||
Params: s,
|
||||
Body: s,
|
||||
Headers: map[string]interface{}{
|
||||
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnRfaWQiOjAsImNsaWVudF9pZCI6MCwidXNlcl9pZCI6MSwiZXhwIjoxNzAxMzk3NzkxfQ.9_uJ6y8I4JZTwgSenwHC_01nddLuI4zmgpyPhn5M6j8",
|
||||
},
|
||||
ContentType: ContentTypeForm,
|
||||
}
|
||||
resp, code, err := NewCurlx().Send(context.Background(), SetAll(p))
|
||||
resp, code, err := NewCurlx().Send(context.Background(), SetParamsAll(p))
|
||||
fmt.Println(resp, code, err)
|
||||
}
|
||||
|
||||
+2
-2
@@ -18,7 +18,7 @@ type Option func(*clientOptions)
|
||||
/**
|
||||
* 设置超时时间
|
||||
*/
|
||||
func SetTimeOut(t time.Duration) Option {
|
||||
func SetOptionTimeOut(t time.Duration) Option {
|
||||
return func(options *clientOptions) {
|
||||
options.TimeOut = t
|
||||
}
|
||||
@@ -27,7 +27,7 @@ func SetTimeOut(t time.Duration) Option {
|
||||
/**
|
||||
* 不校验HTTPS证书
|
||||
*/
|
||||
func SetTLSInsecureSkipVerify() Option {
|
||||
func SetOptionTLSInsecureSkipVerify() Option {
|
||||
return func(options *clientOptions) {
|
||||
options.InsecureSkipVerify = true
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package curlx
|
||||
type ClientParams struct {
|
||||
Url string
|
||||
Method Method // GET/POST
|
||||
Params interface{}
|
||||
Body interface{}
|
||||
Headers map[string]interface{}
|
||||
Cookies interface{}
|
||||
ContentType ContentType // FORM,JSON,XML
|
||||
@@ -15,11 +15,11 @@ func defaultParams() ClientParams {
|
||||
|
||||
type Param func(*ClientParams)
|
||||
|
||||
func SetAll(cp ClientParams) Param {
|
||||
func SetParamsAll(cp ClientParams) Param {
|
||||
return func(param *ClientParams) {
|
||||
param.Url = cp.Url
|
||||
param.Method = cp.Method
|
||||
param.Params = cp.Params
|
||||
param.Body = cp.Body
|
||||
param.Headers = cp.Headers
|
||||
param.Cookies = cp.Cookies
|
||||
param.ContentType = cp.ContentType
|
||||
@@ -29,7 +29,7 @@ func SetAll(cp ClientParams) Param {
|
||||
/**
|
||||
* 设置URL
|
||||
*/
|
||||
func SetUrl(url string) Param {
|
||||
func SetParamsUrl(url string) Param {
|
||||
return func(param *ClientParams) {
|
||||
param.Url = url
|
||||
}
|
||||
@@ -38,7 +38,7 @@ func SetUrl(url string) Param {
|
||||
/**
|
||||
* 设置方法
|
||||
*/
|
||||
func SetMethod(m Method) Param {
|
||||
func SetParamsMethod(m Method) Param {
|
||||
return func(param *ClientParams) {
|
||||
param.Method = m
|
||||
}
|
||||
@@ -47,9 +47,9 @@ func SetMethod(m Method) Param {
|
||||
/**
|
||||
* 设置参数
|
||||
*/
|
||||
func SetParams(p interface{}) Param {
|
||||
func SetParamsBody(p interface{}) Param {
|
||||
return func(param *ClientParams) {
|
||||
param.Params = p
|
||||
param.Body = p
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,13 +58,13 @@ func SetParams(p interface{}) Param {
|
||||
*/
|
||||
func SetParamsFormText(fieldName, fieldValue string) Param {
|
||||
return func(param *ClientParams) {
|
||||
fp := param.Params.([]FormParam)
|
||||
fp := param.Body.([]FormParam)
|
||||
fp = append(fp, FormParam{
|
||||
FieldName: fieldName,
|
||||
FieldValue: fieldValue,
|
||||
FieldType: FieldTypeText,
|
||||
})
|
||||
param.Params = fp
|
||||
param.Body = fp
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,21 +73,21 @@ func SetParamsFormText(fieldName, fieldValue string) Param {
|
||||
*/
|
||||
func SetParamsFormFile(fieldName, fileName string, fileBytes []byte) Param {
|
||||
return func(param *ClientParams) {
|
||||
fp := param.Params.([]FormParam)
|
||||
fp := param.Body.([]FormParam)
|
||||
fp = append(fp, FormParam{
|
||||
FieldName: fieldName,
|
||||
FieldType: FieldTypeFile,
|
||||
FileName: fileName,
|
||||
FileBytes: fileBytes,
|
||||
})
|
||||
param.Params = fp
|
||||
param.Body = fp
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置请求头
|
||||
*/
|
||||
func SetHeaders(h map[string]interface{}) Param {
|
||||
func SetParamsHeaders(h map[string]interface{}) Param {
|
||||
return func(param *ClientParams) {
|
||||
param.Headers = h
|
||||
}
|
||||
@@ -96,7 +96,7 @@ func SetHeaders(h map[string]interface{}) Param {
|
||||
/**
|
||||
* 设置cookies
|
||||
*/
|
||||
func SetCookies(c interface{}) Param {
|
||||
func SetParamsCookies(c interface{}) Param {
|
||||
return func(param *ClientParams) {
|
||||
param.Cookies = c
|
||||
}
|
||||
@@ -105,7 +105,7 @@ func SetCookies(c interface{}) Param {
|
||||
/**
|
||||
* 设置请求方法
|
||||
*/
|
||||
func SetContentType(t ContentType) Param {
|
||||
func SetParamsContentType(t ContentType) Param {
|
||||
return func(param *ClientParams) {
|
||||
param.ContentType = t
|
||||
}
|
||||
|
||||
+10
-10
@@ -68,17 +68,17 @@ func (p *ClientParams) parseParams() (str io.Reader, err error) {
|
||||
p.Headers = make(map[string]interface{})
|
||||
}
|
||||
|
||||
if p.Params != nil {
|
||||
if p.Body != nil {
|
||||
if p.ContentType == ContentTypeJson {
|
||||
// 判断是否存在
|
||||
if _, ok := p.Headers["Content-Type"]; !ok {
|
||||
p.Headers["Content-Type"] = ContentTypeJson
|
||||
}
|
||||
strParam, ok := p.Params.(string)
|
||||
strParam, ok := p.Body.(string)
|
||||
if ok {
|
||||
return bytes.NewReader([]byte(strParam)), nil
|
||||
}
|
||||
b, err := json.Marshal(p.Params)
|
||||
b, err := json.Marshal(p.Body)
|
||||
if err == nil {
|
||||
return bytes.NewReader(b), nil
|
||||
}
|
||||
@@ -86,9 +86,9 @@ func (p *ClientParams) parseParams() (str io.Reader, err error) {
|
||||
// 表单上传(可能有文件)
|
||||
// 文件上传的
|
||||
params := []FormParam{}
|
||||
if value, ok := p.Params.([]FormParam); ok {
|
||||
if value, ok := p.Body.([]FormParam); ok {
|
||||
params = value
|
||||
} else if value, ok := p.Params.(FormParam); ok {
|
||||
} else if value, ok := p.Body.(FormParam); ok {
|
||||
params = append(params, value)
|
||||
} else {
|
||||
return nil, errors.New("表单上传的参数格式不正确")
|
||||
@@ -113,11 +113,11 @@ func (p *ClientParams) parseParams() (str io.Reader, err error) {
|
||||
p.Headers["Content-Type"] = ContentTypeXml
|
||||
}
|
||||
var string_data string
|
||||
if value, ok := p.Params.(string); ok {
|
||||
if value, ok := p.Body.(string); ok {
|
||||
string_data = string(value)
|
||||
} else {
|
||||
var by []byte
|
||||
by, err = xml.Marshal(p.Params)
|
||||
by, err = xml.Marshal(p.Body)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -130,7 +130,7 @@ func (p *ClientParams) parseParams() (str io.Reader, err error) {
|
||||
}
|
||||
|
||||
var string_data string
|
||||
if value, ok := p.Params.(string); ok {
|
||||
if value, ok := p.Body.(string); ok {
|
||||
string_data = string(value)
|
||||
} else {
|
||||
err = errors.New("TEXT类型的参数仅支持字符串")
|
||||
@@ -144,7 +144,7 @@ func (p *ClientParams) parseParams() (str io.Reader, err error) {
|
||||
}
|
||||
|
||||
// 判断需要map[string]interface{}类型
|
||||
paramValue, ok := p.Params.(map[string]interface{})
|
||||
paramValue, ok := p.Body.(map[string]interface{})
|
||||
if !ok {
|
||||
return strings.NewReader(""), errors.New("参数需map[string]interface{}")
|
||||
}
|
||||
@@ -183,7 +183,7 @@ func (p *ClientParams) parseParams() (str io.Reader, err error) {
|
||||
// 如果是GET请求
|
||||
if p.Method == MethodGet {
|
||||
// 判断需要map[string]interface{}类型
|
||||
paramValue, ok := p.Params.(map[string]interface{})
|
||||
paramValue, ok := p.Body.(map[string]interface{})
|
||||
if !ok {
|
||||
return strings.NewReader(""), errors.New("参数需map[string]interface{}")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user