dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/rest/client/client_impl/resty_client.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package client_impl
    19  
    20  import (
    21  	"context"
    22  	"net"
    23  	"net/http"
    24  	"path"
    25  	"time"
    26  )
    27  
    28  import (
    29  	"github.com/go-resty/resty/v2"
    30  
    31  	perrors "github.com/pkg/errors"
    32  )
    33  
    34  import (
    35  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    36  	"dubbo.apache.org/dubbo-go/v3/common/extension"
    37  	"dubbo.apache.org/dubbo-go/v3/protocol/rest/client"
    38  )
    39  
    40  func init() {
    41  	extension.SetRestClient(constant.DefaultRestClient, NewRestyClient)
    42  }
    43  
    44  // RestyClient a rest client implement by Resty
    45  type RestyClient struct {
    46  	client *resty.Client
    47  }
    48  
    49  // NewRestyClient a constructor of RestyClient
    50  func NewRestyClient(restOption *client.RestOptions) client.RestClient {
    51  	client := resty.New()
    52  	client.SetTransport(
    53  		&http.Transport{
    54  			DialContext: func(_ context.Context, network, addr string) (net.Conn, error) {
    55  				c, err := net.DialTimeout(network, addr, restOption.ConnectTimeout)
    56  				if err != nil {
    57  					return nil, err
    58  				}
    59  				err = c.SetDeadline(time.Now().Add(restOption.RequestTimeout))
    60  				if err != nil {
    61  					return nil, err
    62  				}
    63  				return c, nil
    64  			},
    65  		})
    66  	return &RestyClient{
    67  		client: client,
    68  	}
    69  }
    70  
    71  // Do send request by RestyClient
    72  func (rc *RestyClient) Do(restRequest *client.RestClientRequest, res interface{}) error {
    73  	req := rc.client.R()
    74  	req.Header = restRequest.Header
    75  	resp, err := req.
    76  		SetPathParams(restRequest.PathParams).
    77  		SetQueryParams(restRequest.QueryParams).
    78  		SetBody(restRequest.Body).
    79  		SetResult(res).
    80  		Execute(restRequest.Method, "http://"+path.Join(restRequest.Location, restRequest.Path))
    81  	if err != nil {
    82  		return perrors.WithStack(err)
    83  	}
    84  	if resp.IsError() {
    85  		return perrors.New(resp.String())
    86  	}
    87  	return nil
    88  }