github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_client.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 ghttp
     8  
     9  import (
    10  	"github.com/gogf/gf/container/gvar"
    11  	"github.com/gogf/gf/net/ghttp/internal/client"
    12  )
    13  
    14  type (
    15  	Client            = client.Client
    16  	ClientResponse    = client.Response
    17  	ClientHandlerFunc = client.HandlerFunc
    18  )
    19  
    20  // NewClient creates and returns a new HTTP client object.
    21  func NewClient() *Client {
    22  	return client.New()
    23  }
    24  
    25  // Get is a convenience method for sending GET request.
    26  // NOTE that remembers CLOSING the response object when it'll never be used.
    27  // Deprecated, please use g.Client().Get or NewClient().Get instead.
    28  func Get(url string, data ...interface{}) (*ClientResponse, error) {
    29  	return DoRequest("GET", url, data...)
    30  }
    31  
    32  // Put is a convenience method for sending PUT request.
    33  // NOTE that remembers CLOSING the response object when it'll never be used.
    34  // Deprecated, please use g.Client().Put or NewClient().Put instead.
    35  func Put(url string, data ...interface{}) (*ClientResponse, error) {
    36  	return DoRequest("PUT", url, data...)
    37  }
    38  
    39  // Post is a convenience method for sending POST request.
    40  // NOTE that remembers CLOSING the response object when it'll never be used.
    41  // Deprecated, please use g.Client().Post or NewClient().Post instead.
    42  func Post(url string, data ...interface{}) (*ClientResponse, error) {
    43  	return DoRequest("POST", url, data...)
    44  }
    45  
    46  // Delete is a convenience method for sending DELETE request.
    47  // NOTE that remembers CLOSING the response object when it'll never be used.
    48  // Deprecated, please use g.Client().Delete or NewClient().Delete instead.
    49  func Delete(url string, data ...interface{}) (*ClientResponse, error) {
    50  	return DoRequest("DELETE", url, data...)
    51  }
    52  
    53  // Head is a convenience method for sending HEAD request.
    54  // NOTE that remembers CLOSING the response object when it'll never be used.
    55  // Deprecated, please use g.Client().Head or NewClient().Head instead.
    56  func Head(url string, data ...interface{}) (*ClientResponse, error) {
    57  	return DoRequest("HEAD", url, data...)
    58  }
    59  
    60  // Patch is a convenience method for sending PATCH request.
    61  // NOTE that remembers CLOSING the response object when it'll never be used.
    62  // Deprecated, please use g.Client().Patch or NewClient().Patch instead.
    63  func Patch(url string, data ...interface{}) (*ClientResponse, error) {
    64  	return DoRequest("PATCH", url, data...)
    65  }
    66  
    67  // Connect is a convenience method for sending CONNECT request.
    68  // NOTE that remembers CLOSING the response object when it'll never be used.
    69  // Deprecated, please use g.Client().Connect or NewClient().Connect instead.
    70  func Connect(url string, data ...interface{}) (*ClientResponse, error) {
    71  	return DoRequest("CONNECT", url, data...)
    72  }
    73  
    74  // Options is a convenience method for sending OPTIONS request.
    75  // NOTE that remembers CLOSING the response object when it'll never be used.
    76  // Deprecated, please use g.Client().Options or NewClient().Options instead.
    77  func Options(url string, data ...interface{}) (*ClientResponse, error) {
    78  	return DoRequest("OPTIONS", url, data...)
    79  }
    80  
    81  // Trace is a convenience method for sending TRACE request.
    82  // NOTE that remembers CLOSING the response object when it'll never be used.
    83  // Deprecated, please use g.Client().Trace or NewClient().Trace instead.
    84  func Trace(url string, data ...interface{}) (*ClientResponse, error) {
    85  	return DoRequest("TRACE", url, data...)
    86  }
    87  
    88  // DoRequest is a convenience method for sending custom http method request.
    89  // NOTE that remembers CLOSING the response object when it'll never be used.
    90  // Deprecated, please use g.Client().DoRequest or NewClient().DoRequest instead.
    91  func DoRequest(method, url string, data ...interface{}) (*ClientResponse, error) {
    92  	return client.New().DoRequest(method, url, data...)
    93  }
    94  
    95  // GetContent is a convenience method for sending GET request, which retrieves and returns
    96  // the result content and automatically closes response object.
    97  // Deprecated, please use g.Client().GetContent or NewClient().GetContent instead.
    98  func GetContent(url string, data ...interface{}) string {
    99  	return RequestContent("GET", url, data...)
   100  }
   101  
   102  // PutContent is a convenience method for sending PUT request, which retrieves and returns
   103  // the result content and automatically closes response object.
   104  // Deprecated, please use g.Client().PutContent or NewClient().PutContent instead.
   105  func PutContent(url string, data ...interface{}) string {
   106  	return RequestContent("PUT", url, data...)
   107  }
   108  
   109  // PostContent is a convenience method for sending POST request, which retrieves and returns
   110  // the result content and automatically closes response object.
   111  // Deprecated, please use g.Client().PostContent or NewClient().PostContent instead.
   112  func PostContent(url string, data ...interface{}) string {
   113  	return RequestContent("POST", url, data...)
   114  }
   115  
   116  // DeleteContent is a convenience method for sending DELETE request, which retrieves and returns
   117  // the result content and automatically closes response object.
   118  // Deprecated, please use g.Client().DeleteContent or NewClient().DeleteContent instead.
   119  func DeleteContent(url string, data ...interface{}) string {
   120  	return RequestContent("DELETE", url, data...)
   121  }
   122  
   123  // HeadContent is a convenience method for sending HEAD request, which retrieves and returns
   124  // the result content and automatically closes response object.
   125  // Deprecated, please use g.Client().HeadContent or NewClient().HeadContent instead.
   126  func HeadContent(url string, data ...interface{}) string {
   127  	return RequestContent("HEAD", url, data...)
   128  }
   129  
   130  // PatchContent is a convenience method for sending PATCH request, which retrieves and returns
   131  // the result content and automatically closes response object.
   132  // Deprecated, please use g.Client().PatchContent or NewClient().PatchContent instead.
   133  func PatchContent(url string, data ...interface{}) string {
   134  	return RequestContent("PATCH", url, data...)
   135  }
   136  
   137  // ConnectContent is a convenience method for sending CONNECT request, which retrieves and returns
   138  // the result content and automatically closes response object.
   139  // Deprecated, please use g.Client().ConnectContent or NewClient().ConnectContent instead.
   140  func ConnectContent(url string, data ...interface{}) string {
   141  	return RequestContent("CONNECT", url, data...)
   142  }
   143  
   144  // OptionsContent is a convenience method for sending OPTIONS request, which retrieves and returns
   145  // the result content and automatically closes response object.
   146  // Deprecated, please use g.Client().OptionsContent or NewClient().OptionsContent instead.
   147  func OptionsContent(url string, data ...interface{}) string {
   148  	return RequestContent("OPTIONS", url, data...)
   149  }
   150  
   151  // TraceContent is a convenience method for sending TRACE request, which retrieves and returns
   152  // the result content and automatically closes response object.
   153  // Deprecated, please use g.Client().TraceContent or NewClient().TraceContent instead.
   154  func TraceContent(url string, data ...interface{}) string {
   155  	return RequestContent("TRACE", url, data...)
   156  }
   157  
   158  // RequestContent is a convenience method for sending custom http method request, which
   159  // retrieves and returns the result content and automatically closes response object.
   160  // Deprecated, please use g.Client().RequestContent or NewClient().RequestContent instead.
   161  func RequestContent(method string, url string, data ...interface{}) string {
   162  	return client.New().RequestContent(method, url, data...)
   163  }
   164  
   165  // GetBytes is a convenience method for sending GET request, which retrieves and returns
   166  // the result content as bytes and automatically closes response object.
   167  // Deprecated, please use g.Client().GetBytes or NewClient().GetBytes instead.
   168  func GetBytes(url string, data ...interface{}) []byte {
   169  	return RequestBytes("GET", url, data...)
   170  }
   171  
   172  // PutBytes is a convenience method for sending PUT request, which retrieves and returns
   173  // the result content as bytes and automatically closes response object.
   174  // Deprecated, please use g.Client().PutBytes or NewClient().PutBytes instead.
   175  func PutBytes(url string, data ...interface{}) []byte {
   176  	return RequestBytes("PUT", url, data...)
   177  }
   178  
   179  // PostBytes is a convenience method for sending POST request, which retrieves and returns
   180  // the result content as bytes and automatically closes response object.
   181  // Deprecated, please use g.Client().PostBytes or NewClient().PostBytes instead.
   182  func PostBytes(url string, data ...interface{}) []byte {
   183  	return RequestBytes("POST", url, data...)
   184  }
   185  
   186  // DeleteBytes is a convenience method for sending DELETE request, which retrieves and returns
   187  // the result content as bytes and automatically closes response object.
   188  // Deprecated, please use g.Client().DeleteBytes or NewClient().DeleteBytes instead.
   189  func DeleteBytes(url string, data ...interface{}) []byte {
   190  	return RequestBytes("DELETE", url, data...)
   191  }
   192  
   193  // HeadBytes is a convenience method for sending HEAD request, which retrieves and returns
   194  // the result content as bytes and automatically closes response object.
   195  // Deprecated, please use g.Client().HeadBytes or NewClient().HeadBytes instead.
   196  func HeadBytes(url string, data ...interface{}) []byte {
   197  	return RequestBytes("HEAD", url, data...)
   198  }
   199  
   200  // PatchBytes is a convenience method for sending PATCH request, which retrieves and returns
   201  // the result content as bytes and automatically closes response object.
   202  // Deprecated, please use g.Client().PatchBytes or NewClient().PatchBytes instead.
   203  func PatchBytes(url string, data ...interface{}) []byte {
   204  	return RequestBytes("PATCH", url, data...)
   205  }
   206  
   207  // ConnectBytes is a convenience method for sending CONNECT request, which retrieves and returns
   208  // the result content as bytes and automatically closes response object.
   209  // Deprecated, please use g.Client().ConnectBytes or NewClient().ConnectBytes instead.
   210  func ConnectBytes(url string, data ...interface{}) []byte {
   211  	return RequestBytes("CONNECT", url, data...)
   212  }
   213  
   214  // OptionsBytes is a convenience method for sending OPTIONS request, which retrieves and returns
   215  // the result content as bytes and automatically closes response object.
   216  // Deprecated, please use g.Client().OptionsBytes or NewClient().OptionsBytes instead.
   217  func OptionsBytes(url string, data ...interface{}) []byte {
   218  	return RequestBytes("OPTIONS", url, data...)
   219  }
   220  
   221  // TraceBytes is a convenience method for sending TRACE request, which retrieves and returns
   222  // the result content as bytes and automatically closes response object.
   223  // Deprecated, please use g.Client().TraceBytes or NewClient().TraceBytes instead.
   224  func TraceBytes(url string, data ...interface{}) []byte {
   225  	return RequestBytes("TRACE", url, data...)
   226  }
   227  
   228  // RequestBytes is a convenience method for sending custom http method request, which
   229  // retrieves and returns the result content as bytes and automatically closes response object.
   230  // Deprecated, please use g.Client().RequestBytes or NewClient().RequestBytes instead.
   231  func RequestBytes(method string, url string, data ...interface{}) []byte {
   232  	return client.New().RequestBytes(method, url, data...)
   233  }
   234  
   235  // GetVar sends a GET request, retrieves and converts the result content to specified pointer.
   236  // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
   237  // Deprecated, please use g.Client().GetVar or NewClient().GetVar instead.
   238  func GetVar(url string, data ...interface{}) *gvar.Var {
   239  	return RequestVar("GET", url, data...)
   240  }
   241  
   242  // PutVar sends a PUT request, retrieves and converts the result content to specified pointer.
   243  // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
   244  // Deprecated, please use g.Client().PutVar or NewClient().PutVar instead.
   245  func PutVar(url string, data ...interface{}) *gvar.Var {
   246  	return RequestVar("PUT", url, data...)
   247  }
   248  
   249  // PostVar sends a POST request, retrieves and converts the result content to specified pointer.
   250  // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
   251  // Deprecated, please use g.Client().PostVar or NewClient().PostVar instead.
   252  func PostVar(url string, data ...interface{}) *gvar.Var {
   253  	return RequestVar("POST", url, data...)
   254  }
   255  
   256  // DeleteVar sends a DELETE request, retrieves and converts the result content to specified pointer.
   257  // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
   258  // Deprecated, please use g.Client().DeleteVar or NewClient().DeleteVar instead.
   259  func DeleteVar(url string, data ...interface{}) *gvar.Var {
   260  	return RequestVar("DELETE", url, data...)
   261  }
   262  
   263  // HeadVar sends a HEAD request, retrieves and converts the result content to specified pointer.
   264  // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
   265  // Deprecated, please use g.Client().HeadVar or NewClient().HeadVar instead.
   266  func HeadVar(url string, data ...interface{}) *gvar.Var {
   267  	return RequestVar("HEAD", url, data...)
   268  }
   269  
   270  // PatchVar sends a PATCH request, retrieves and converts the result content to specified pointer.
   271  // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
   272  // Deprecated, please use g.Client().PatchVar or NewClient().PatchVar instead.
   273  func PatchVar(url string, data ...interface{}) *gvar.Var {
   274  	return RequestVar("PATCH", url, data...)
   275  }
   276  
   277  // ConnectVar sends a CONNECT request, retrieves and converts the result content to specified pointer.
   278  // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
   279  // Deprecated, please use g.Client().ConnectVar or NewClient().ConnectVar instead.
   280  func ConnectVar(url string, data ...interface{}) *gvar.Var {
   281  	return RequestVar("CONNECT", url, data...)
   282  }
   283  
   284  // OptionsVar sends a OPTIONS request, retrieves and converts the result content to specified pointer.
   285  // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
   286  // Deprecated, please use g.Client().OptionsVar or NewClient().OptionsVar instead.
   287  func OptionsVar(url string, data ...interface{}) *gvar.Var {
   288  	return RequestVar("OPTIONS", url, data...)
   289  }
   290  
   291  // TraceVar sends a TRACE request, retrieves and converts the result content to specified pointer.
   292  // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
   293  // Deprecated, please use g.Client().TraceVar or NewClient().TraceVar instead.
   294  func TraceVar(url string, data ...interface{}) *gvar.Var {
   295  	return RequestVar("TRACE", url, data...)
   296  }
   297  
   298  // RequestVar sends request using given HTTP method and data, retrieves converts the result
   299  // to specified pointer. It reads and closes the response object internally automatically.
   300  // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
   301  // Deprecated, please use g.Client().RequestVar or NewClient().RequestVar instead.
   302  func RequestVar(method string, url string, data ...interface{}) *gvar.Var {
   303  	response, err := DoRequest(method, url, data...)
   304  	if err != nil {
   305  		return gvar.New(nil)
   306  	}
   307  	defer response.Close()
   308  	return gvar.New(response.ReadAll())
   309  }