This commit is contained in:
holooooo 2021-05-25 09:56:54 +08:00
parent 00b6c32dc2
commit 8a24192762
7 changed files with 13 additions and 16 deletions

View File

@ -1,6 +1,7 @@
# developing # developing
- Add http client and option func. [4455](https://github.com/beego/beego/issues/4455) - Add http client and option func. [4455](https://github.com/beego/beego/issues/4455)
- Add: Convenient way to generate mock object [4620](https://github.com/beego/beego/issues/4620)
- Infra: use dependabot to update dependencies. [4623](https://github.com/beego/beego/pull/4623) - Infra: use dependabot to update dependencies. [4623](https://github.com/beego/beego/pull/4623)
- Lint: use golangci-lint. [4619](https://github.com/beego/beego/pull/4619) - Lint: use golangci-lint. [4619](https://github.com/beego/beego/pull/4619)
- Chore: format code. [4615](https://github.com/beego/beego/pull/4615) - Chore: format code. [4615](https://github.com/beego/beego/pull/4615)

View File

@ -21,8 +21,10 @@ import (
"time" "time"
) )
type ClientOption func(client *Client) type (
type BeegoHTTPRequestOption func(request *BeegoHTTPRequest) ClientOption func(client *Client)
BeegoHTTPRequestOption func(request *BeegoHTTPRequest)
)
// WithEnableCookie will enable cookie in all subsequent request // WithEnableCookie will enable cookie in all subsequent request
func WithEnableCookie(enable bool) ClientOption { func WithEnableCookie(enable bool) ClientOption {

View File

@ -30,9 +30,9 @@ type Client struct {
Setting BeegoHTTPSettings Setting BeegoHTTPSettings
} }
// HTTPResponseCarrier If value implement HTTPResponseCarrier. http.Response will pass to SetHttpResponse // HTTPResponseCarrier If value implement HTTPResponseCarrier. http.Response will pass to SetHTTPResponse
type HTTPResponseCarrier interface { type HTTPResponseCarrier interface {
SetHttpResponse(resp *http.Response) SetHTTPResponse(resp *http.Response)
} }
// HTTPBodyCarrier If value implement HTTPBodyCarrier. http.Response.Body will pass to SetReader // HTTPBodyCarrier If value implement HTTPBodyCarrier. http.Response.Body will pass to SetReader
@ -52,7 +52,7 @@ type HTTPStatusCarrier interface {
} }
// HttpHeaderCarrier If value implement HttpHeaderCarrier. http.Response.Header will pass to SetHeader // HttpHeaderCarrier If value implement HttpHeaderCarrier. http.Response.Header will pass to SetHeader
type HttpHeadersCarrier interface { type HTTPHeadersCarrier interface {
SetHeader(header map[string][]string) SetHeader(header map[string][]string)
} }
@ -91,6 +91,7 @@ func (c *Client) handleResponse(value interface{}, req *BeegoHTTPRequest) error
// handleCarrier set http data to value // handleCarrier set http data to value
func (c *Client) handleCarrier(value interface{}, req *BeegoHTTPRequest) error { func (c *Client) handleCarrier(value interface{}, req *BeegoHTTPRequest) error {
resp, err := req.Response() resp, err := req.Response()
defer resp.Body.Close()
if err != nil { if err != nil {
return err return err
} }
@ -104,7 +105,7 @@ func (c *Client) handleCarrier(value interface{}, req *BeegoHTTPRequest) error {
return err return err
} }
resp.Body = ioutil.NopCloser(bytes.NewReader(b)) resp.Body = ioutil.NopCloser(bytes.NewReader(b))
carrier.SetHttpResponse(resp) carrier.SetHTTPResponse(resp)
} }
if carrier, ok := value.(HTTPBodyCarrier); ok { if carrier, ok := value.(HTTPBodyCarrier); ok {
b, err := req.Bytes() b, err := req.Bytes()
@ -124,7 +125,7 @@ func (c *Client) handleCarrier(value interface{}, req *BeegoHTTPRequest) error {
if carrier, ok := value.(HTTPStatusCarrier); ok { if carrier, ok := value.(HTTPStatusCarrier); ok {
carrier.SetStatusCode(resp.StatusCode) carrier.SetStatusCode(resp.StatusCode)
} }
if carrier, ok := value.(HttpHeadersCarrier); ok { if carrier, ok := value.(HTTPHeadersCarrier); ok {
carrier.SetHeader(resp.Header) carrier.SetHeader(resp.Header)
} }
return nil return nil

View File

@ -41,7 +41,7 @@ type slideShowResponse struct {
Slideshow slideshow `json:"slideshow" yaml:"slideshow"` Slideshow slideshow `json:"slideshow" yaml:"slideshow"`
} }
func (r *slideShowResponse) SetHttpResponse(resp *http.Response) { func (r *slideShowResponse) SetHTTPResponse(resp *http.Response) {
r.Resp = resp r.Resp = resp
} }

View File

@ -49,7 +49,6 @@ import (
"strings" "strings"
"time" "time"
xrv "github.com/mattermost/xml-roundtrip-validator"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"github.com/beego/beego/v2/core/berror" "github.com/beego/beego/v2/core/berror"
@ -639,10 +638,6 @@ func (b *BeegoHTTPRequest) ToXML(v interface{}) error {
if err != nil { if err != nil {
return err return err
} }
if err := xrv.Validate(bytes.NewReader(data)); err != nil {
return err
}
return berror.Wrap(xml.Unmarshal(data, v), return berror.Wrap(xml.Unmarshal(data, v),
UnmarshalXMLResponseToObjectFailed, "unmarshal xml body to object failed.") UnmarshalXMLResponseToObjectFailed, "unmarshal xml body to object failed.")
} }
@ -668,6 +663,7 @@ func (b *BeegoHTTPRequest) ToValue(value interface{}) error {
} }
resp, err := b.Response() resp, err := b.Response()
defer resp.Body.Close()
if err != nil { if err != nil {
return err return err
} }

1
go.mod
View File

@ -23,7 +23,6 @@ require (
github.com/hashicorp/golang-lru v0.5.4 github.com/hashicorp/golang-lru v0.5.4
github.com/ledisdb/ledisdb v0.0.0-20200510135210-d35789ec47e6 github.com/ledisdb/ledisdb v0.0.0-20200510135210-d35789ec47e6
github.com/lib/pq v1.10.2 github.com/lib/pq v1.10.2
github.com/mattermost/xml-roundtrip-validator v0.1.0
github.com/mattn/go-sqlite3 v1.14.7 github.com/mattn/go-sqlite3 v1.14.7
github.com/mitchellh/mapstructure v1.4.1 github.com/mitchellh/mapstructure v1.4.1
github.com/opentracing/opentracing-go v1.2.0 github.com/opentracing/opentracing-go v1.2.0

2
go.sum
View File

@ -226,8 +226,6 @@ github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
github.com/mattermost/xml-roundtrip-validator v0.1.0 h1:RXbVD2UAl7A7nOTR4u7E3ILa4IbtvKBHw64LDsmu9hU=
github.com/mattermost/xml-roundtrip-validator v0.1.0/go.mod h1:qccnGMcpgwcNaBnxqpJpWWUiPNr5H3O8eDgGV9gT5To=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=