github.com/gogf/gf@v1.16.9/net/ghttp/internal/client/client_chain.go (about)

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