yjp 2 年之前
父節點
當前提交
93f17d89df
共有 8 個文件被更改,包括 121 次插入0 次删除
  1. 8 0
      .idea/.gitignore
  2. 8 0
      .idea/modules.xml
  3. 9 0
      .idea/scm-sdk.iml
  4. 6 0
      .idea/vcs.xml
  5. 7 0
      go.mod
  6. 9 0
      go.sum
  7. 23 0
      response.go
  8. 51 0
      sdk.go

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/scm-sdk.iml" filepath="$PROJECT_DIR$/.idea/scm-sdk.iml" />
+    </modules>
+  </component>
+</project>

+ 9 - 0
.idea/scm-sdk.iml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+  <component name="Go" enabled="true" />
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="" vcs="Git" />
+  </component>
+</project>

+ 7 - 0
go.mod

@@ -0,0 +1,7 @@
+module git.sxidc.com/service-supports/scm-sdk
+
+go 1.19
+
+require github.com/go-resty/resty/v2 v2.7.0
+
+require golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect

+ 9 - 0
go.sum

@@ -0,0 +1,9 @@
+github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY=
+github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I=
+golang.org/x/net v0.0.0-20211029224645-99673261e6eb h1:pirldcYWx7rx7kE5r+9WsOXPXK0+WH5+uZ7uPmJ44uM=
+golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
+golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
+golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

+ 23 - 0
response.go

@@ -0,0 +1,23 @@
+package scm_sdk
+
+type (
+	msgResponse struct {
+		Success bool   `json:"success"`
+		ErrCode uint32 `json:"errCode"`
+		Msg     string `json:"msg"`
+	}
+
+	GetCurrentConfigurationResponse struct {
+		msgResponse
+		Info *ConfigurationInfo `json:"info"`
+	}
+
+	ConfigurationInfo struct {
+		Namespace string `json:"namespace"`
+		Name      string `json:"name"`
+		Tag       string `json:"tag"`
+		Describe  string `json:"describe"`
+		Content   string `json:"content"`
+		IsCurrent bool   `json:"isCurrent"`
+	}
+)

+ 51 - 0
sdk.go

@@ -0,0 +1,51 @@
+package scm_sdk
+
+import (
+	"encoding/json"
+	"errors"
+	"github.com/go-resty/resty/v2"
+	"net/url"
+	"time"
+)
+
+const (
+	timeoutSec = 30
+)
+
+const (
+	getCurrentConfigurationUrl = "/scm/api/inner/configuration/current/query"
+)
+
+func GetCurrentConfiguration(baseUrl string, namespace string, name string) (*ConfigurationInfo, error) {
+	fullUrl, err := url.JoinPath(baseUrl, getCurrentConfigurationUrl)
+	if err != nil {
+		return nil, err
+	}
+
+	resp, err := resty.New().SetTimeout(timeoutSec*time.Second).R().
+		SetHeader("Content-Type", "application/json").
+		SetQueryParams(map[string]string{
+			"namespace": namespace,
+			"name":      name,
+		}).
+		Get(fullUrl)
+	if err != nil {
+		return nil, err
+	}
+
+	if resp.IsError() {
+		return nil, errors.New(resp.Status())
+	}
+
+	response := new(GetCurrentConfigurationResponse)
+	err = json.Unmarshal(resp.Body(), response)
+	if err != nil {
+		return nil, err
+	}
+
+	if !response.Success {
+		return nil, errors.New(response.Msg)
+	}
+
+	return response.Info, nil
+}