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

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