gclient_chain.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "time"
  9. )
  10. // Prefix is a chaining function,
  11. // which sets the URL prefix for next request of this client.
  12. // Eg:
  13. // Prefix("http://127.0.0.1:8199/api/v1")
  14. // Prefix("http://127.0.0.1:8199/api/v2")
  15. func (c *Client) Prefix(prefix string) *Client {
  16. newClient := c.Clone()
  17. newClient.SetPrefix(prefix)
  18. return newClient
  19. }
  20. // Header is a chaining function,
  21. // which sets custom HTTP headers with map for next request.
  22. func (c *Client) Header(m map[string]string) *Client {
  23. newClient := c.Clone()
  24. newClient.SetHeaderMap(m)
  25. return newClient
  26. }
  27. // HeaderRaw is a chaining function,
  28. // which sets custom HTTP header using raw string for next request.
  29. func (c *Client) HeaderRaw(headers string) *Client {
  30. newClient := c.Clone()
  31. newClient.SetHeaderRaw(headers)
  32. return newClient
  33. }
  34. // Cookie is a chaining function,
  35. // which sets cookie items with map for next request.
  36. func (c *Client) Cookie(m map[string]string) *Client {
  37. newClient := c.Clone()
  38. newClient.SetCookieMap(m)
  39. return newClient
  40. }
  41. // ContentType is a chaining function,
  42. // which sets HTTP content type for the next request.
  43. func (c *Client) ContentType(contentType string) *Client {
  44. newClient := c.Clone()
  45. newClient.SetContentType(contentType)
  46. return newClient
  47. }
  48. // ContentJson is a chaining function,
  49. // which sets the HTTP content type as "application/json" for the next request.
  50. //
  51. // Note that it also checks and encodes the parameter to JSON format automatically.
  52. func (c *Client) ContentJson() *Client {
  53. newClient := c.Clone()
  54. newClient.SetContentType(httpHeaderContentTypeJson)
  55. return newClient
  56. }
  57. // ContentXml is a chaining function,
  58. // which sets the HTTP content type as "application/xml" for the next request.
  59. //
  60. // Note that it also checks and encodes the parameter to XML format automatically.
  61. func (c *Client) ContentXml() *Client {
  62. newClient := c.Clone()
  63. newClient.SetContentType(httpHeaderContentTypeXml)
  64. return newClient
  65. }
  66. // Timeout is a chaining function,
  67. // which sets the timeout for next request.
  68. func (c *Client) Timeout(t time.Duration) *Client {
  69. newClient := c.Clone()
  70. newClient.SetTimeout(t)
  71. return newClient
  72. }
  73. // BasicAuth is a chaining function,
  74. // which sets HTTP basic authentication information for next request.
  75. func (c *Client) BasicAuth(user, pass string) *Client {
  76. newClient := c.Clone()
  77. newClient.SetBasicAuth(user, pass)
  78. return newClient
  79. }
  80. // Retry is a chaining function,
  81. // which sets retry count and interval when failure for next request.
  82. func (c *Client) Retry(retryCount int, retryInterval time.Duration) *Client {
  83. newClient := c.Clone()
  84. newClient.SetRetry(retryCount, retryInterval)
  85. return newClient
  86. }
  87. // Proxy is a chaining function,
  88. // which sets proxy for next request.
  89. // Make sure you pass the correct `proxyURL`.
  90. // The correct pattern is like `http://USER:PASSWORD@IP:PORT` or `socks5://USER:PASSWORD@IP:PORT`.
  91. // Only `http` and `socks5` proxies are supported currently.
  92. func (c *Client) Proxy(proxyURL string) *Client {
  93. newClient := c.Clone()
  94. newClient.SetProxy(proxyURL)
  95. return newClient
  96. }
  97. // RedirectLimit is a chaining function,
  98. // which sets the redirect limit the number of jumps for the request.
  99. func (c *Client) RedirectLimit(redirectLimit int) *Client {
  100. newClient := c.Clone()
  101. newClient.SetRedirectLimit(redirectLimit)
  102. return newClient
  103. }