center-config/client/curl.go

157 lines
4.0 KiB
Go
Raw Normal View History

2021-11-30 17:50:21 +08:00
// Package client ...
//
// Description : 使用的客户端
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-30 10:42 上午
package client
import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"git.zhangdeman.cn/zhangdeman/gopkg/util"
"github.com/ddliu/go-httpclient"
)
// CenterClient 客户端实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:01 上午 2021/11/30
type CenterClient struct {
cfg *Config
}
// CreateConfig 生成配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:44 上午 2021/11/30
func (cc *CenterClient) CreateConfig(ctx context.Context, key string, value string, operateUserID string, description string) error {
parameter := map[string]string{
"key": key,
"value": value,
"operate_user_id": operateUserID,
"description": description,
}
_, err := cc.request(ctx, SetConfigURI, parameter, PostHeader)
return err
}
// GetConfig 读取配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:09 下午 2021/11/30
func (cc *CenterClient) GetConfig(ctx context.Context, key string, receiver interface{}) ([]byte, error) {
var (
result []byte
err error
)
parameter := map[string]string{
"key": key,
}
if result, err = cc.request(ctx, GetConfigURI, parameter, GetHeader); nil != err {
return nil, err
}
if nil != receiver {
return result, util.JSONUnmarshalWithNumber(result, result)
}
return result, nil
}
// UpdateConfig 更新配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:34 下午 2021/11/30
func (cc *CenterClient) UpdateConfig(ctx context.Context, key string, value string, operateUserID string, description string) error {
parameter := map[string]string{
"key": key,
"value": value,
"operate_user_id": operateUserID,
"description": description,
}
_, err := cc.request(nil, UpdateConfigURI, parameter, GetHeader)
return err
}
// CreateNamespace 创建命名空间
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:48 下午 2021/11/30
func (cc *CenterClient) CreateNamespace(ctx context.Context, namespace string, createUserID string, name string, description string) error {
parameter := map[string]string{
"namespace": namespace,
"create_user_id": createUserID,
"name": name,
"description": description,
}
_, err := cc.request(ctx, CreateNamespaceURI, parameter, PostHeader)
return err
}
// ActiveNamespace ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:54 下午 2021/11/30
func (cc *CenterClient) ActiveNamespace(ctx context.Context, namespace string, updateUserID string) error {
parameter := map[string]string{
"namespace": namespace,
"update_user_id": updateUserID,
}
_, err := cc.request(ctx, ActiveNamespaceURI, parameter, GetHeader)
return err
}
// request 发送请求
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:51 上午 2021/11/30
func (cc *CenterClient) request(ctx context.Context, uriConfig *RequestURIConfig, parameter map[string]string, header map[string]string) ([]byte, error) {
var (
client *httpclient.HttpClient
response *httpclient.Response
responseData []byte
err error
)
if nil == parameter {
parameter = make(map[string]string)
}
if nil == ctx {
ctx = context.TODO()
}
if _, exist := parameter["namespace"]; !exist {
parameter["namespace"] = cc.cfg.Namespace
}
parameter["app_id"] = cc.cfg.AppID
fullRequestURL := cc.cfg.Domain + uriConfig.URI
if uriConfig.Method == http.MethodGet {
urlParam := url.Values{}
for k, v := range parameter {
urlParam.Set(k, v)
}
fullRequestURL += "?" + urlParam.Encode()
}
client = httpclient.NewHttpClient()
inputByteData, _ := json.Marshal(parameter)
requestData := bytes.NewReader(inputByteData)
if response, err = client.Do(uriConfig.Method, fullRequestURL, header, requestData); nil != err {
return nil, err
}
if responseData, err = ioutil.ReadAll(response.Body); nil != err {
return nil, err
}
return responseData, nil
}