gclient_chain.go 4.0 KB

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