requests/requests_method.go
2018-10-17 14:25:17 +08:00

150 lines
3.1 KiB
Go

package requests
import (
"net/http"
"net/url"
)
// Params 相关参数结构
type Params struct {
// Query map[string][]string
Body interface{}
// Files []UploadFile
ContentType string
}
// Session 的基本方法
type Session struct {
client *http.Client
params *Params
}
// 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"
)
// NewSession 创建Session
func NewSession() *Session {
return &Session{client: &http.Client{}, params: &Params{}}
}
// Get 请求
func (ses *Session) Get(url string) (*Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
}
resp, err := ses.client.Do(req)
if err != nil {
return nil, err
}
return FromHTTPResponse(resp)
}
// SetParams 设置Post参数
func (ses *Session) SetParams(params ...interface{}) {
plen := len(params)
defaultContentType := TypeURLENCODED
if plen >= 2 {
t := params[plen-1]
defaultContentType = t.(string)
ses.params.ContentType = defaultContentType
} else {
ses.params.ContentType = defaultContentType
}
if defaultContentType == TypeFormData {
// TODO: form-data
createMultipart(ses.params, 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)
}
ses.params.Body = []byte(values.Encode())
case map[string][]string:
values = param
ses.params.Body = []byte(values.Encode())
case string:
ses.params.Body = []byte(param)
case []byte:
ses.params.Body = param
}
}
}
// Post 请求
func (ses *Session) Post(url string) (*Response, error) {
req := buildBodyRequest("POST", url, ses.params)
resp, err := ses.client.Do(req)
if err != nil {
return nil, err
}
return FromHTTPResponse(resp)
}
// Put 请求
func (ses *Session) Put(url string) (*Response, error) {
req := buildBodyRequest("PUT", url, ses.params)
resp, err := ses.client.Do(req)
if err != nil {
return nil, err
}
return FromHTTPResponse(resp)
}
// Patch 请求
func (ses *Session) Patch(url string) (*Response, error) {
req := buildBodyRequest("PATCH", url, ses.params)
resp, err := ses.client.Do(req)
if err != nil {
return nil, err
}
return FromHTTPResponse(resp)
}
// Delete 请求
func (ses *Session) Delete(url string) (*Response, error) {
req := buildBodyRequest("DELETE", url, ses.params)
resp, err := ses.client.Do(req)
if err != nil {
return nil, err
}
return FromHTTPResponse(resp)
}
// Head 请求
func (ses *Session) Head(url string) (*Response, error) {
req := buildBodyRequest("HEAD", url, ses.params)
resp, err := ses.client.Do(req)
if err != nil {
return nil, err
}
return FromHTTPResponse(resp)
}
// Options 请求
func (ses *Session) Options(url string) (*Response, error) {
req := buildBodyRequest("OPTIONS", url, ses.params)
resp, err := ses.client.Do(req)
if err != nil {
return nil, err
}
return FromHTTPResponse(resp)
}