Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
72491204d6 | ||
|
|
84ae0a710e | ||
| 12cf8d58ba | |||
|
|
a6217886dc | ||
|
|
0c71408d1f | ||
|
|
9d6dee25c9 | ||
|
|
8a6e29bc8a | ||
|
|
c4a12f9314 |
12
base.go
12
base.go
@@ -12,17 +12,19 @@ func buildBodyRequest(wf *Workflow) *http.Request {
|
||||
var err error
|
||||
contentType := ""
|
||||
|
||||
if wf.Body.IOBody() == nil {
|
||||
if wf.Body.GetIOBody() == nil {
|
||||
req, err = http.NewRequest(wf.Method, wf.GetRawURL(), nil)
|
||||
} else {
|
||||
var bodybuf *bytes.Buffer
|
||||
switch wf.Body.IOBody().(type) {
|
||||
switch wf.Body.GetIOBody().(type) {
|
||||
case []byte:
|
||||
bodybuf = bytes.NewBuffer(wf.Body.IOBody().([]byte))
|
||||
bodybuf = bytes.NewBuffer(wf.Body.GetIOBody().([]byte))
|
||||
case string:
|
||||
bodybuf = bytes.NewBuffer([]byte(wf.Body.GetIOBody().(string)))
|
||||
case *bytes.Buffer:
|
||||
bodybuf = bytes.NewBuffer(wf.Body.IOBody().(*bytes.Buffer).Bytes())
|
||||
bodybuf = bytes.NewBuffer(wf.Body.GetIOBody().(*bytes.Buffer).Bytes())
|
||||
default:
|
||||
panic(errors.New("the type is not exist, type is" + reflect.TypeOf(wf.Body.IOBody).String()))
|
||||
panic(errors.New("the type is not exist, type is " + reflect.TypeOf(wf.Body.GetIOBody()).String()))
|
||||
}
|
||||
req, err = http.NewRequest(wf.Method, wf.GetRawURL(), bodybuf)
|
||||
}
|
||||
|
||||
12
multipart.go
12
multipart.go
@@ -15,6 +15,11 @@ func writeFormUploadFile(mwriter *multipart.Writer, ufile *UploadFile) {
|
||||
log.Panic(err)
|
||||
}
|
||||
io.Copy(part, ufile.FileReaderCloser)
|
||||
|
||||
err = ufile.FileReaderCloser.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func createMultipart(postParams IBody, params []interface{}) {
|
||||
@@ -23,8 +28,6 @@ func createMultipart(postParams IBody, params []interface{}) {
|
||||
body := &bytes.Buffer{}
|
||||
mwriter := multipart.NewWriter(body)
|
||||
|
||||
defer mwriter.Close()
|
||||
|
||||
for _, iparam := range params[0 : plen-1] {
|
||||
switch param := iparam.(type) {
|
||||
case *UploadFile:
|
||||
@@ -87,4 +90,9 @@ func createMultipart(postParams IBody, params []interface{}) {
|
||||
|
||||
postParams.AddContentType(mwriter.FormDataContentType())
|
||||
postParams.SetIOBody(body)
|
||||
|
||||
err := mwriter.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
45
session.go
45
session.go
@@ -36,8 +36,8 @@ func (body *Body) SetIOBody(iobody interface{}) {
|
||||
body.ioBody = iobody
|
||||
}
|
||||
|
||||
// IOBody 获取ioBody值
|
||||
func (body *Body) IOBody() interface{} {
|
||||
// GetIOBody 获取ioBody值
|
||||
func (body *Body) GetIOBody() interface{} {
|
||||
return body.ioBody
|
||||
}
|
||||
|
||||
@@ -70,16 +70,15 @@ func (body *Body) AddContentType(ct string) {
|
||||
|
||||
// IBody 相关参数结构
|
||||
type IBody interface {
|
||||
|
||||
// Query map[string][]string
|
||||
IOBody() interface{}
|
||||
// SetIOBody
|
||||
// GetIOBody 获取iobody data
|
||||
GetIOBody() interface{}
|
||||
// SetIOBody 设置iobody data
|
||||
SetIOBody(iobody interface{})
|
||||
// Files []UploadFile
|
||||
// ContentType 返回包括 Prefix 所有的ContentType
|
||||
ContentType() string
|
||||
// AppendContent
|
||||
AddContentType(ct string)
|
||||
// AddPrefix 添加 Prefix
|
||||
// SetPrefix 设置 Prefix; 唯一前缀
|
||||
SetPrefix(ct string)
|
||||
}
|
||||
|
||||
@@ -230,7 +229,7 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
|
||||
ses.transport.Proxy = nil
|
||||
}
|
||||
case CInsecure:
|
||||
ses.transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: !values.(bool)}
|
||||
ses.transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: values.(bool)}
|
||||
case CTLS:
|
||||
ses.transport.TLSClientConfig = values.(*tls.Config)
|
||||
case CBasicAuth:
|
||||
@@ -308,6 +307,13 @@ func (ses *Session) ClearCookies() {
|
||||
ses.client.Jar = ses.cookiejar
|
||||
}
|
||||
|
||||
// Head 请求
|
||||
func (ses *Session) Head(url string) *Workflow {
|
||||
wf := NewWorkflow(ses, url)
|
||||
wf.Method = "HEAD"
|
||||
return wf
|
||||
}
|
||||
|
||||
// Get 请求
|
||||
func (ses *Session) Get(url string) *Workflow {
|
||||
wf := NewWorkflow(ses, url)
|
||||
@@ -336,20 +342,6 @@ func (ses *Session) Patch(url string) *Workflow {
|
||||
return wf
|
||||
}
|
||||
|
||||
// Delete 请求
|
||||
func (ses *Session) Delete(url string) *Workflow {
|
||||
wf := NewWorkflow(ses, url)
|
||||
wf.Method = "DELETE"
|
||||
return wf
|
||||
}
|
||||
|
||||
// Head 请求
|
||||
func (ses *Session) Head(url string) *Workflow {
|
||||
wf := NewWorkflow(ses, url)
|
||||
wf.Method = "HEAD"
|
||||
return wf
|
||||
}
|
||||
|
||||
// Options 请求
|
||||
func (ses *Session) Options(url string) *Workflow {
|
||||
wf := NewWorkflow(ses, url)
|
||||
@@ -357,6 +349,13 @@ func (ses *Session) Options(url string) *Workflow {
|
||||
return wf
|
||||
}
|
||||
|
||||
// Delete 请求
|
||||
func (ses *Session) Delete(url string) *Workflow {
|
||||
wf := NewWorkflow(ses, url)
|
||||
wf.Method = "DELETE"
|
||||
return wf
|
||||
}
|
||||
|
||||
// CloseIdleConnections closes the idle connections that a session client may make use of
|
||||
// 从levigross/grequests 借鉴
|
||||
func (ses *Session) CloseIdleConnections() {
|
||||
|
||||
@@ -76,7 +76,7 @@ func TestSession_Post(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ses := NewSession()
|
||||
got, err := ses.Post("http://httpbin.org/post").SetBody(tt.args.params...).Execute()
|
||||
got, err := ses.Post("http://httpbin.org/post").AutoSetBody(tt.args.params...).Execute()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Metchod error = %v", err)
|
||||
@@ -127,7 +127,7 @@ func TestSession_Setparams(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ses := NewSession()
|
||||
|
||||
got, err := ses.Post("http://httpbin.org/post").SetBody(tt.args.params...).Execute()
|
||||
got, err := ses.Post("http://httpbin.org/post").AutoSetBody(tt.args.params...).Execute()
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Metchod error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
@@ -169,7 +169,7 @@ func TestSession_PostUploadFile(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ses := NewSession()
|
||||
got, err := ses.Post("http://httpbin.org/post").SetBody(tt.args.params, TypeFormData).Execute()
|
||||
got, err := ses.Post("http://httpbin.org/post").AutoSetBody(tt.args.params, TypeFormData).Execute()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Metchod error = %v", err)
|
||||
@@ -213,7 +213,7 @@ func TestSession_Put(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ses := NewSession()
|
||||
got, err := ses.Put("http://httpbin.org/put").SetBody(tt.args.params, TypeFormData).Execute()
|
||||
got, err := ses.Put("http://httpbin.org/put").AutoSetBody(tt.args.params, TypeFormData).Execute()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Metchod error = %v", err)
|
||||
@@ -257,7 +257,7 @@ func TestSession_Patch(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ses := NewSession()
|
||||
got, err := ses.Patch("http://httpbin.org/patch").SetBody(tt.args.params, TypeFormData).Execute()
|
||||
got, err := ses.Patch("http://httpbin.org/patch").AutoSetBody(tt.args.params, TypeFormData).Execute()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Metchod error = %v", err)
|
||||
|
||||
@@ -17,13 +17,11 @@ type UploadFile struct {
|
||||
// UploadFileFromPath 从本地文件获取上传文件
|
||||
func UploadFileFromPath(fileName string) (*UploadFile, error) {
|
||||
fd, err := os.Open(fileName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &UploadFile{FileReaderCloser: fd, FileName: fileName}, nil
|
||||
|
||||
}
|
||||
|
||||
// UploadFileFromGlob 根据Glob从本地文件获取上传文件
|
||||
|
||||
39
workflow.go
39
workflow.go
@@ -41,9 +41,16 @@ func (wf *Workflow) AddHeader(key, value string) *Workflow {
|
||||
return wf
|
||||
}
|
||||
|
||||
// SetHeader 设置头信息 这样构造 []string{value} Get方法从Header参数上获取
|
||||
func (wf *Workflow) SetHeader(key, value string) *Workflow {
|
||||
wf.Header.Set(key, value)
|
||||
// SetHeader 设置完全替换原有Header
|
||||
func (wf *Workflow) SetHeader(header http.Header) *Workflow {
|
||||
wf.Header = make(http.Header)
|
||||
for k, HValues := range header {
|
||||
var newHValues []string
|
||||
for _, value := range HValues {
|
||||
newHValues = append(newHValues, value)
|
||||
}
|
||||
wf.Header[k] = newHValues
|
||||
}
|
||||
return wf
|
||||
}
|
||||
|
||||
@@ -69,6 +76,14 @@ func (wf *Workflow) AddCookie(c *http.Cookie) *Workflow {
|
||||
return wf
|
||||
}
|
||||
|
||||
// AddCookies 添加[]*http.Cookie
|
||||
func (wf *Workflow) AddCookies(cookies []*http.Cookie) *Workflow {
|
||||
for _, c := range cookies {
|
||||
wf.AddCookie(c)
|
||||
}
|
||||
return wf
|
||||
}
|
||||
|
||||
// AddKVCookie 添加 以 key value 的 Cookie
|
||||
func (wf *Workflow) AddKVCookie(name, value string) *Workflow {
|
||||
wf.Cookies[name] = &http.Cookie{Name: name, Value: value}
|
||||
@@ -185,7 +200,18 @@ func (wf *Workflow) SetURLRawPath(path string) *Workflow {
|
||||
}
|
||||
|
||||
// SetBody 参数设置
|
||||
func (wf *Workflow) SetBody(params ...interface{}) *Workflow {
|
||||
func (wf *Workflow) SetBody(body IBody) *Workflow {
|
||||
wf.Body = body
|
||||
return wf
|
||||
}
|
||||
|
||||
// GetBody 参数设置
|
||||
func (wf *Workflow) GetBody(body IBody) IBody {
|
||||
return wf.Body
|
||||
}
|
||||
|
||||
// AutoSetBody 参数设置
|
||||
func (wf *Workflow) AutoSetBody(params ...interface{}) *Workflow {
|
||||
|
||||
if params != nil {
|
||||
plen := len(params)
|
||||
@@ -194,11 +220,10 @@ func (wf *Workflow) SetBody(params ...interface{}) *Workflow {
|
||||
if plen >= 2 {
|
||||
t := params[plen-1]
|
||||
defaultContentType = t.(string)
|
||||
wf.Body.SetPrefix(defaultContentType)
|
||||
} else {
|
||||
wf.Body.SetPrefix(defaultContentType)
|
||||
}
|
||||
|
||||
wf.Body.SetPrefix(defaultContentType)
|
||||
|
||||
if defaultContentType == TypeFormData {
|
||||
createMultipart(wf.Body, params)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user