2018-10-17 06:25:17 +00:00
|
|
|
package requests
|
|
|
|
|
|
|
|
import (
|
2018-10-19 08:26:50 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
2018-10-17 06:25:17 +00:00
|
|
|
"net/http"
|
2018-10-19 08:26:50 +00:00
|
|
|
"net/url"
|
|
|
|
"reflect"
|
|
|
|
"time"
|
2018-10-17 06:25:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Params 相关参数结构
|
|
|
|
type Params struct {
|
|
|
|
// Query map[string][]string
|
2018-10-17 10:38:04 +00:00
|
|
|
IOBody interface{}
|
2018-10-17 06:25:17 +00:00
|
|
|
// Files []UploadFile
|
|
|
|
ContentType string
|
|
|
|
}
|
|
|
|
|
2018-10-19 08:26:50 +00:00
|
|
|
// BasicAuth 帐号认真结构
|
|
|
|
type BasicAuth struct {
|
|
|
|
// User 帐号
|
|
|
|
User string
|
|
|
|
// Password 密码
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
2018-10-17 06:25:17 +00:00
|
|
|
// Session 的基本方法
|
|
|
|
type Session struct {
|
2018-10-19 08:26:50 +00:00
|
|
|
client *http.Client
|
|
|
|
transport *http.Transport
|
2018-10-19 11:16:34 +00:00
|
|
|
cookiejar http.CookieJar
|
2018-10-19 08:26:50 +00:00
|
|
|
params *Params
|
|
|
|
auth *BasicAuth
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TypeContent post类型参数
|
|
|
|
type TypeContent int
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ TypeContent = iota
|
|
|
|
// TypeJSON 类型
|
|
|
|
TypeJSON = "application/json"
|
|
|
|
// TypeXML 类型
|
|
|
|
TypeXML = "text/xml"
|
|
|
|
// TypeURLENCODED 类型
|
|
|
|
TypeURLENCODED = "application/x-www-form-urlencoded"
|
|
|
|
// TypeFormData 类型
|
|
|
|
TypeFormData = "multipart/form-data"
|
|
|
|
)
|
|
|
|
|
2018-10-19 08:26:50 +00:00
|
|
|
// TypeConfig 配置类型
|
|
|
|
type TypeConfig int
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ TypeConfig = iota
|
|
|
|
// ConfigRequestTimeout request 包括 dial request redirect 总时间超时
|
|
|
|
ConfigRequestTimeout // 支持time.Duration 和 int(秒为单位)
|
|
|
|
|
|
|
|
// ConfigDialTimeout 一个Connect过程的Timeout
|
|
|
|
ConfigDialTimeout // 支持time.Duration 和 int(秒为单位)
|
|
|
|
|
|
|
|
// ConfigProxy 代理链接
|
|
|
|
ConfigProxy // http, https, socks5
|
|
|
|
|
|
|
|
// ConfigInsecure InsecureSkipVerify
|
|
|
|
ConfigInsecure // true, false
|
|
|
|
|
|
|
|
// ConfigBasicAuth 帐号认证
|
|
|
|
ConfigBasicAuth // user pwd
|
|
|
|
|
|
|
|
// ConfigTLS 帐号认证
|
|
|
|
ConfigTLS // user pwd
|
|
|
|
)
|
|
|
|
|
2018-10-17 06:25:17 +00:00
|
|
|
// NewSession 创建Session
|
|
|
|
func NewSession() *Session {
|
2018-10-19 08:26:50 +00:00
|
|
|
client := &http.Client{}
|
|
|
|
transport := &http.Transport{}
|
|
|
|
client.Transport = transport
|
|
|
|
|
|
|
|
return &Session{client: client, params: &Params{}, transport: transport, auth: nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig 设置配置
|
|
|
|
func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
|
|
|
|
|
|
|
|
switch typeConfig {
|
|
|
|
case ConfigRequestTimeout:
|
|
|
|
switch v := values.(type) {
|
|
|
|
case time.Duration:
|
|
|
|
ses.client.Timeout = v
|
|
|
|
case int:
|
|
|
|
ses.client.Timeout = time.Duration(v * int(time.Second))
|
|
|
|
case int64:
|
|
|
|
ses.client.Timeout = time.Duration(v * int64(time.Second))
|
|
|
|
case float32:
|
|
|
|
ses.client.Timeout = time.Duration(v * float32(time.Second))
|
|
|
|
case float64:
|
|
|
|
ses.client.Timeout = time.Duration(v * float64(time.Second))
|
|
|
|
default:
|
|
|
|
panic(errors.New("error type " + reflect.TypeOf(v).String()))
|
|
|
|
}
|
|
|
|
case ConfigDialTimeout:
|
2018-10-19 11:16:34 +00:00
|
|
|
// 没时间实现这些小细节
|
2018-10-19 08:26:50 +00:00
|
|
|
case ConfigProxy:
|
|
|
|
switch v := values.(type) {
|
|
|
|
case string:
|
|
|
|
purl, err := (url.Parse(v))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
ses.transport.Proxy = http.ProxyURL(purl)
|
|
|
|
case *url.URL:
|
|
|
|
ses.transport.Proxy = http.ProxyURL(v)
|
|
|
|
}
|
|
|
|
case ConfigInsecure:
|
|
|
|
ses.transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: !values.(bool)}
|
|
|
|
case ConfigTLS:
|
|
|
|
ses.transport.TLSClientConfig = values.(*tls.Config)
|
|
|
|
case ConfigBasicAuth:
|
|
|
|
if ses.auth == nil {
|
|
|
|
ses.auth = &BasicAuth{}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := values.(type) {
|
|
|
|
case *BasicAuth:
|
|
|
|
ses.auth.User = v.User
|
|
|
|
ses.auth.User = v.Password
|
|
|
|
case BasicAuth:
|
|
|
|
ses.auth.User = v.User
|
|
|
|
ses.auth.User = v.Password
|
|
|
|
case []string:
|
|
|
|
ses.auth.User = v[0]
|
|
|
|
ses.auth.User = v[1]
|
|
|
|
case nil:
|
|
|
|
ses.auth = nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(errors.New("unknown typeConfig " + reflect.TypeOf(typeConfig).String()))
|
|
|
|
}
|
|
|
|
return
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 11:16:34 +00:00
|
|
|
// SetCookies 设置Cookies 或者添加Cookies
|
|
|
|
func (ses *Session) SetCookies(cookies interface{}) {
|
|
|
|
switch v := cookies.(type) {
|
|
|
|
case http.Cookie:
|
|
|
|
var req *http.Request
|
|
|
|
req.AddCookie((*http.Cookie))
|
|
|
|
ses.cookies.SetCookies(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DelCookies 设置Cookies 或者添加Cookies
|
|
|
|
func (ses *Session) DelCookies(cookies interface{}) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-17 06:25:17 +00:00
|
|
|
// Get 请求
|
2018-10-17 10:38:04 +00:00
|
|
|
func (ses *Session) Get(url string) *Workflow {
|
|
|
|
wf := NewWorkflow(ses)
|
|
|
|
wf.Method = "GET"
|
|
|
|
wf.SetURL(url)
|
|
|
|
return wf
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Post 请求
|
2018-10-17 10:38:04 +00:00
|
|
|
func (ses *Session) Post(url string) *Workflow {
|
|
|
|
wf := NewWorkflow(ses)
|
|
|
|
wf.Method = "POST"
|
|
|
|
wf.SetURL(url)
|
|
|
|
return wf
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put 请求
|
2018-10-17 10:38:04 +00:00
|
|
|
func (ses *Session) Put(url string) *Workflow {
|
|
|
|
wf := NewWorkflow(ses)
|
|
|
|
wf.Method = "PUT"
|
|
|
|
wf.SetURL(url)
|
|
|
|
return wf
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Patch 请求
|
2018-10-17 10:38:04 +00:00
|
|
|
func (ses *Session) Patch(url string) *Workflow {
|
|
|
|
wf := NewWorkflow(ses)
|
|
|
|
wf.Method = "PATCH"
|
|
|
|
wf.SetURL(url)
|
|
|
|
return wf
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete 请求
|
2018-10-17 10:38:04 +00:00
|
|
|
func (ses *Session) Delete(url string) *Workflow {
|
|
|
|
wf := NewWorkflow(ses)
|
|
|
|
wf.Method = "DELETE"
|
|
|
|
wf.SetURL(url)
|
|
|
|
return wf
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Head 请求
|
2018-10-17 10:38:04 +00:00
|
|
|
func (ses *Session) Head(url string) *Workflow {
|
|
|
|
wf := NewWorkflow(ses)
|
|
|
|
wf.Method = "HEAD"
|
|
|
|
wf.SetURL(url)
|
|
|
|
return wf
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Options 请求
|
2018-10-17 10:38:04 +00:00
|
|
|
func (ses *Session) Options(url string) *Workflow {
|
|
|
|
wf := NewWorkflow(ses)
|
|
|
|
wf.Method = "OPTIONS"
|
|
|
|
wf.SetURL(url)
|
|
|
|
return wf
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|