sdk.go 988 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package scm_sdk
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/go-resty/resty/v2"
  6. "net/url"
  7. "time"
  8. )
  9. const (
  10. timeoutSec = 30
  11. )
  12. const (
  13. getCurrentConfigurationUrl = "/scm/api/inner/configuration/current/query"
  14. )
  15. func GetCurrentConfiguration(baseUrl string, namespace string, name string) (*ConfigurationInfo, error) {
  16. fullUrl, err := url.JoinPath(baseUrl, getCurrentConfigurationUrl)
  17. if err != nil {
  18. return nil, err
  19. }
  20. resp, err := resty.New().SetTimeout(timeoutSec*time.Second).R().
  21. SetHeader("Content-Type", "application/json").
  22. SetQueryParams(map[string]string{
  23. "namespace": namespace,
  24. "name": name,
  25. }).
  26. Get(fullUrl)
  27. if err != nil {
  28. return nil, err
  29. }
  30. if resp.IsError() {
  31. return nil, errors.New(resp.Status())
  32. }
  33. response := new(GetCurrentConfigurationResponse)
  34. err = json.Unmarshal(resp.Body(), response)
  35. if err != nil {
  36. return nil, err
  37. }
  38. if !response.Success {
  39. return nil, errors.New(response.Msg)
  40. }
  41. return response.Info, nil
  42. }