mirror of
https://github.com/astaxie/beego.git
synced 2024-11-21 22:00:54 +00:00
deprecated httplib and then support prometheus for httplib
This commit is contained in:
parent
dec98f004c
commit
2e891152dd
@ -74,6 +74,7 @@ func createDefaultCookie() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SetDefaultSetting Overwrite default settings
|
// SetDefaultSetting Overwrite default settings
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func SetDefaultSetting(setting BeegoHTTPSettings) {
|
func SetDefaultSetting(setting BeegoHTTPSettings) {
|
||||||
settingMutex.Lock()
|
settingMutex.Lock()
|
||||||
defer settingMutex.Unlock()
|
defer settingMutex.Unlock()
|
||||||
@ -81,6 +82,7 @@ func SetDefaultSetting(setting BeegoHTTPSettings) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewBeegoRequest return *BeegoHttpRequest with specific method
|
// NewBeegoRequest return *BeegoHttpRequest with specific method
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func NewBeegoRequest(rawurl, method string) *BeegoHTTPRequest {
|
func NewBeegoRequest(rawurl, method string) *BeegoHTTPRequest {
|
||||||
var resp http.Response
|
var resp http.Response
|
||||||
u, err := url.Parse(rawurl)
|
u, err := url.Parse(rawurl)
|
||||||
@ -106,31 +108,37 @@ func NewBeegoRequest(rawurl, method string) *BeegoHTTPRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get returns *BeegoHttpRequest with GET method.
|
// Get returns *BeegoHttpRequest with GET method.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func Get(url string) *BeegoHTTPRequest {
|
func Get(url string) *BeegoHTTPRequest {
|
||||||
return NewBeegoRequest(url, "GET")
|
return NewBeegoRequest(url, "GET")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post returns *BeegoHttpRequest with POST method.
|
// Post returns *BeegoHttpRequest with POST method.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func Post(url string) *BeegoHTTPRequest {
|
func Post(url string) *BeegoHTTPRequest {
|
||||||
return NewBeegoRequest(url, "POST")
|
return NewBeegoRequest(url, "POST")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put returns *BeegoHttpRequest with PUT method.
|
// Put returns *BeegoHttpRequest with PUT method.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func Put(url string) *BeegoHTTPRequest {
|
func Put(url string) *BeegoHTTPRequest {
|
||||||
return NewBeegoRequest(url, "PUT")
|
return NewBeegoRequest(url, "PUT")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete returns *BeegoHttpRequest DELETE method.
|
// Delete returns *BeegoHttpRequest DELETE method.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func Delete(url string) *BeegoHTTPRequest {
|
func Delete(url string) *BeegoHTTPRequest {
|
||||||
return NewBeegoRequest(url, "DELETE")
|
return NewBeegoRequest(url, "DELETE")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Head returns *BeegoHttpRequest with HEAD method.
|
// Head returns *BeegoHttpRequest with HEAD method.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func Head(url string) *BeegoHTTPRequest {
|
func Head(url string) *BeegoHTTPRequest {
|
||||||
return NewBeegoRequest(url, "HEAD")
|
return NewBeegoRequest(url, "HEAD")
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeegoHTTPSettings is the http.Client setting
|
// BeegoHTTPSettings is the http.Client setting
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
type BeegoHTTPSettings struct {
|
type BeegoHTTPSettings struct {
|
||||||
ShowDebug bool
|
ShowDebug bool
|
||||||
UserAgent string
|
UserAgent string
|
||||||
@ -148,6 +156,7 @@ type BeegoHTTPSettings struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BeegoHTTPRequest provides more useful methods for requesting one url than http.Request.
|
// BeegoHTTPRequest provides more useful methods for requesting one url than http.Request.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
type BeegoHTTPRequest struct {
|
type BeegoHTTPRequest struct {
|
||||||
url string
|
url string
|
||||||
req *http.Request
|
req *http.Request
|
||||||
@ -160,35 +169,41 @@ type BeegoHTTPRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetRequest return the request object
|
// GetRequest return the request object
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) GetRequest() *http.Request {
|
func (b *BeegoHTTPRequest) GetRequest() *http.Request {
|
||||||
return b.req
|
return b.req
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setting Change request settings
|
// Setting Change request settings
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) Setting(setting BeegoHTTPSettings) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) Setting(setting BeegoHTTPSettings) *BeegoHTTPRequest {
|
||||||
b.setting = setting
|
b.setting = setting
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.
|
// SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) SetBasicAuth(username, password string) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) SetBasicAuth(username, password string) *BeegoHTTPRequest {
|
||||||
b.req.SetBasicAuth(username, password)
|
b.req.SetBasicAuth(username, password)
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetEnableCookie sets enable/disable cookiejar
|
// SetEnableCookie sets enable/disable cookiejar
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) SetEnableCookie(enable bool) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) SetEnableCookie(enable bool) *BeegoHTTPRequest {
|
||||||
b.setting.EnableCookie = enable
|
b.setting.EnableCookie = enable
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetUserAgent sets User-Agent header field
|
// SetUserAgent sets User-Agent header field
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) SetUserAgent(useragent string) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) SetUserAgent(useragent string) *BeegoHTTPRequest {
|
||||||
b.setting.UserAgent = useragent
|
b.setting.UserAgent = useragent
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debug sets show debug or not when executing request.
|
// Debug sets show debug or not when executing request.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) Debug(isdebug bool) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) Debug(isdebug bool) *BeegoHTTPRequest {
|
||||||
b.setting.ShowDebug = isdebug
|
b.setting.ShowDebug = isdebug
|
||||||
return b
|
return b
|
||||||
@ -198,28 +213,33 @@ func (b *BeegoHTTPRequest) Debug(isdebug bool) *BeegoHTTPRequest {
|
|||||||
// default is 0 means no retried.
|
// default is 0 means no retried.
|
||||||
// -1 means retried forever.
|
// -1 means retried forever.
|
||||||
// others means retried times.
|
// others means retried times.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) Retries(times int) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) Retries(times int) *BeegoHTTPRequest {
|
||||||
b.setting.Retries = times
|
b.setting.Retries = times
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) RetryDelay(delay time.Duration) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) RetryDelay(delay time.Duration) *BeegoHTTPRequest {
|
||||||
b.setting.RetryDelay = delay
|
b.setting.RetryDelay = delay
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// DumpBody setting whether need to Dump the Body.
|
// DumpBody setting whether need to Dump the Body.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) DumpBody(isdump bool) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) DumpBody(isdump bool) *BeegoHTTPRequest {
|
||||||
b.setting.DumpBody = isdump
|
b.setting.DumpBody = isdump
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// DumpRequest return the DumpRequest
|
// DumpRequest return the DumpRequest
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) DumpRequest() []byte {
|
func (b *BeegoHTTPRequest) DumpRequest() []byte {
|
||||||
return b.dump
|
return b.dump
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetTimeout sets connect time out and read-write time out for BeegoRequest.
|
// SetTimeout sets connect time out and read-write time out for BeegoRequest.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *BeegoHTTPRequest {
|
||||||
b.setting.ConnectTimeout = connectTimeout
|
b.setting.ConnectTimeout = connectTimeout
|
||||||
b.setting.ReadWriteTimeout = readWriteTimeout
|
b.setting.ReadWriteTimeout = readWriteTimeout
|
||||||
@ -227,18 +247,21 @@ func (b *BeegoHTTPRequest) SetTimeout(connectTimeout, readWriteTimeout time.Dura
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SetTLSClientConfig sets tls connection configurations if visiting https url.
|
// SetTLSClientConfig sets tls connection configurations if visiting https url.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) SetTLSClientConfig(config *tls.Config) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) SetTLSClientConfig(config *tls.Config) *BeegoHTTPRequest {
|
||||||
b.setting.TLSClientConfig = config
|
b.setting.TLSClientConfig = config
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// Header add header item string in request.
|
// Header add header item string in request.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) Header(key, value string) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) Header(key, value string) *BeegoHTTPRequest {
|
||||||
b.req.Header.Set(key, value)
|
b.req.Header.Set(key, value)
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetHost set the request host
|
// SetHost set the request host
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) SetHost(host string) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) SetHost(host string) *BeegoHTTPRequest {
|
||||||
b.req.Host = host
|
b.req.Host = host
|
||||||
return b
|
return b
|
||||||
@ -246,6 +269,7 @@ func (b *BeegoHTTPRequest) SetHost(host string) *BeegoHTTPRequest {
|
|||||||
|
|
||||||
// SetProtocolVersion Set the protocol version for incoming requests.
|
// SetProtocolVersion Set the protocol version for incoming requests.
|
||||||
// Client requests always use HTTP/1.1.
|
// Client requests always use HTTP/1.1.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) SetProtocolVersion(vers string) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) SetProtocolVersion(vers string) *BeegoHTTPRequest {
|
||||||
if len(vers) == 0 {
|
if len(vers) == 0 {
|
||||||
vers = "HTTP/1.1"
|
vers = "HTTP/1.1"
|
||||||
@ -262,12 +286,14 @@ func (b *BeegoHTTPRequest) SetProtocolVersion(vers string) *BeegoHTTPRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SetCookie add cookie into request.
|
// SetCookie add cookie into request.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) SetCookie(cookie *http.Cookie) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) SetCookie(cookie *http.Cookie) *BeegoHTTPRequest {
|
||||||
b.req.Header.Add("Cookie", cookie.String())
|
b.req.Header.Add("Cookie", cookie.String())
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetTransport set the setting transport
|
// SetTransport set the setting transport
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) SetTransport(transport http.RoundTripper) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) SetTransport(transport http.RoundTripper) *BeegoHTTPRequest {
|
||||||
b.setting.Transport = transport
|
b.setting.Transport = transport
|
||||||
return b
|
return b
|
||||||
@ -280,6 +306,7 @@ func (b *BeegoHTTPRequest) SetTransport(transport http.RoundTripper) *BeegoHTTPR
|
|||||||
// u, _ := url.ParseRequestURI("http://127.0.0.1:8118")
|
// u, _ := url.ParseRequestURI("http://127.0.0.1:8118")
|
||||||
// return u, nil
|
// return u, nil
|
||||||
// }
|
// }
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) SetProxy(proxy func(*http.Request) (*url.URL, error)) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) SetProxy(proxy func(*http.Request) (*url.URL, error)) *BeegoHTTPRequest {
|
||||||
b.setting.Proxy = proxy
|
b.setting.Proxy = proxy
|
||||||
return b
|
return b
|
||||||
@ -289,6 +316,7 @@ func (b *BeegoHTTPRequest) SetProxy(proxy func(*http.Request) (*url.URL, error))
|
|||||||
//
|
//
|
||||||
// If CheckRedirect is nil, the Client uses its default policy,
|
// If CheckRedirect is nil, the Client uses its default policy,
|
||||||
// which is to stop after 10 consecutive requests.
|
// which is to stop after 10 consecutive requests.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) SetCheckRedirect(redirect func(req *http.Request, via []*http.Request) error) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) SetCheckRedirect(redirect func(req *http.Request, via []*http.Request) error) *BeegoHTTPRequest {
|
||||||
b.setting.CheckRedirect = redirect
|
b.setting.CheckRedirect = redirect
|
||||||
return b
|
return b
|
||||||
@ -296,6 +324,7 @@ func (b *BeegoHTTPRequest) SetCheckRedirect(redirect func(req *http.Request, via
|
|||||||
|
|
||||||
// Param adds query param in to request.
|
// Param adds query param in to request.
|
||||||
// params build query string as ?key1=value1&key2=value2...
|
// params build query string as ?key1=value1&key2=value2...
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) Param(key, value string) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) Param(key, value string) *BeegoHTTPRequest {
|
||||||
if param, ok := b.params[key]; ok {
|
if param, ok := b.params[key]; ok {
|
||||||
b.params[key] = append(param, value)
|
b.params[key] = append(param, value)
|
||||||
@ -306,6 +335,7 @@ func (b *BeegoHTTPRequest) Param(key, value string) *BeegoHTTPRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PostFile add a post file to the request
|
// PostFile add a post file to the request
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) PostFile(formname, filename string) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) PostFile(formname, filename string) *BeegoHTTPRequest {
|
||||||
b.files[formname] = filename
|
b.files[formname] = filename
|
||||||
return b
|
return b
|
||||||
@ -313,6 +343,7 @@ func (b *BeegoHTTPRequest) PostFile(formname, filename string) *BeegoHTTPRequest
|
|||||||
|
|
||||||
// Body adds request raw body.
|
// Body adds request raw body.
|
||||||
// it supports string and []byte.
|
// it supports string and []byte.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) Body(data interface{}) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) Body(data interface{}) *BeegoHTTPRequest {
|
||||||
switch t := data.(type) {
|
switch t := data.(type) {
|
||||||
case string:
|
case string:
|
||||||
@ -328,6 +359,7 @@ func (b *BeegoHTTPRequest) Body(data interface{}) *BeegoHTTPRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// XMLBody adds request raw body encoding by XML.
|
// XMLBody adds request raw body encoding by XML.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) XMLBody(obj interface{}) (*BeegoHTTPRequest, error) {
|
func (b *BeegoHTTPRequest) XMLBody(obj interface{}) (*BeegoHTTPRequest, error) {
|
||||||
if b.req.Body == nil && obj != nil {
|
if b.req.Body == nil && obj != nil {
|
||||||
byts, err := xml.Marshal(obj)
|
byts, err := xml.Marshal(obj)
|
||||||
@ -342,6 +374,7 @@ func (b *BeegoHTTPRequest) XMLBody(obj interface{}) (*BeegoHTTPRequest, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// YAMLBody adds request raw body encoding by YAML.
|
// YAMLBody adds request raw body encoding by YAML.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) YAMLBody(obj interface{}) (*BeegoHTTPRequest, error) {
|
func (b *BeegoHTTPRequest) YAMLBody(obj interface{}) (*BeegoHTTPRequest, error) {
|
||||||
if b.req.Body == nil && obj != nil {
|
if b.req.Body == nil && obj != nil {
|
||||||
byts, err := yaml.Marshal(obj)
|
byts, err := yaml.Marshal(obj)
|
||||||
@ -356,6 +389,7 @@ func (b *BeegoHTTPRequest) YAMLBody(obj interface{}) (*BeegoHTTPRequest, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// JSONBody adds request raw body encoding by JSON.
|
// JSONBody adds request raw body encoding by JSON.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) JSONBody(obj interface{}) (*BeegoHTTPRequest, error) {
|
func (b *BeegoHTTPRequest) JSONBody(obj interface{}) (*BeegoHTTPRequest, error) {
|
||||||
if b.req.Body == nil && obj != nil {
|
if b.req.Body == nil && obj != nil {
|
||||||
byts, err := json.Marshal(obj)
|
byts, err := json.Marshal(obj)
|
||||||
@ -438,6 +472,7 @@ func (b *BeegoHTTPRequest) getResponse() (*http.Response, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DoRequest will do the client.Do
|
// DoRequest will do the client.Do
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) DoRequest() (resp *http.Response, err error) {
|
func (b *BeegoHTTPRequest) DoRequest() (resp *http.Response, err error) {
|
||||||
var paramBody string
|
var paramBody string
|
||||||
if len(b.params) > 0 {
|
if len(b.params) > 0 {
|
||||||
@ -531,6 +566,7 @@ func (b *BeegoHTTPRequest) DoRequest() (resp *http.Response, err error) {
|
|||||||
|
|
||||||
// String returns the body string in response.
|
// String returns the body string in response.
|
||||||
// it calls Response inner.
|
// it calls Response inner.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) String() (string, error) {
|
func (b *BeegoHTTPRequest) String() (string, error) {
|
||||||
data, err := b.Bytes()
|
data, err := b.Bytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -542,6 +578,7 @@ func (b *BeegoHTTPRequest) String() (string, error) {
|
|||||||
|
|
||||||
// Bytes returns the body []byte in response.
|
// Bytes returns the body []byte in response.
|
||||||
// it calls Response inner.
|
// it calls Response inner.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) Bytes() ([]byte, error) {
|
func (b *BeegoHTTPRequest) Bytes() ([]byte, error) {
|
||||||
if b.body != nil {
|
if b.body != nil {
|
||||||
return b.body, nil
|
return b.body, nil
|
||||||
@ -568,6 +605,7 @@ func (b *BeegoHTTPRequest) Bytes() ([]byte, error) {
|
|||||||
|
|
||||||
// ToFile saves the body data in response to one file.
|
// ToFile saves the body data in response to one file.
|
||||||
// it calls Response inner.
|
// it calls Response inner.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) ToFile(filename string) error {
|
func (b *BeegoHTTPRequest) ToFile(filename string) error {
|
||||||
resp, err := b.getResponse()
|
resp, err := b.getResponse()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -608,6 +646,7 @@ func pathExistAndMkdir(filename string) (err error) {
|
|||||||
|
|
||||||
// ToJSON returns the map that marshals from the body bytes as json in response .
|
// ToJSON returns the map that marshals from the body bytes as json in response .
|
||||||
// it calls Response inner.
|
// it calls Response inner.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) ToJSON(v interface{}) error {
|
func (b *BeegoHTTPRequest) ToJSON(v interface{}) error {
|
||||||
data, err := b.Bytes()
|
data, err := b.Bytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -618,6 +657,7 @@ func (b *BeegoHTTPRequest) ToJSON(v interface{}) error {
|
|||||||
|
|
||||||
// ToXML returns the map that marshals from the body bytes as xml in response .
|
// ToXML returns the map that marshals from the body bytes as xml in response .
|
||||||
// it calls Response inner.
|
// it calls Response inner.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) ToXML(v interface{}) error {
|
func (b *BeegoHTTPRequest) ToXML(v interface{}) error {
|
||||||
data, err := b.Bytes()
|
data, err := b.Bytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -628,6 +668,7 @@ func (b *BeegoHTTPRequest) ToXML(v interface{}) error {
|
|||||||
|
|
||||||
// ToYAML returns the map that marshals from the body bytes as yaml in response .
|
// ToYAML returns the map that marshals from the body bytes as yaml in response .
|
||||||
// it calls Response inner.
|
// it calls Response inner.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) ToYAML(v interface{}) error {
|
func (b *BeegoHTTPRequest) ToYAML(v interface{}) error {
|
||||||
data, err := b.Bytes()
|
data, err := b.Bytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -637,11 +678,13 @@ func (b *BeegoHTTPRequest) ToYAML(v interface{}) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Response executes request client gets response mannually.
|
// Response executes request client gets response mannually.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func (b *BeegoHTTPRequest) Response() (*http.Response, error) {
|
func (b *BeegoHTTPRequest) Response() (*http.Response, error) {
|
||||||
return b.getResponse()
|
return b.getResponse()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TimeoutDialer returns functions of connection dialer with timeout settings for http.Transport Dial field.
|
// TimeoutDialer returns functions of connection dialer with timeout settings for http.Transport Dial field.
|
||||||
|
// Deprecated: using pkg/httplib, we will delete this in v2.1.0
|
||||||
func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error) {
|
func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error) {
|
||||||
return func(netw, addr string) (net.Conn, error) {
|
return func(netw, addr string) (net.Conn, error) {
|
||||||
conn, err := net.DialTimeout(netw, addr, cTimeout)
|
conn, err := net.DialTimeout(netw, addr, cTimeout)
|
||||||
|
24
pkg/httplib/filter.go
Normal file
24
pkg/httplib/filter.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
// Copyright 2020 beego
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package httplib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FilterChain func(next Filter) Filter
|
||||||
|
|
||||||
|
type Filter func(ctx context.Context, req *BeegoHTTPRequest) (*http.Response, error)
|
73
pkg/httplib/filter/prometheus/filter.go
Normal file
73
pkg/httplib/filter/prometheus/filter.go
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
// Copyright 2020 beego
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package prometheus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
|
||||||
|
beego "github.com/astaxie/beego/pkg"
|
||||||
|
"github.com/astaxie/beego/pkg/httplib"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FilterChainBuilder struct {
|
||||||
|
summaryVec prometheus.ObserverVec
|
||||||
|
}
|
||||||
|
|
||||||
|
func (builder *FilterChainBuilder) FilterChain(next httplib.Filter) httplib.Filter {
|
||||||
|
|
||||||
|
builder.summaryVec = prometheus.NewSummaryVec(prometheus.SummaryOpts{
|
||||||
|
Name: "beego",
|
||||||
|
Subsystem: "remote_http_request",
|
||||||
|
ConstLabels: map[string]string{
|
||||||
|
"server": beego.BConfig.ServerName,
|
||||||
|
"env": beego.BConfig.RunMode,
|
||||||
|
"appname": beego.BConfig.AppName,
|
||||||
|
},
|
||||||
|
Help: "The statics info for remote http requests",
|
||||||
|
}, []string{"proto", "scheme", "method", "host", "path", "status", "duration", "isError"})
|
||||||
|
|
||||||
|
return func(ctx context.Context, req *httplib.BeegoHTTPRequest) (*http.Response, error) {
|
||||||
|
startTime := time.Now()
|
||||||
|
resp, err := next(ctx, req)
|
||||||
|
endTime := time.Now()
|
||||||
|
go builder.report(startTime, endTime, ctx, req, resp, err)
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (builder *FilterChainBuilder) report(startTime time.Time, endTime time.Time,
|
||||||
|
ctx context.Context, req *httplib.BeegoHTTPRequest, resp *http.Response, err error) {
|
||||||
|
|
||||||
|
proto := req.GetRequest().Proto
|
||||||
|
|
||||||
|
scheme := req.GetRequest().URL.Scheme
|
||||||
|
method := req.GetRequest().Method
|
||||||
|
|
||||||
|
host := req.GetRequest().URL.Host
|
||||||
|
path := req.GetRequest().URL.Path
|
||||||
|
|
||||||
|
status := resp.StatusCode
|
||||||
|
|
||||||
|
dur := int(endTime.Sub(startTime) / time.Millisecond)
|
||||||
|
|
||||||
|
|
||||||
|
builder.summaryVec.WithLabelValues(proto, scheme, method, host, path,
|
||||||
|
strconv.Itoa(status), strconv.Itoa(dur), strconv.FormatBool(err == nil))
|
||||||
|
}
|
41
pkg/httplib/filter/prometheus/filter_test.go
Normal file
41
pkg/httplib/filter/prometheus/filter_test.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
// Copyright 2020 beego
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package prometheus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/astaxie/beego/pkg/httplib"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFilterChainBuilder_FilterChain(t *testing.T) {
|
||||||
|
next := func(ctx context.Context, req *httplib.BeegoHTTPRequest) (*http.Response, error) {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
return &http.Response{
|
||||||
|
StatusCode: 404,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
builder := &FilterChainBuilder{}
|
||||||
|
filter := builder.FilterChain(next)
|
||||||
|
req := httplib.Get("https://github.com/notifications?query=repo%3Aastaxie%2Fbeego")
|
||||||
|
resp, err := filter(context.Background(), req)
|
||||||
|
assert.NotNil(t, resp)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
@ -34,6 +34,7 @@ package httplib
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
@ -66,6 +67,11 @@ var defaultSetting = BeegoHTTPSettings{
|
|||||||
var defaultCookieJar http.CookieJar
|
var defaultCookieJar http.CookieJar
|
||||||
var settingMutex sync.Mutex
|
var settingMutex sync.Mutex
|
||||||
|
|
||||||
|
// it will be the last filter and execute request.Do
|
||||||
|
var doRequestFilter = func(ctx context.Context, req *BeegoHTTPRequest) (*http.Response, error) {
|
||||||
|
return req.doRequest(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
// createDefaultCookie creates a global cookiejar to store cookies.
|
// createDefaultCookie creates a global cookiejar to store cookies.
|
||||||
func createDefaultCookie() {
|
func createDefaultCookie() {
|
||||||
settingMutex.Lock()
|
settingMutex.Lock()
|
||||||
@ -145,6 +151,7 @@ type BeegoHTTPSettings struct {
|
|||||||
DumpBody bool
|
DumpBody bool
|
||||||
Retries int // if set to -1 means will retry forever
|
Retries int // if set to -1 means will retry forever
|
||||||
RetryDelay time.Duration
|
RetryDelay time.Duration
|
||||||
|
FilterChains []FilterChain
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeegoHTTPRequest provides more useful methods than http.Request for requesting a url.
|
// BeegoHTTPRequest provides more useful methods than http.Request for requesting a url.
|
||||||
@ -295,6 +302,18 @@ func (b *BeegoHTTPRequest) SetCheckRedirect(redirect func(req *http.Request, via
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetFilters will use the filter as the invocation filters
|
||||||
|
func (b *BeegoHTTPRequest) SetFilters(fcs ...FilterChain) *BeegoHTTPRequest {
|
||||||
|
b.setting.FilterChains = fcs
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddFilters adds filter
|
||||||
|
func (b *BeegoHTTPRequest) AddFilters(fcs ...FilterChain) *BeegoHTTPRequest {
|
||||||
|
b.setting.FilterChains = append(b.setting.FilterChains, fcs...)
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
// Param adds query param in to request.
|
// Param adds query param in to request.
|
||||||
// params build query string as ?key1=value1&key2=value2...
|
// params build query string as ?key1=value1&key2=value2...
|
||||||
func (b *BeegoHTTPRequest) Param(key, value string) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) Param(key, value string) *BeegoHTTPRequest {
|
||||||
@ -397,7 +416,7 @@ func (b *BeegoHTTPRequest) buildURL(paramBody string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Httplib:", err)
|
log.Println("Httplib:", err)
|
||||||
}
|
}
|
||||||
//iocopy
|
// iocopy
|
||||||
_, err = io.Copy(fileWriter, fh)
|
_, err = io.Copy(fileWriter, fh)
|
||||||
fh.Close()
|
fh.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -440,6 +459,21 @@ func (b *BeegoHTTPRequest) getResponse() (*http.Response, error) {
|
|||||||
|
|
||||||
// DoRequest executes client.Do
|
// DoRequest executes client.Do
|
||||||
func (b *BeegoHTTPRequest) DoRequest() (resp *http.Response, err error) {
|
func (b *BeegoHTTPRequest) DoRequest() (resp *http.Response, err error) {
|
||||||
|
return b.DoRequestWithCtx(context.Background())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BeegoHTTPRequest) DoRequestWithCtx(ctx context.Context) (resp *http.Response, err error) {
|
||||||
|
|
||||||
|
root := doRequestFilter
|
||||||
|
if len(b.setting.FilterChains) > 0 {
|
||||||
|
for i := len(b.setting.FilterChains) - 1; i >= 0; i-- {
|
||||||
|
root = b.setting.FilterChains[i](root)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return root(ctx, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BeegoHTTPRequest) doRequest(ctx context.Context) (resp *http.Response, err error) {
|
||||||
var paramBody string
|
var paramBody string
|
||||||
if len(b.params) > 0 {
|
if len(b.params) > 0 {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
Loading…
Reference in New Issue
Block a user