feat: add NewBeegoRequestWithCtx
This commit is contained in:
parent
3ae30a6beb
commit
fd84973f67
@ -2,7 +2,8 @@
|
|||||||
- [upgrade redisgo to v1.8.8](https://github.com/beego/beego/pull/4872)
|
- [upgrade redisgo to v1.8.8](https://github.com/beego/beego/pull/4872)
|
||||||
- [fix prometheus CVE-2022-21698](https://github.com/beego/beego/pull/4878)
|
- [fix prometheus CVE-2022-21698](https://github.com/beego/beego/pull/4878)
|
||||||
- [upgrade to Go 1.18](https://github.com/beego/beego/pull/4896)
|
- [upgrade to Go 1.18](https://github.com/beego/beego/pull/4896)
|
||||||
|
- [Support NewBeegoRequestWithCtx in httplib](https://github.com/beego/beego/pull/4895)
|
||||||
|
|
||||||
# v2.0.2
|
# v2.0.2
|
||||||
See v2.0.2-beta.1
|
See v2.0.2-beta.1
|
||||||
- [fix bug: etcd should use etcd as adapter name](https://github.com/beego/beego/pull/4845)
|
- [fix bug: etcd should use etcd as adapter name](https://github.com/beego/beego/pull/4845)
|
||||||
|
|||||||
@ -51,6 +51,10 @@ Sometimes you got JSON document and you want to make it as request body. So you
|
|||||||
If you do this, you got this code. Instead, you should call Header to set Content-type and call Body to set body data.
|
If you do this, you got this code. Instead, you should call Header to set Content-type and call Body to set body data.
|
||||||
`)
|
`)
|
||||||
|
|
||||||
|
var InvalidURLOrMethod = berror.DefineCode(4001007, moduleName, "InvalidURLOrMethod", `
|
||||||
|
You pass invalid url or method to httplib module. Please check the url and method, be careful about special characters.
|
||||||
|
`)
|
||||||
|
|
||||||
// start with 5 --------------------------------------------------------------------------
|
// start with 5 --------------------------------------------------------------------------
|
||||||
|
|
||||||
var CreateFormFileFailed = berror.DefineCode(5001001, moduleName, "CreateFormFileFailed", `
|
var CreateFormFileFailed = berror.DefineCode(5001001, moduleName, "CreateFormFileFailed", `
|
||||||
|
|||||||
@ -67,26 +67,23 @@ var doRequestFilter = func(ctx context.Context, req *BeegoHTTPRequest) (*http.Re
|
|||||||
// I think if we don't return error
|
// I think if we don't return error
|
||||||
// users are hard to check whether we create Beego request successfully
|
// users are hard to check whether we create Beego request successfully
|
||||||
func NewBeegoRequest(rawurl, method string) *BeegoHTTPRequest {
|
func NewBeegoRequest(rawurl, method string) *BeegoHTTPRequest {
|
||||||
var resp http.Response
|
return NewBeegoRequestWithCtx(context.Background(), rawurl, method)
|
||||||
u, err := url.Parse(rawurl)
|
}
|
||||||
|
|
||||||
|
// NewBeegoRequestWithCtx returns a new BeegoHTTPRequest given a method, URL
|
||||||
|
func NewBeegoRequestWithCtx(ctx context.Context, rawurl, method string) *BeegoHTTPRequest {
|
||||||
|
req, err := http.NewRequestWithContext(ctx, method, rawurl, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Error("%+v", berror.Wrapf(err, InvalidUrl, "invalid raw url: %s", rawurl))
|
logs.Error("%+v", berror.Wrapf(err, InvalidURLOrMethod, "invalid raw url or method: %s %s", rawurl, method))
|
||||||
}
|
|
||||||
req := http.Request{
|
|
||||||
URL: u,
|
|
||||||
Method: method,
|
|
||||||
Header: make(http.Header),
|
|
||||||
Proto: "HTTP/1.1",
|
|
||||||
ProtoMajor: 1,
|
|
||||||
ProtoMinor: 1,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &BeegoHTTPRequest{
|
return &BeegoHTTPRequest{
|
||||||
url: rawurl,
|
url: rawurl,
|
||||||
req: &req,
|
req: req,
|
||||||
params: map[string][]string{},
|
params: map[string][]string{},
|
||||||
files: map[string]string{},
|
files: map[string]string{},
|
||||||
setting: defaultSetting,
|
setting: defaultSetting,
|
||||||
resp: &resp,
|
resp: &http.Response{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -409,7 +406,7 @@ func (b *BeegoHTTPRequest) handleFiles() {
|
|||||||
b.Header("Transfer-Encoding", "chunked")
|
b.Header("Transfer-Encoding", "chunked")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BeegoHTTPRequest) handleFileToBody(bodyWriter *multipart.Writer, formname string, filename string) {
|
func (*BeegoHTTPRequest) handleFileToBody(bodyWriter *multipart.Writer, formname string, filename string) {
|
||||||
fileWriter, err := bodyWriter.CreateFormFile(formname, filename)
|
fileWriter, err := bodyWriter.CreateFormFile(formname, filename)
|
||||||
const errFmt = "Httplib: %+v"
|
const errFmt = "Httplib: %+v"
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -445,9 +442,16 @@ 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())
|
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(b.req.Context(), b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: please use NewBeegoRequestWithContext
|
||||||
func (b *BeegoHTTPRequest) DoRequestWithCtx(ctx context.Context) (resp *http.Response, err error) {
|
func (b *BeegoHTTPRequest) DoRequestWithCtx(ctx context.Context) (resp *http.Response, err error) {
|
||||||
root := doRequestFilter
|
root := doRequestFilter
|
||||||
if len(b.setting.FilterChains) > 0 {
|
if len(b.setting.FilterChains) > 0 {
|
||||||
@ -458,7 +462,7 @@ func (b *BeegoHTTPRequest) DoRequestWithCtx(ctx context.Context) (resp *http.Res
|
|||||||
return root(ctx, b)
|
return root(ctx, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BeegoHTTPRequest) doRequest(ctx context.Context) (*http.Response, error) {
|
func (b *BeegoHTTPRequest) doRequest(_ context.Context) (*http.Response, error) {
|
||||||
paramBody := b.buildParamBody()
|
paramBody := b.buildParamBody()
|
||||||
|
|
||||||
b.buildURL(paramBody)
|
b.buildURL(paramBody)
|
||||||
|
|||||||
@ -350,6 +350,20 @@ func TestNewBeegoRequest(t *testing.T) {
|
|||||||
assert.NotNil(t, req)
|
assert.NotNil(t, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewBeegoRequestWithCtx(t *testing.T) {
|
||||||
|
req := NewBeegoRequestWithCtx(context.Background(), "http://beego.vip", "GET")
|
||||||
|
assert.NotNil(t, req)
|
||||||
|
assert.Equal(t, "GET", req.req.Method)
|
||||||
|
|
||||||
|
// bad url but still get request
|
||||||
|
req = NewBeegoRequestWithCtx(context.Background(), "httpa\ta://beego.vip", "GET")
|
||||||
|
assert.NotNil(t, req)
|
||||||
|
|
||||||
|
// bad method but still get request
|
||||||
|
req = NewBeegoRequestWithCtx(context.Background(), "http://beego.vip", "G\tET")
|
||||||
|
assert.NotNil(t, req)
|
||||||
|
}
|
||||||
|
|
||||||
func TestBeegoHTTPRequestSetProtocolVersion(t *testing.T) {
|
func TestBeegoHTTPRequestSetProtocolVersion(t *testing.T) {
|
||||||
req := NewBeegoRequest("http://beego.vip", "GET")
|
req := NewBeegoRequest("http://beego.vip", "GET")
|
||||||
assert.Equal(t, 1, req.req.ProtoMajor)
|
assert.Equal(t, 1, req.req.ProtoMajor)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user