github.com/wangyougui/gf/v2@v2.6.5/net/gclient/gclient_var.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/wangyougui/gf.
     6  
     7  package gclient
     8  
     9  import (
    10  	"context"
    11  	"net/http"
    12  
    13  	"github.com/wangyougui/gf/v2/container/gvar"
    14  	"github.com/wangyougui/gf/v2/internal/intlog"
    15  )
    16  
    17  // GetVar sends a GET request, retrieves and converts the result content to *gvar.Var.
    18  // The client reads and closes the response object internally automatically.
    19  // The result *gvar.Var can be conveniently converted to any type you want.
    20  func (c *Client) GetVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
    21  	return c.RequestVar(ctx, http.MethodGet, url, data...)
    22  }
    23  
    24  // PutVar sends a PUT request, retrieves and converts the result content to *gvar.Var.
    25  // The client reads and closes the response object internally automatically.
    26  // The result *gvar.Var can be conveniently converted to any type you want.
    27  func (c *Client) PutVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
    28  	return c.RequestVar(ctx, http.MethodPut, url, data...)
    29  }
    30  
    31  // PostVar sends a POST request, retrieves and converts the result content to *gvar.Var.
    32  // The client reads and closes the response object internally automatically.
    33  // The result *gvar.Var can be conveniently converted to any type you want.
    34  func (c *Client) PostVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
    35  	return c.RequestVar(ctx, http.MethodPost, url, data...)
    36  }
    37  
    38  // DeleteVar sends a DELETE request, retrieves and converts the result content to *gvar.Var.
    39  // The client reads and closes the response object internally automatically.
    40  // The result *gvar.Var can be conveniently converted to any type you want.
    41  func (c *Client) DeleteVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
    42  	return c.RequestVar(ctx, http.MethodDelete, url, data...)
    43  }
    44  
    45  // HeadVar sends a HEAD request, retrieves and converts the result content to *gvar.Var.
    46  // The client reads and closes the response object internally automatically.
    47  // The result *gvar.Var can be conveniently converted to any type you want.
    48  func (c *Client) HeadVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
    49  	return c.RequestVar(ctx, http.MethodHead, url, data...)
    50  }
    51  
    52  // PatchVar sends a PATCH request, retrieves and converts the result content to *gvar.Var.
    53  // The client reads and closes the response object internally automatically.
    54  // The result *gvar.Var can be conveniently converted to any type you want.
    55  func (c *Client) PatchVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
    56  	return c.RequestVar(ctx, http.MethodPatch, url, data...)
    57  }
    58  
    59  // ConnectVar sends a CONNECT request, retrieves and converts the result content to *gvar.Var.
    60  // The client reads and closes the response object internally automatically.
    61  // The result *gvar.Var can be conveniently converted to any type you want.
    62  func (c *Client) ConnectVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
    63  	return c.RequestVar(ctx, http.MethodConnect, url, data...)
    64  }
    65  
    66  // OptionsVar sends an OPTIONS request, retrieves and converts the result content to *gvar.Var.
    67  // The client reads and closes the response object internally automatically.
    68  // The result *gvar.Var can be conveniently converted to any type you want.
    69  func (c *Client) OptionsVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
    70  	return c.RequestVar(ctx, http.MethodOptions, url, data...)
    71  }
    72  
    73  // TraceVar sends a TRACE request, retrieves and converts the result content to *gvar.Var.
    74  // The client reads and closes the response object internally automatically.
    75  // The result *gvar.Var can be conveniently converted to any type you want.
    76  func (c *Client) TraceVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
    77  	return c.RequestVar(ctx, http.MethodTrace, url, data...)
    78  }
    79  
    80  // RequestVar sends request using given HTTP method and data, retrieves converts the result to *gvar.Var.
    81  // The client reads and closes the response object internally automatically.
    82  // The result *gvar.Var can be conveniently converted to any type you want.
    83  func (c *Client) RequestVar(ctx context.Context, method string, url string, data ...interface{}) *gvar.Var {
    84  	response, err := c.DoRequest(ctx, method, url, data...)
    85  	if err != nil {
    86  		intlog.Errorf(ctx, `%+v`, err)
    87  		return gvar.New(nil)
    88  	}
    89  	defer func() {
    90  		if err = response.Close(); err != nil {
    91  			intlog.Errorf(ctx, `%+v`, err)
    92  		}
    93  	}()
    94  	return gvar.New(response.ReadAll())
    95  }