github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/client/http/resty.go (about)

     1  package http
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/go-resty/resty/v2"
     8  )
     9  
    10  // docs: https://github.com/go-resty/resty
    11  
    12  type restyClient struct {
    13  	timeout time.Duration
    14  }
    15  
    16  // NewRestyClient 创建http client客户端
    17  func NewRestyClient() Client {
    18  	return &restyClient{
    19  		timeout: defaultTimeout,
    20  	}
    21  }
    22  
    23  // Get 发送get请求
    24  func (r *restyClient) Get(ctx context.Context, url string, options ...ClientOption) ([]byte, error) {
    25  	client := resty.New()
    26  	r.setting(ctx, client, options...)
    27  
    28  	resp, err := client.R().Get(url)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	return resp.Body(), nil
    33  }
    34  
    35  // Post 发送form post请求
    36  func (r *restyClient) Post(ctx context.Context, url string, data map[string]string, options ...ClientOption) ([]byte, error) {
    37  	options = append(options, WithHTTPHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"))
    38  	client := resty.New()
    39  	r.setting(ctx, client, options...)
    40  
    41  	resp, err := client.R().SetFormData(data).Post(url)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	return resp.Body(), nil
    46  }
    47  
    48  // PostJSON 发送post raw json 请求
    49  func (r *restyClient) PostJSON(ctx context.Context, url string, body []byte, options ...ClientOption) ([]byte, error) {
    50  	options = append(options, WithHTTPHeader("Content-Type", "application/json; charset=utf-8"))
    51  	client := resty.New()
    52  	r.setting(ctx, client, options...)
    53  
    54  	resp, err := client.R().SetBody(body).Post(url)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	return resp.Body(), nil
    59  }
    60  
    61  // Upload 上传
    62  func (r *restyClient) Upload(ctx context.Context, url string, form UploadForm, options ...ClientOption) (b []byte, err error) {
    63  	return
    64  }
    65  
    66  func (r *restyClient) setting(ctx context.Context, client *resty.Client, options ...ClientOption) {
    67  	settings := &httpSettings{timeout: r.timeout}
    68  
    69  	if len(options) != 0 {
    70  		settings.headers = make(map[string]string)
    71  
    72  		for _, f := range options {
    73  			f(settings)
    74  		}
    75  	}
    76  
    77  	if r.timeout != 0 {
    78  		client.SetTimeout(settings.timeout)
    79  	}
    80  
    81  	// headers
    82  	if len(settings.headers) != 0 {
    83  		client.SetHeaders(settings.headers)
    84  	}
    85  
    86  	// cookies
    87  	if len(settings.cookies) != 0 {
    88  		client.SetCookies(settings.cookies)
    89  	}
    90  
    91  	if settings.debug {
    92  		client.SetDebug(true)
    93  	}
    94  
    95  	if settings.close {
    96  		client.SetCloseConnection(true)
    97  	}
    98  }