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

     1  package test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"net/http"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  
    12  	"github.com/go-resty/resty/v2"
    13  	_querystring "github.com/google/go-querystring/query"
    14  	"github.com/opentracing-contrib/go-stdlib/nethttp"
    15  	"github.com/opentracing/opentracing-go"
    16  	"github.com/pkg/errors"
    17  	ddhttp "github.com/unionj-cloud/go-doudou/framework/http"
    18  	"github.com/unionj-cloud/go-doudou/framework/registry"
    19  	"github.com/unionj-cloud/go-doudou/toolkit/fileutils"
    20  	"github.com/unionj-cloud/go-doudou/toolkit/stringutils"
    21  )
    22  
    23  type DownloadClient struct {
    24  	provider registry.IServiceProvider
    25  	client   *resty.Client
    26  	rootPath string
    27  }
    28  
    29  func (receiver *DownloadClient) SetRootPath(rootPath string) {
    30  	receiver.rootPath = rootPath
    31  }
    32  
    33  func (receiver *DownloadClient) SetProvider(provider registry.IServiceProvider) {
    34  	receiver.provider = provider
    35  }
    36  
    37  func (receiver *DownloadClient) SetClient(client *resty.Client) {
    38  	receiver.client = client
    39  }
    40  
    41  // GetDownloadAvatar GetDownloadAvatar demonstrate how to define download file api
    42  // there must be *os.File parameter among output parameters
    43  func (receiver *DownloadClient) GetDownloadAvatar(ctx context.Context, _headers map[string]string,
    44  	queryParams struct {
    45  		// required
    46  		UserId string `json:"userId,omitempty" url:"userId"`
    47  	}) (_downloadFile *os.File, _resp *resty.Response, err error) {
    48  	var _err error
    49  
    50  	_req := receiver.client.R()
    51  	_req.SetContext(ctx)
    52  	if len(_headers) > 0 {
    53  		_req.SetHeaders(_headers)
    54  	}
    55  	_queryParams, _ := _querystring.Values(queryParams)
    56  	_req.SetQueryParamsFromValues(_queryParams)
    57  	_req.SetDoNotParseResponse(true)
    58  
    59  	_resp, _err = _req.Get("/download/avatar")
    60  	if _err != nil {
    61  		err = errors.Wrap(_err, "")
    62  		return
    63  	}
    64  	if _resp.IsError() {
    65  		err = errors.New(_resp.String())
    66  		return
    67  	}
    68  	_disp := _resp.Header().Get("Content-Disposition")
    69  	_file := strings.TrimPrefix(_disp, "attachment; filename=")
    70  	_output := os.TempDir()
    71  	if stringutils.IsNotEmpty(_output) {
    72  		_file = _output + string(filepath.Separator) + _file
    73  	}
    74  	_file = filepath.Clean(_file)
    75  	if _err = fileutils.CreateDirectory(filepath.Dir(_file)); _err != nil {
    76  		err = errors.Wrap(_err, "")
    77  		return
    78  	}
    79  	_outFile, _err := os.Create(_file)
    80  	if _err != nil {
    81  		err = errors.Wrap(_err, "")
    82  		return
    83  	}
    84  	defer _outFile.Close()
    85  	defer _resp.RawBody().Close()
    86  	_, _err = io.Copy(_outFile, _resp.RawBody())
    87  	if _err != nil {
    88  		err = errors.Wrap(_err, "")
    89  		return
    90  	}
    91  	_downloadFile = _outFile
    92  	return
    93  }
    94  
    95  func NewDownload(opts ...ddhttp.DdClientOption) *DownloadClient {
    96  	defaultProvider := ddhttp.NewServiceProvider("DOWNLOAD")
    97  	defaultClient := ddhttp.NewClient()
    98  
    99  	svcClient := &DownloadClient{
   100  		provider: defaultProvider,
   101  		client:   defaultClient,
   102  	}
   103  
   104  	for _, opt := range opts {
   105  		opt(svcClient)
   106  	}
   107  
   108  	svcClient.client.OnBeforeRequest(func(_ *resty.Client, request *resty.Request) error {
   109  		request.URL = svcClient.provider.SelectServer() + svcClient.rootPath + request.URL
   110  		return nil
   111  	})
   112  
   113  	svcClient.client.SetPreRequestHook(func(_ *resty.Client, request *http.Request) error {
   114  		traceReq, _ := nethttp.TraceRequest(opentracing.GlobalTracer(), request,
   115  			nethttp.OperationName(fmt.Sprintf("HTTP %s: %s", request.Method, request.URL.Path)))
   116  		*request = *traceReq
   117  		return nil
   118  	})
   119  
   120  	svcClient.client.OnAfterResponse(func(_ *resty.Client, response *resty.Response) error {
   121  		nethttp.TracerFromRequest(response.Request.RawRequest).Finish()
   122  		return nil
   123  	})
   124  
   125  	return svcClient
   126  }