|
@@ -58,6 +58,27 @@ type infoResponse struct {
|
|
|
Info getByKeysResult `json:"info"`
|
|
Info getByKeysResult `json:"info"`
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+type updateRequest struct {
|
|
|
|
|
+ ID string `json:"id"`
|
|
|
|
|
+ ConfigKey string `json:"configKey"`
|
|
|
|
|
+ ConfigValue string `json:"configValue"`
|
|
|
|
|
+ ValueType string `json:"valueType"`
|
|
|
|
|
+ ConfigGroup string `json:"configGroup"`
|
|
|
|
|
+ Service string `json:"service"`
|
|
|
|
|
+ Description string `json:"description"`
|
|
|
|
|
+ UpdateUserID string `json:"updateUserId"`
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+type createRequest struct {
|
|
|
|
|
+ ConfigKey string `json:"configKey"`
|
|
|
|
|
+ ConfigValue string `json:"configValue"`
|
|
|
|
|
+ ValueType string `json:"valueType"`
|
|
|
|
|
+ ConfigGroup string `json:"configGroup"`
|
|
|
|
|
+ Service string `json:"service"`
|
|
|
|
|
+ Description string `json:"description"`
|
|
|
|
|
+ CreateUserID string `json:"createUserId"`
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
var (
|
|
var (
|
|
|
invokeAPI *invoke.API
|
|
invokeAPI *invoke.API
|
|
|
urlPrefix = DefaultURLPrefix
|
|
urlPrefix = DefaultURLPrefix
|
|
@@ -278,6 +299,90 @@ func GetBool(key string, defaultValue bool) bool {
|
|
|
return boolValue
|
|
return boolValue
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// Update 更新已有配置项(需带 ID)。
|
|
|
|
|
+func Update(info ConfigInfo, updateUserID string) error {
|
|
|
|
|
+ if invokeAPI == nil {
|
|
|
|
|
+ return errors.New("mbsdsdk not initialized")
|
|
|
|
|
+ }
|
|
|
|
|
+ if info.ID == "" {
|
|
|
|
|
+ return errors.New("config id is required")
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ body, err := json.Marshal(&updateRequest{
|
|
|
|
|
+ ID: info.ID,
|
|
|
|
|
+ ConfigKey: info.ConfigKey,
|
|
|
|
|
+ ConfigValue: info.ConfigValue,
|
|
|
|
|
+ ValueType: info.ValueType,
|
|
|
|
|
+ ConfigGroup: info.ConfigGroup,
|
|
|
|
|
+ Service: info.Service,
|
|
|
|
|
+ Description: info.Description,
|
|
|
|
|
+ UpdateUserID: updateUserID,
|
|
|
|
|
+ })
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ path, err := url.JoinPath(urlPrefix, "update")
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ respBytes, err := invokeAPI.PutJSON(path, body, nil)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var resp msgResponse
|
|
|
|
|
+ if err = json.Unmarshal(respBytes, &resp); err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ if !resp.Success {
|
|
|
|
|
+ return errors.New(resp.Msg)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ storeCache(info.ConfigKey, info)
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// Create 新建配置项。
|
|
|
|
|
+func Create(info ConfigInfo, createUserID string) error {
|
|
|
|
|
+ if invokeAPI == nil {
|
|
|
|
|
+ return errors.New("mbsdsdk not initialized")
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ body, err := json.Marshal(&createRequest{
|
|
|
|
|
+ ConfigKey: info.ConfigKey,
|
|
|
|
|
+ ConfigValue: info.ConfigValue,
|
|
|
|
|
+ ValueType: info.ValueType,
|
|
|
|
|
+ ConfigGroup: info.ConfigGroup,
|
|
|
|
|
+ Service: info.Service,
|
|
|
|
|
+ Description: info.Description,
|
|
|
|
|
+ CreateUserID: createUserID,
|
|
|
|
|
+ })
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ path, err := url.JoinPath(urlPrefix, "create")
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ respBytes, err := invokeAPI.PostJSON(path, body, nil)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var resp msgResponse
|
|
|
|
|
+ if err = json.Unmarshal(respBytes, &resp); err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ if !resp.Success {
|
|
|
|
|
+ return errors.New(resp.Msg)
|
|
|
|
|
+ }
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// GetJSON 将配置值反序列化到 target;未找到 key 时返回错误。
|
|
// GetJSON 将配置值反序列化到 target;未找到 key 时返回错误。
|
|
|
func GetJSON(key string, target any) error {
|
|
func GetJSON(key string, target any) error {
|
|
|
info, ok, err := Get(key)
|
|
info, ok, err := Get(key)
|