2019-02-13 13:05:59 +00:00
|
|
|
package httpclient
|
|
|
|
|
|
|
|
import (
|
2019-08-26 17:00:17 +00:00
|
|
|
"context"
|
2019-02-13 13:05:59 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Encoding string
|
|
|
|
|
|
|
|
const (
|
|
|
|
EncodingJSON = "JSON"
|
|
|
|
EncodingXML = "XML"
|
|
|
|
EncodingForm = "Form"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RequestData struct {
|
2019-08-26 17:00:17 +00:00
|
|
|
Context context.Context
|
2019-02-13 13:05:59 +00:00
|
|
|
Method string
|
|
|
|
Path string
|
|
|
|
Params url.Values
|
|
|
|
FullURL string // client.BaseURL + Path or FullURL
|
|
|
|
Headers http.Header
|
|
|
|
ReqReader io.Reader
|
|
|
|
ReqEncoding Encoding
|
|
|
|
ReqValue interface{}
|
|
|
|
ReqContentLength int64
|
|
|
|
ExpectedStatus []int
|
|
|
|
IgnoreRedirects bool
|
|
|
|
RespEncoding Encoding
|
|
|
|
RespValue interface{}
|
|
|
|
RespConsume bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RequestData) CanCopy() bool {
|
|
|
|
if r.ReqReader != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RequestData) Copy() (ok bool, nr *RequestData) {
|
|
|
|
if !r.CanCopy() {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nr = &RequestData{
|
|
|
|
Method: r.Method,
|
|
|
|
Path: r.Path,
|
|
|
|
FullURL: r.FullURL,
|
|
|
|
ReqEncoding: r.ReqEncoding,
|
|
|
|
ReqValue: r.ReqValue,
|
|
|
|
IgnoreRedirects: r.IgnoreRedirects,
|
|
|
|
RespEncoding: r.RespEncoding,
|
|
|
|
RespValue: r.RespValue,
|
|
|
|
RespConsume: r.RespConsume,
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Params != nil {
|
|
|
|
nr.Params = make(url.Values)
|
|
|
|
|
|
|
|
for k, vs := range r.Params {
|
|
|
|
nvs := make([]string, len(vs))
|
|
|
|
|
|
|
|
for i, v := range vs {
|
|
|
|
nvs[i] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
nr.Params[k] = nvs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Headers != nil {
|
|
|
|
nr.Headers = make(http.Header)
|
|
|
|
|
|
|
|
for k, vs := range r.Headers {
|
|
|
|
nvs := make([]string, len(vs))
|
|
|
|
|
|
|
|
for i, v := range vs {
|
|
|
|
nvs[i] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
nr.Headers[k] = nvs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.ExpectedStatus != nil {
|
|
|
|
nr.ExpectedStatus = make([]int, len(r.ExpectedStatus))
|
|
|
|
|
|
|
|
for i, v := range r.ExpectedStatus {
|
|
|
|
nr.ExpectedStatus[i] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nr
|
|
|
|
}
|