gclient_var.go 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gclient
  7. import (
  8. "context"
  9. "net/http"
  10. "github.com/gogf/gf/v2/container/gvar"
  11. )
  12. // GetVar sends a GET request, retrieves and converts the result content to specified pointer.
  13. // The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  14. func (c *Client) GetVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
  15. return c.RequestVar(ctx, http.MethodGet, url, data...)
  16. }
  17. // PutVar sends a PUT request, retrieves and converts the result content to specified pointer.
  18. // The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  19. func (c *Client) PutVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
  20. return c.RequestVar(ctx, http.MethodPut, url, data...)
  21. }
  22. // PostVar sends a POST request, retrieves and converts the result content to specified pointer.
  23. // The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  24. func (c *Client) PostVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
  25. return c.RequestVar(ctx, http.MethodPost, url, data...)
  26. }
  27. // DeleteVar sends a DELETE request, retrieves and converts the result content to specified pointer.
  28. // The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  29. func (c *Client) DeleteVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
  30. return c.RequestVar(ctx, http.MethodDelete, url, data...)
  31. }
  32. // HeadVar sends a HEAD request, retrieves and converts the result content to specified pointer.
  33. // The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  34. func (c *Client) HeadVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
  35. return c.RequestVar(ctx, http.MethodHead, url, data...)
  36. }
  37. // PatchVar sends a PATCH request, retrieves and converts the result content to specified pointer.
  38. // The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  39. func (c *Client) PatchVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
  40. return c.RequestVar(ctx, http.MethodPatch, url, data...)
  41. }
  42. // ConnectVar sends a CONNECT request, retrieves and converts the result content to specified pointer.
  43. // The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  44. func (c *Client) ConnectVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
  45. return c.RequestVar(ctx, http.MethodConnect, url, data...)
  46. }
  47. // OptionsVar sends a OPTIONS request, retrieves and converts the result content to specified pointer.
  48. // The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  49. func (c *Client) OptionsVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
  50. return c.RequestVar(ctx, http.MethodOptions, url, data...)
  51. }
  52. // TraceVar sends a TRACE request, retrieves and converts the result content to specified pointer.
  53. // The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  54. func (c *Client) TraceVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
  55. return c.RequestVar(ctx, http.MethodTrace, url, data...)
  56. }
  57. // RequestVar sends request using given HTTP method and data, retrieves converts the result
  58. // to specified pointer. It reads and closes the response object internally automatically.
  59. // The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  60. func (c *Client) RequestVar(ctx context.Context, method string, url string, data ...interface{}) *gvar.Var {
  61. response, err := c.DoRequest(ctx, method, url, data...)
  62. if err != nil {
  63. return gvar.New(nil)
  64. }
  65. defer response.Close()
  66. return gvar.New(response.ReadAll())
  67. }