add internal error logging and update comments for gclient.RequestVar (#3270)

This commit is contained in:
John Guo 2024-01-18 10:21:38 +08:00 committed by GitHub
parent e8d3fcac6b
commit 73fbca40ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 24 deletions

View File

@ -31,14 +31,16 @@ import (
// //
// The response object `res` should be a pointer type. It automatically converts result // The response object `res` should be a pointer type. It automatically converts result
// to given object `res` is success. // to given object `res` is success.
// Eg: //
// Example:
// var ( // var (
// //
// req = UseCreateReq{} // req = UseCreateReq{}
// res *UseCreateRes // res *UseCreateRes
// //
// ) // )
// DoRequestObj(ctx, req, &res) //
// err := DoRequestObj(ctx, req, &res)
func (c *Client) DoRequestObj(ctx context.Context, req, res interface{}) error { func (c *Client) DoRequestObj(ctx context.Context, req, res interface{}) error {
var ( var (
method = gmeta.Get(req, gtag.Method).String() method = gmeta.Get(req, gtag.Method).String()

View File

@ -11,70 +11,85 @@ import (
"net/http" "net/http"
"github.com/gogf/gf/v2/container/gvar" "github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/internal/intlog"
) )
// GetVar sends a GET request, retrieves and converts the result content to specified pointer. // GetVar sends a GET request, retrieves and converts the result content to *gvar.Var.
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc. // The client reads and closes the response object internally automatically.
// The result *gvar.Var can be conveniently converted to any type you want.
func (c *Client) GetVar(ctx context.Context, url string, data ...interface{}) *gvar.Var { func (c *Client) GetVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
return c.RequestVar(ctx, http.MethodGet, url, data...) return c.RequestVar(ctx, http.MethodGet, url, data...)
} }
// PutVar sends a PUT request, retrieves and converts the result content to specified pointer. // PutVar sends a PUT request, retrieves and converts the result content to *gvar.Var.
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc. // The client reads and closes the response object internally automatically.
// The result *gvar.Var can be conveniently converted to any type you want.
func (c *Client) PutVar(ctx context.Context, url string, data ...interface{}) *gvar.Var { func (c *Client) PutVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
return c.RequestVar(ctx, http.MethodPut, url, data...) return c.RequestVar(ctx, http.MethodPut, url, data...)
} }
// PostVar sends a POST request, retrieves and converts the result content to specified pointer. // PostVar sends a POST request, retrieves and converts the result content to *gvar.Var.
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc. // The client reads and closes the response object internally automatically.
// The result *gvar.Var can be conveniently converted to any type you want.
func (c *Client) PostVar(ctx context.Context, url string, data ...interface{}) *gvar.Var { func (c *Client) PostVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
return c.RequestVar(ctx, http.MethodPost, url, data...) return c.RequestVar(ctx, http.MethodPost, url, data...)
} }
// DeleteVar sends a DELETE request, retrieves and converts the result content to specified pointer. // DeleteVar sends a DELETE request, retrieves and converts the result content to *gvar.Var.
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc. // The client reads and closes the response object internally automatically.
// The result *gvar.Var can be conveniently converted to any type you want.
func (c *Client) DeleteVar(ctx context.Context, url string, data ...interface{}) *gvar.Var { func (c *Client) DeleteVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
return c.RequestVar(ctx, http.MethodDelete, url, data...) return c.RequestVar(ctx, http.MethodDelete, url, data...)
} }
// HeadVar sends a HEAD request, retrieves and converts the result content to specified pointer. // HeadVar sends a HEAD request, retrieves and converts the result content to *gvar.Var.
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc. // The client reads and closes the response object internally automatically.
// The result *gvar.Var can be conveniently converted to any type you want.
func (c *Client) HeadVar(ctx context.Context, url string, data ...interface{}) *gvar.Var { func (c *Client) HeadVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
return c.RequestVar(ctx, http.MethodHead, url, data...) return c.RequestVar(ctx, http.MethodHead, url, data...)
} }
// PatchVar sends a PATCH request, retrieves and converts the result content to specified pointer. // PatchVar sends a PATCH request, retrieves and converts the result content to *gvar.Var.
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc. // The client reads and closes the response object internally automatically.
// The result *gvar.Var can be conveniently converted to any type you want.
func (c *Client) PatchVar(ctx context.Context, url string, data ...interface{}) *gvar.Var { func (c *Client) PatchVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
return c.RequestVar(ctx, http.MethodPatch, url, data...) return c.RequestVar(ctx, http.MethodPatch, url, data...)
} }
// ConnectVar sends a CONNECT request, retrieves and converts the result content to specified pointer. // ConnectVar sends a CONNECT request, retrieves and converts the result content to *gvar.Var.
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc. // The client reads and closes the response object internally automatically.
// The result *gvar.Var can be conveniently converted to any type you want.
func (c *Client) ConnectVar(ctx context.Context, url string, data ...interface{}) *gvar.Var { func (c *Client) ConnectVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
return c.RequestVar(ctx, http.MethodConnect, url, data...) return c.RequestVar(ctx, http.MethodConnect, url, data...)
} }
// OptionsVar sends an OPTIONS request, retrieves and converts the result content to specified pointer. // OptionsVar sends an OPTIONS request, retrieves and converts the result content to *gvar.Var.
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc. // The client reads and closes the response object internally automatically.
// The result *gvar.Var can be conveniently converted to any type you want.
func (c *Client) OptionsVar(ctx context.Context, url string, data ...interface{}) *gvar.Var { func (c *Client) OptionsVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
return c.RequestVar(ctx, http.MethodOptions, url, data...) return c.RequestVar(ctx, http.MethodOptions, url, data...)
} }
// TraceVar sends a TRACE request, retrieves and converts the result content to specified pointer. // TraceVar sends a TRACE request, retrieves and converts the result content to *gvar.Var.
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc. // The client reads and closes the response object internally automatically.
// The result *gvar.Var can be conveniently converted to any type you want.
func (c *Client) TraceVar(ctx context.Context, url string, data ...interface{}) *gvar.Var { func (c *Client) TraceVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
return c.RequestVar(ctx, http.MethodTrace, url, data...) return c.RequestVar(ctx, http.MethodTrace, url, data...)
} }
// RequestVar sends request using given HTTP method and data, retrieves converts the result // RequestVar sends request using given HTTP method and data, retrieves converts the result to *gvar.Var.
// to specified pointer. It reads and closes the response object internally automatically. // The client reads and closes the response object internally automatically.
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc. // The result *gvar.Var can be conveniently converted to any type you want.
func (c *Client) RequestVar(ctx context.Context, method string, url string, data ...interface{}) *gvar.Var { func (c *Client) RequestVar(ctx context.Context, method string, url string, data ...interface{}) *gvar.Var {
response, err := c.DoRequest(ctx, method, url, data...) response, err := c.DoRequest(ctx, method, url, data...)
if err != nil { if err != nil {
intlog.Errorf(ctx, `%+v`, err)
return gvar.New(nil) return gvar.New(nil)
} }
defer response.Close() defer func() {
if err = response.Close(); err != nil {
intlog.Errorf(ctx, `%+v`, err)
}
}()
return gvar.New(response.ReadAll()) return gvar.New(response.ReadAll())
} }