github.com/polarismesh/polaris@v1.17.8/test/integrate/http/client.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package http
    19  
    20  import (
    21  	"bytes"
    22  	"errors"
    23  	"fmt"
    24  	"io"
    25  	"net/http"
    26  
    27  	"github.com/golang/protobuf/jsonpb"
    28  	apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
    29  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    30  )
    31  
    32  // NewClient 创建HTTP客户端
    33  func NewClient(address, version string) *Client {
    34  	return &Client{
    35  		Address: address,
    36  		Version: version,
    37  		Worker:  &http.Client{},
    38  	}
    39  }
    40  
    41  // Client HTTP客户端
    42  type Client struct {
    43  	Address string
    44  	Version string
    45  	Worker  *http.Client
    46  }
    47  
    48  // SendRequest 发送请求 HTTP Post/Put
    49  func (c *Client) SendRequest(method string, url string, body *bytes.Buffer) (*http.Response, error) {
    50  	var request *http.Request
    51  	var err error
    52  
    53  	if body == nil {
    54  		request, err = http.NewRequest(method, url, nil)
    55  	} else {
    56  		request, err = http.NewRequest(method, url, body)
    57  	}
    58  
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	request.Header.Add("Content-Type", "application/json")
    64  	request.Header.Add("Request-Id", "test")
    65  	request.Header.Add("X-Polaris-Token", "nu/0WRA4EqSR1FagrjRj0fZwPXuGlMpX+zCuWu4uMqy8xr1vRjisSbA25aAC3mtU8MeeRsKhQiDAynUR09I=")
    66  
    67  	response, err := c.Worker.Do(request)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	return response, nil
    73  }
    74  
    75  // CompleteURL 生成GET请求的完整URL
    76  func (c *Client) CompleteURL(url string, params map[string][]interface{}) string {
    77  	count := 1
    78  	url += "?"
    79  
    80  	num := 0
    81  	for _, param := range params {
    82  		num += len(param)
    83  	}
    84  
    85  	for index, param := range params {
    86  		for _, item := range param {
    87  			url += fmt.Sprintf("%v=%v", index, item)
    88  			if count != num {
    89  				url += "&"
    90  			}
    91  			count++
    92  		}
    93  	}
    94  	return url
    95  }
    96  
    97  // GetBatchWriteResponse 获取BatchWriteResponse
    98  func GetBatchWriteResponse(response *http.Response) (*apiservice.BatchWriteResponse, error) {
    99  	// 打印回复
   100  	fmt.Printf("http code: %v\n", response.StatusCode)
   101  
   102  	ret := &apiservice.BatchWriteResponse{}
   103  	checkErr := jsonpb.Unmarshal(response.Body, ret)
   104  	if checkErr == nil {
   105  		fmt.Printf("%+v\n", ret)
   106  	} else {
   107  		fmt.Printf("%v\n", checkErr)
   108  		ret = nil
   109  	}
   110  
   111  	// 检查回复
   112  	if response.StatusCode != 200 {
   113  		return ret, fmt.Errorf("invalid http code : %d, ret code : %d", response.StatusCode, ret.GetCode().GetValue())
   114  	}
   115  
   116  	if checkErr == nil {
   117  		return ret, nil
   118  	} else if checkErr == io.EOF {
   119  		return nil, io.EOF
   120  	} else {
   121  		return nil, errors.New("body decode failed")
   122  	}
   123  }
   124  
   125  func GetConfigResponse(response *http.Response) (*apiconfig.ConfigResponse, error) {
   126  	// 打印回复
   127  	fmt.Printf("http code: %v\n", response.StatusCode)
   128  
   129  	ret := &apiconfig.ConfigResponse{}
   130  	checkErr := jsonpb.Unmarshal(response.Body, ret)
   131  	if checkErr == nil {
   132  		fmt.Printf("%+v\n", ret)
   133  	} else {
   134  		fmt.Printf("%v\n", checkErr)
   135  	}
   136  
   137  	// 检查回复
   138  	if response.StatusCode != 200 {
   139  		return nil, errors.New("invalid http code")
   140  	}
   141  
   142  	if checkErr == nil {
   143  		return ret, nil
   144  	} else if checkErr == io.EOF {
   145  		return nil, io.EOF
   146  	} else {
   147  		return nil, errors.New("body decode failed")
   148  	}
   149  }
   150  
   151  func GetConfigQueryResponse(response *http.Response) (*apiconfig.ConfigBatchQueryResponse, error) {
   152  	// 打印回复
   153  	fmt.Printf("http code: %v\n", response.StatusCode)
   154  
   155  	ret := &apiconfig.ConfigBatchQueryResponse{}
   156  	checkErr := jsonpb.Unmarshal(response.Body, ret)
   157  	if checkErr == nil {
   158  		fmt.Printf("%+v\n", ret)
   159  	} else {
   160  		fmt.Printf("%v\n", checkErr)
   161  	}
   162  
   163  	// 检查回复
   164  	if response.StatusCode != 200 {
   165  		return nil, errors.New("invalid http code")
   166  	}
   167  
   168  	if checkErr == nil {
   169  		return ret, nil
   170  	} else if checkErr == io.EOF {
   171  		return nil, io.EOF
   172  	} else {
   173  		return nil, errors.New("body decode failed")
   174  	}
   175  }
   176  
   177  // GetConfigBatchWriteResponse 获取BatchWriteResponse
   178  func GetConfigBatchWriteResponse(response *http.Response) (*apiconfig.ConfigBatchWriteResponse, error) {
   179  	// 打印回复
   180  	fmt.Printf("http code: %v\n", response.StatusCode)
   181  
   182  	ret := &apiconfig.ConfigBatchWriteResponse{}
   183  	checkErr := jsonpb.Unmarshal(response.Body, ret)
   184  	if checkErr == nil {
   185  		fmt.Printf("%+v\n", ret)
   186  	} else {
   187  		fmt.Printf("%v\n", checkErr)
   188  	}
   189  
   190  	// 检查回复
   191  	if response.StatusCode != 200 {
   192  		return nil, errors.New("invalid http code")
   193  	}
   194  
   195  	if checkErr == nil {
   196  		return ret, nil
   197  	} else if checkErr == io.EOF {
   198  		return nil, io.EOF
   199  	} else {
   200  		return nil, errors.New("body decode failed")
   201  	}
   202  }
   203  
   204  // GetBatchQueryResponse 获取BatchQueryResponse
   205  func GetBatchQueryResponse(response *http.Response) (*apiservice.BatchQueryResponse, error) {
   206  	// 打印回复
   207  	fmt.Printf("http code: %v\n", response.StatusCode)
   208  
   209  	ret := &apiservice.BatchQueryResponse{}
   210  	checkErr := jsonpb.Unmarshal(response.Body, ret)
   211  	if checkErr == nil {
   212  		fmt.Printf("%+v\n", ret)
   213  	} else {
   214  		fmt.Printf("%v\n", checkErr)
   215  	}
   216  
   217  	// 检查回复
   218  	if response.StatusCode != 200 {
   219  		return nil, errors.New("invalid http code")
   220  	}
   221  
   222  	if checkErr == nil {
   223  		return ret, nil
   224  	} else if checkErr == io.EOF {
   225  		return nil, io.EOF
   226  	} else {
   227  		return nil, errors.New("body decode failed")
   228  	}
   229  }
   230  
   231  // GetSimpleResponse 获取SimpleResponse
   232  func GetSimpleResponse(response *http.Response) (*apiservice.Response, error) {
   233  	// 打印回复
   234  	fmt.Printf("http code: %v\n", response.StatusCode)
   235  
   236  	ret := &apiservice.Response{}
   237  	checkErr := jsonpb.Unmarshal(response.Body, ret)
   238  	if checkErr == nil {
   239  		fmt.Printf("%+v\n", ret)
   240  	} else {
   241  		fmt.Printf("%v\n", checkErr)
   242  	}
   243  
   244  	// 检查回复
   245  	if response.StatusCode != 200 {
   246  		return nil, errors.New("invalid http code")
   247  	}
   248  
   249  	if checkErr == nil {
   250  		return ret, nil
   251  	} else if checkErr == io.EOF {
   252  		return nil, io.EOF
   253  	} else {
   254  		return nil, errors.New("body decode failed")
   255  	}
   256  }
   257  
   258  // GetConfigImportResponse 获取ConfigImportResponse
   259  func GetConfigImportResponse(response *http.Response) (*apiconfig.ConfigImportResponse, error) {
   260  	// 打印回复
   261  	fmt.Printf("http code: %v\n", response.StatusCode)
   262  
   263  	ret := &apiconfig.ConfigImportResponse{}
   264  	checkErr := jsonpb.Unmarshal(response.Body, ret)
   265  	if checkErr == nil {
   266  		fmt.Printf("%+v\n", ret)
   267  	} else {
   268  		fmt.Printf("%v\n", checkErr)
   269  	}
   270  
   271  	// 检查回复
   272  	if response.StatusCode != 200 {
   273  		return nil, errors.New("invalid http code")
   274  	}
   275  
   276  	if checkErr == nil {
   277  		return ret, nil
   278  	} else if checkErr == io.EOF {
   279  		return nil, io.EOF
   280  	} else {
   281  		return nil, errors.New("body decode failed")
   282  	}
   283  }
   284  
   285  func GetConfigEncryptAlgorithmResponse(response *http.Response) (*apiconfig.ConfigEncryptAlgorithmResponse, error) {
   286  	// 打印回复
   287  	fmt.Printf("http code: %v\n", response.StatusCode)
   288  
   289  	ret := &apiconfig.ConfigEncryptAlgorithmResponse{}
   290  	checkErr := jsonpb.Unmarshal(response.Body, ret)
   291  	if checkErr == nil {
   292  		fmt.Printf("%+v\n", ret)
   293  	} else {
   294  		fmt.Printf("%v\n", checkErr)
   295  	}
   296  
   297  	// 检查回复
   298  	if response.StatusCode != 200 {
   299  		return nil, errors.New("invalid http code")
   300  	}
   301  
   302  	if checkErr == nil {
   303  		return ret, nil
   304  	} else if checkErr == io.EOF {
   305  		return nil, io.EOF
   306  	} else {
   307  		return nil, errors.New("body decode failed")
   308  	}
   309  }