...
|
...
|
@@ -36,7 +36,7 @@ type Request struct { |
|
|
Param interface{}
|
|
|
}
|
|
|
|
|
|
func (gateway BaseServiceGateway) CreateRequest(url string, method string) *httplib.BeegoHTTPRequest {
|
|
|
func (gateway BaseServiceGateway) CreateRequest(url string, method string, options *RequestOptions) *httplib.BeegoHTTPRequest {
|
|
|
var request *httplib.BeegoHTTPRequest
|
|
|
method = strings.ToUpper(method)
|
|
|
switch method {
|
...
|
...
|
@@ -53,6 +53,11 @@ func (gateway BaseServiceGateway) CreateRequest(url string, method string) *http |
|
|
default:
|
|
|
request = httplib.Get(url)
|
|
|
}
|
|
|
if len(options.Header) > 0 {
|
|
|
for k, v := range options.Header {
|
|
|
request.Header(k, strings.Join(v, ";"))
|
|
|
}
|
|
|
}
|
|
|
return request.SetTimeout(gateway.ConnectTimeout, gateway.ReadWriteTimeout)
|
|
|
}
|
|
|
|
...
|
...
|
@@ -64,7 +69,7 @@ func (gateway BaseServiceGateway) GetResponseData(result Response, data interfac |
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (gateway BaseServiceGateway) FastDoRequest(url, method string, param interface{}, data interface{}) error {
|
|
|
func (gateway BaseServiceGateway) FastDoRequest(url, method string, param interface{}, data interface{}, options ...Option) error {
|
|
|
begin := time.Now()
|
|
|
var err error
|
|
|
var result = "success"
|
...
|
...
|
@@ -82,19 +87,23 @@ func (gateway BaseServiceGateway) FastDoRequest(url, method string, param interf |
|
|
))
|
|
|
}
|
|
|
}()
|
|
|
var requestOptions = &RequestOptions{}
|
|
|
for _, option := range options {
|
|
|
option(requestOptions)
|
|
|
}
|
|
|
err = gateway.DoRequest(Request{
|
|
|
Url: url,
|
|
|
Method: method,
|
|
|
Param: param,
|
|
|
}, &data)
|
|
|
}, &data, requestOptions)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (gateway BaseServiceGateway) DoRequest(requestParam Request, val interface{}) error {
|
|
|
r := gateway.CreateRequest(requestParam.Url, requestParam.Method)
|
|
|
func (gateway BaseServiceGateway) DoRequest(requestParam Request, val interface{}, options *RequestOptions) error {
|
|
|
r := gateway.CreateRequest(requestParam.Url, requestParam.Method, options)
|
|
|
req, err := r.JSONBody(requestParam.Param)
|
|
|
if err != nil {
|
|
|
return err
|
...
|
...
|
@@ -123,3 +132,15 @@ func NewBaseServiceGateway(host string) BaseServiceGateway { |
|
|
host: host,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
type RequestOptions struct {
|
|
|
Header http.Header
|
|
|
}
|
|
|
|
|
|
type Option func(o *RequestOptions)
|
|
|
|
|
|
func WithHeader(header http.Header) Option {
|
|
|
return func(o *RequestOptions) {
|
|
|
o.Header = header
|
|
|
}
|
|
|
} |
...
|
...
|
|