github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/cmd/internal/openapi/v3/codegen/testdata/test/customerclient.go (about)

     1  package test
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/http"
     8  
     9  	"github.com/go-resty/resty/v2"
    10  	_querystring "github.com/google/go-querystring/query"
    11  	"github.com/opentracing-contrib/go-stdlib/nethttp"
    12  	"github.com/opentracing/opentracing-go"
    13  	"github.com/pkg/errors"
    14  	ddhttp "github.com/unionj-cloud/go-doudou/framework/http"
    15  	"github.com/unionj-cloud/go-doudou/framework/registry"
    16  )
    17  
    18  type CustomerClient struct {
    19  	provider registry.IServiceProvider
    20  	client   *resty.Client
    21  	rootPath string
    22  }
    23  
    24  func (receiver *CustomerClient) SetRootPath(rootPath string) {
    25  	receiver.rootPath = rootPath
    26  }
    27  
    28  func (receiver *CustomerClient) SetProvider(provider registry.IServiceProvider) {
    29  	receiver.provider = provider
    30  }
    31  
    32  func (receiver *CustomerClient) SetClient(client *resty.Client) {
    33  	receiver.client = client
    34  }
    35  func (receiver *CustomerClient) GetCustomerValidateToken(ctx context.Context, _headers map[string]string,
    36  	queryParams struct {
    37  		// required
    38  		Token string `json:"token,omitempty" url:"token"`
    39  	}) (ret bool, _resp *resty.Response, err error) {
    40  	var _err error
    41  
    42  	_req := receiver.client.R()
    43  	_req.SetContext(ctx)
    44  	if len(_headers) > 0 {
    45  		_req.SetHeaders(_headers)
    46  	}
    47  	_queryParams, _ := _querystring.Values(queryParams)
    48  	_req.SetQueryParamsFromValues(_queryParams)
    49  
    50  	_resp, _err = _req.Get("/customer/validateToken")
    51  	if _err != nil {
    52  		err = errors.Wrap(_err, "")
    53  		return
    54  	}
    55  	if _resp.IsError() {
    56  		err = errors.New(_resp.String())
    57  		return
    58  	}
    59  	if _err = json.Unmarshal(_resp.Body(), &ret); _err != nil {
    60  		err = errors.Wrap(_err, "")
    61  		return
    62  	}
    63  	return
    64  }
    65  
    66  func NewCustomer(opts ...ddhttp.DdClientOption) *CustomerClient {
    67  	defaultProvider := ddhttp.NewServiceProvider("CUSTOMER")
    68  	defaultClient := ddhttp.NewClient()
    69  
    70  	svcClient := &CustomerClient{
    71  		provider: defaultProvider,
    72  		client:   defaultClient,
    73  	}
    74  
    75  	for _, opt := range opts {
    76  		opt(svcClient)
    77  	}
    78  
    79  	svcClient.client.OnBeforeRequest(func(_ *resty.Client, request *resty.Request) error {
    80  		request.URL = svcClient.provider.SelectServer() + svcClient.rootPath + request.URL
    81  		return nil
    82  	})
    83  
    84  	svcClient.client.SetPreRequestHook(func(_ *resty.Client, request *http.Request) error {
    85  		traceReq, _ := nethttp.TraceRequest(opentracing.GlobalTracer(), request,
    86  			nethttp.OperationName(fmt.Sprintf("HTTP %s: %s", request.Method, request.URL.Path)))
    87  		*request = *traceReq
    88  		return nil
    89  	})
    90  
    91  	svcClient.client.OnAfterResponse(func(_ *resty.Client, response *resty.Response) error {
    92  		nethttp.TracerFromRequest(response.Request.RawRequest).Finish()
    93  		return nil
    94  	})
    95  
    96  	return svcClient
    97  }