ghttp_client_chain.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2020 gf Author(https://github.com/gogf/gf). 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 ghttp
  7. import (
  8. "context"
  9. "time"
  10. )
  11. // Prefix is a chaining function,
  12. // which sets the URL prefix for next request of this client.
  13. func (c *Client) Prefix(prefix string) *Client {
  14. newClient := c
  15. if c.parent == nil {
  16. newClient = c.Clone()
  17. }
  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
  25. if c.parent == nil {
  26. newClient = c.Clone()
  27. }
  28. newClient.SetHeaderMap(m)
  29. return newClient
  30. }
  31. // Header is a chaining function,
  32. // which sets custom HTTP header using raw string for next request.
  33. func (c *Client) HeaderRaw(headers string) *Client {
  34. newClient := c
  35. if c.parent == nil {
  36. newClient = c.Clone()
  37. }
  38. newClient.SetHeaderRaw(headers)
  39. return newClient
  40. }
  41. // Cookie is a chaining function,
  42. // which sets cookie items with map for next request.
  43. func (c *Client) Cookie(m map[string]string) *Client {
  44. newClient := c
  45. if c.parent == nil {
  46. newClient = c.Clone()
  47. }
  48. newClient.SetCookieMap(m)
  49. return newClient
  50. }
  51. // ContentType is a chaining function,
  52. // which sets HTTP content type for the next request.
  53. func (c *Client) ContentType(contentType string) *Client {
  54. newClient := c
  55. if c.parent == nil {
  56. newClient = c.Clone()
  57. }
  58. newClient.SetContentType(contentType)
  59. return newClient
  60. }
  61. // ContentJson is a chaining function,
  62. // which sets the HTTP content type as "application/json" for the next request.
  63. //
  64. // Note that it also checks and encodes the parameter to JSON format automatically.
  65. func (c *Client) ContentJson() *Client {
  66. newClient := c
  67. if c.parent == nil {
  68. newClient = c.Clone()
  69. }
  70. newClient.SetContentType("application/json")
  71. return newClient
  72. }
  73. // ContentXml is a chaining function,
  74. // which sets the HTTP content type as "application/xml" for the next request.
  75. //
  76. // Note that it also checks and encodes the parameter to XML format automatically.
  77. func (c *Client) ContentXml() *Client {
  78. newClient := c
  79. if c.parent == nil {
  80. newClient = c.Clone()
  81. }
  82. newClient.SetContentType("application/xml")
  83. return newClient
  84. }
  85. // TimeOut is a chaining function,
  86. // which sets the timeout for next request.
  87. func (c *Client) Timeout(t time.Duration) *Client {
  88. newClient := c
  89. if c.parent == nil {
  90. newClient = c.Clone()
  91. }
  92. newClient.SetTimeout(t)
  93. return newClient
  94. }
  95. // BasicAuth is a chaining function,
  96. // which sets HTTP basic authentication information for next request.
  97. func (c *Client) BasicAuth(user, pass string) *Client {
  98. newClient := c
  99. if c.parent == nil {
  100. newClient = c.Clone()
  101. }
  102. newClient.SetBasicAuth(user, pass)
  103. return newClient
  104. }
  105. // Ctx is a chaining function,
  106. // which sets context for next request of this client.
  107. func (c *Client) Ctx(ctx context.Context) *Client {
  108. newClient := c
  109. if c.parent == nil {
  110. newClient = c.Clone()
  111. }
  112. newClient.SetCtx(ctx)
  113. return newClient
  114. }
  115. // Retry is a chaining function,
  116. // which sets retry count and interval when failure for next request.
  117. func (c *Client) Retry(retryCount int, retryInterval time.Duration) *Client {
  118. newClient := c
  119. if c.parent == nil {
  120. newClient = c.Clone()
  121. }
  122. newClient.SetRetry(retryCount, retryInterval)
  123. return c
  124. }
  125. // Proxy is a chaining function,
  126. // which sets proxy for next request.
  127. // Make sure you pass the correct `proxyURL`.
  128. // The correct pattern is like `http://USER:PASSWORD@IP:PORT` or `socks5://USER:PASSWORD@IP:PORT`.
  129. // Only `http` and `socks5` proxies are supported currently.
  130. func (c *Client) Proxy(proxyURL string) *Client {
  131. newClient := c
  132. if c.parent == nil {
  133. newClient = c.Clone()
  134. }
  135. newClient.SetProxy(proxyURL)
  136. return c
  137. }
  138. // RedirectLimit is a chaining function,
  139. // which sets the redirect limit the number of jumps for the request.
  140. func (c *Client) RedirectLimit(redirectLimit int) *Client {
  141. newClient := c
  142. if c.parent == nil {
  143. newClient = c.Clone()
  144. }
  145. newClient.SetRedirectLimit(redirectLimit)
  146. return c
  147. }