requests/workflow.go

151 lines
3.0 KiB
Go
Raw Normal View History

2018-10-17 06:25:17 +00:00
package requests
2018-10-17 10:38:04 +00:00
import (
"net/url"
"regexp"
)
2018-10-17 06:25:17 +00:00
// Workflow 工作流
type Workflow struct {
2018-10-17 10:38:04 +00:00
session *Session
ParsedURL *url.URL
Method string
Body *Params
2018-10-17 06:25:17 +00:00
}
// NewWorkflow new and init workflow
func NewWorkflow(ses *Session) *Workflow {
wf := &Workflow{}
wf.SwitchSession(ses)
2018-10-17 10:38:04 +00:00
wf.Body = &Params{}
2018-10-17 06:25:17 +00:00
return wf
}
// SwitchSession 替换Session
func (wf *Workflow) SwitchSession(ses *Session) {
wf.session = ses
}
2018-10-17 10:38:04 +00:00
// GetStringURL 获取url的string形式
func (wf *Workflow) GetStringURL() string {
return wf.ParsedURL.String()
}
// SetURL 设置 url
func (wf *Workflow) SetURL(srcURL string) *Workflow {
purl, err := url.ParseRequestURI(srcURL)
if err != nil {
panic(err)
}
wf.ParsedURL = purl
return wf
}
// GetURLQuery 获取Query参数
func (wf *Workflow) GetURLQuery() url.Values {
if wf.ParsedURL != nil {
return wf.ParsedURL.Query()
}
return nil
}
// SetURLQuery 设置Query参数
func (wf *Workflow) SetURLQuery(query url.Values) *Workflow {
if query == nil {
return wf
}
wf.ParsedURL.RawQuery = query.Encode()
return wf
}
var regexGetPath = regexp.MustCompile("/[^/]*")
// GetURLPath 获取Path参数
func (wf *Workflow) GetURLPath() []string {
return regexGetPath.FindAllString(wf.ParsedURL.Path, -1)
}
// GetURLRawPath 获取未分解Path参数
func (wf *Workflow) GetURLRawPath() string {
return wf.ParsedURL.Path
}
// encodePath path格式每个item都必须以/开头
func encodePath(path []string) string {
rawpath := ""
for _, p := range path {
if p[0] != '/' {
p = "/" + p
}
rawpath += p
}
return rawpath
}
// SetURLPath 设置Path参数
func (wf *Workflow) SetURLPath(path []string) *Workflow {
if path == nil {
return wf
}
wf.ParsedURL.Path = encodePath(path)
return wf
}
// SetURLRawPath 设置Path参数
func (wf *Workflow) SetURLRawPath(path string) *Workflow {
wf.ParsedURL.Path = path
return wf
}
// SetBodyParams 参数设置
func (wf *Workflow) SetBodyParams(params ...interface{}) *Workflow {
if params == nil {
return wf
}
2018-10-17 06:25:17 +00:00
plen := len(params)
defaultContentType := TypeURLENCODED
if plen >= 2 {
t := params[plen-1]
defaultContentType = t.(string)
wf.Body.ContentType = defaultContentType
} else {
wf.Body.ContentType = defaultContentType
}
if defaultContentType == TypeFormData {
// TODO: form-data
createMultipart(wf.Body, params)
} else {
var values url.Values
switch param := params[0].(type) {
case map[string]string:
values := make(url.Values)
for k, v := range param {
values.Set(k, v)
}
2018-10-17 10:38:04 +00:00
wf.Body.IOBody = []byte(values.Encode())
2018-10-17 06:25:17 +00:00
case map[string][]string:
values = param
2018-10-17 10:38:04 +00:00
wf.Body.IOBody = []byte(values.Encode())
2018-10-17 06:25:17 +00:00
case string:
2018-10-17 10:38:04 +00:00
wf.Body.IOBody = []byte(param)
2018-10-17 06:25:17 +00:00
case []byte:
2018-10-17 10:38:04 +00:00
wf.Body.IOBody = param
2018-10-17 06:25:17 +00:00
}
}
return wf
}
// Execute 执行
func (wf *Workflow) Execute() (*Response, error) {
2018-10-17 10:38:04 +00:00
req := buildBodyRequest(wf.Method, wf.GetStringURL(), wf.Body)
2018-10-17 06:25:17 +00:00
resp, err := wf.session.client.Do(req)
if err != nil {
return nil, err
}
return FromHTTPResponse(resp)
}