github.com/nacos-group/nacos-sdk-go@v1.1.4/clients/config_client/config_proxy.go (about)

     1  /*
     2   * Copyright 1999-2020 Alibaba Group Holding Ltd.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package config_client
    18  
    19  import (
    20  	"encoding/json"
    21  	"errors"
    22  	"net/http"
    23  	"strconv"
    24  	"strings"
    25  
    26  	"github.com/nacos-group/nacos-sdk-go/common/constant"
    27  	"github.com/nacos-group/nacos-sdk-go/common/http_agent"
    28  	"github.com/nacos-group/nacos-sdk-go/common/logger"
    29  	"github.com/nacos-group/nacos-sdk-go/common/nacos_server"
    30  	"github.com/nacos-group/nacos-sdk-go/model"
    31  	"github.com/nacos-group/nacos-sdk-go/util"
    32  	"github.com/nacos-group/nacos-sdk-go/vo"
    33  )
    34  
    35  type ConfigProxy struct {
    36  	nacosServer  *nacos_server.NacosServer
    37  	clientConfig constant.ClientConfig
    38  }
    39  
    40  func NewConfigProxy(serverConfig []constant.ServerConfig, clientConfig constant.ClientConfig, httpAgent http_agent.IHttpAgent) (ConfigProxy, error) {
    41  	proxy := ConfigProxy{}
    42  	var err error
    43  	proxy.nacosServer, err = nacos_server.NewNacosServer(serverConfig, clientConfig, httpAgent, clientConfig.TimeoutMs, clientConfig.Endpoint)
    44  	proxy.clientConfig = clientConfig
    45  	return proxy, err
    46  
    47  }
    48  
    49  func (cp *ConfigProxy) GetServerList() []constant.ServerConfig {
    50  	return cp.nacosServer.GetServerList()
    51  }
    52  
    53  func (cp *ConfigProxy) GetConfigProxy(param vo.ConfigParam, tenant, accessKey, secretKey string) (string, error) {
    54  	params := util.TransformObject2Param(param)
    55  	if len(tenant) > 0 {
    56  		params["tenant"] = tenant
    57  	}
    58  
    59  	var headers = map[string]string{}
    60  	headers[constant.KEY_ACCESS_KEY] = accessKey
    61  	headers[constant.KEY_SECRET_KEY] = secretKey
    62  
    63  	result, err := cp.nacosServer.ReqConfigApi(constant.CONFIG_PATH, params, headers, http.MethodGet, cp.clientConfig.TimeoutMs)
    64  	return result, err
    65  }
    66  
    67  func (cp *ConfigProxy) SearchConfigProxy(param vo.SearchConfigParam, tenant, accessKey, secretKey string) (*model.ConfigPage, error) {
    68  	params := util.TransformObject2Param(param)
    69  	if len(tenant) > 0 {
    70  		params["tenant"] = tenant
    71  	}
    72  	if _, ok := params["group"]; !ok {
    73  		params["group"] = ""
    74  	}
    75  	if _, ok := params["dataId"]; !ok {
    76  		params["dataId"] = ""
    77  	}
    78  	var headers = map[string]string{}
    79  	headers["accessKey"] = accessKey
    80  	headers["secretKey"] = secretKey
    81  	result, err := cp.nacosServer.ReqConfigApi(constant.CONFIG_PATH, params, headers, http.MethodGet, cp.clientConfig.TimeoutMs)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	var configPage model.ConfigPage
    86  	err = json.Unmarshal([]byte(result), &configPage)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	return &configPage, nil
    91  }
    92  func (cp *ConfigProxy) PublishConfigProxy(param vo.ConfigParam, tenant, accessKey, secretKey string) (bool, error) {
    93  	params := util.TransformObject2Param(param)
    94  	if len(tenant) > 0 {
    95  		params["tenant"] = tenant
    96  	}
    97  
    98  	var headers = map[string]string{}
    99  	headers["accessKey"] = accessKey
   100  	headers["secretKey"] = secretKey
   101  	result, err := cp.nacosServer.ReqConfigApi(constant.CONFIG_PATH, params, headers, http.MethodPost, cp.clientConfig.TimeoutMs)
   102  	if err != nil {
   103  		return false, errors.New("[client.PublishConfig] publish config failed:" + err.Error())
   104  	}
   105  	if strings.ToLower(strings.Trim(result, " ")) == "true" {
   106  		return true, nil
   107  	} else {
   108  		return false, errors.New("[client.PublishConfig] publish config failed:" + result)
   109  	}
   110  }
   111  
   112  func (cp *ConfigProxy) PublishAggProxy(param vo.ConfigParam, tenant, accessKey, secretKey string) (bool, error) {
   113  	params := util.TransformObject2Param(param)
   114  	if len(tenant) > 0 {
   115  		params["tenant"] = tenant
   116  	}
   117  	params["method"] = "addDatum"
   118  	var headers = map[string]string{}
   119  	headers["accessKey"] = accessKey
   120  	headers["secretKey"] = secretKey
   121  	_, err := cp.nacosServer.ReqConfigApi(constant.CONFIG_AGG_PATH, params, headers, http.MethodPost, cp.clientConfig.TimeoutMs)
   122  	if err != nil {
   123  		return false, errors.New("[client.PublishAggProxy] publish agg failed:" + err.Error())
   124  	}
   125  	return true, nil
   126  }
   127  
   128  func (cp *ConfigProxy) DeleteAggProxy(param vo.ConfigParam, tenant, accessKey, secretKey string) (bool, error) {
   129  	params := util.TransformObject2Param(param)
   130  	if len(tenant) > 0 {
   131  		params["tenant"] = tenant
   132  	}
   133  	params["method"] = "deleteDatum"
   134  	var headers = map[string]string{}
   135  	headers["accessKey"] = accessKey
   136  	headers["secretKey"] = secretKey
   137  	_, err := cp.nacosServer.ReqConfigApi(constant.CONFIG_AGG_PATH, params, headers, http.MethodPost, cp.clientConfig.TimeoutMs)
   138  	if err != nil {
   139  		return false, errors.New("[client.DeleteAggProxy] delete agg failed:" + err.Error())
   140  	}
   141  	return true, nil
   142  }
   143  
   144  func (cp *ConfigProxy) DeleteConfigProxy(param vo.ConfigParam, tenant, accessKey, secretKey string) (bool, error) {
   145  	params := util.TransformObject2Param(param)
   146  	if len(tenant) > 0 {
   147  		params["tenant"] = tenant
   148  	}
   149  	var headers = map[string]string{}
   150  	headers["accessKey"] = accessKey
   151  	headers["secretKey"] = secretKey
   152  	result, err := cp.nacosServer.ReqConfigApi(constant.CONFIG_PATH, params, headers, http.MethodDelete, cp.clientConfig.TimeoutMs)
   153  	if err != nil {
   154  		return false, errors.New("[client.DeleteConfig] deleted config failed:" + err.Error())
   155  	}
   156  	if strings.ToLower(strings.Trim(result, " ")) == "true" {
   157  		return true, nil
   158  	} else {
   159  		return false, errors.New("[client.DeleteConfig] deleted config failed: " + string(result))
   160  	}
   161  }
   162  
   163  func (cp *ConfigProxy) ListenConfig(params map[string]string, isInitializing bool, tenant, accessKey, secretKey string) (string, error) {
   164  	//fixed at 30000ms,avoid frequent request on the server
   165  	var listenInterval uint64 = 30000
   166  	headers := map[string]string{
   167  		"Content-Type":         "application/x-www-form-urlencoded;charset=utf-8",
   168  		"Long-Pulling-Timeout": strconv.FormatUint(listenInterval, 10),
   169  	}
   170  	if isInitializing {
   171  		headers["Long-Pulling-Timeout-No-Hangup"] = "true"
   172  	}
   173  
   174  	headers["accessKey"] = accessKey
   175  	headers["secretKey"] = secretKey
   176  
   177  	if len(tenant) > 0 {
   178  		params["tenant"] = tenant
   179  	}
   180  	logger.Infof("[client.ListenConfig] request params:%+v header:%+v \n", params, headers)
   181  	// In order to prevent the server from handling the delay of the client's long task,
   182  	// increase the client's read timeout to avoid this problem.
   183  	timeout := listenInterval + listenInterval/10
   184  	result, err := cp.nacosServer.ReqConfigApi(constant.CONFIG_LISTEN_PATH, params, headers, http.MethodPost, timeout)
   185  	return result, err
   186  }