data_source.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package client
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "git.sxidc.com/go-tools/api_binding/http_binding/response"
  6. "net/url"
  7. "strconv"
  8. )
  9. const (
  10. createDataSourceUrl = "/ds/api/v1/dataSource/create"
  11. deleteDataSourceUrl = "/ds/api/v1/dataSource/delete"
  12. getDataSourcesUrl = "/ds/api/v1/dataSource/query"
  13. getDataSourceSpec = "/ds/api/v1/dataSource/spec/get"
  14. )
  15. func (c *Client) CreateDataSource(token string, baseUrl string, namespace string, name string, typeStr string, spec map[string]any) error {
  16. fullUrl, err := url.JoinPath(baseUrl, createDataSourceUrl)
  17. if err != nil {
  18. return err
  19. }
  20. resp := new(response.MsgResponse)
  21. specJsonBytes, err := json.Marshal(spec)
  22. if err != nil {
  23. return err
  24. }
  25. err = c.post(token, fullUrl, map[string]any{
  26. "namespace": namespace,
  27. "name": name,
  28. "type": typeStr,
  29. "spec": string(specJsonBytes),
  30. }, resp)
  31. if err != nil {
  32. return err
  33. }
  34. if !resp.Success {
  35. return fmt.Errorf(resp.Msg)
  36. }
  37. return nil
  38. }
  39. func (c *Client) DeleteDataSource(token string, baseUrl string, namespace string, name string) error {
  40. fullUrl, err := url.JoinPath(baseUrl, deleteDataSourceUrl)
  41. if err != nil {
  42. return err
  43. }
  44. resp := new(response.MsgResponse)
  45. err = c.post(token, fullUrl, map[string]any{
  46. "namespace": namespace,
  47. "name": name,
  48. }, resp)
  49. if err != nil {
  50. return err
  51. }
  52. if !resp.Success {
  53. return fmt.Errorf(resp.Msg)
  54. }
  55. return nil
  56. }
  57. func (c *Client) GetDataSources(token string, baseUrl string, namespace string, name string, typeStr string, pageNo int, pageSize int) ([]DataSourceInfo, error) {
  58. fullUrl, err := url.JoinPath(baseUrl, getDataSourcesUrl)
  59. if err != nil {
  60. return nil, err
  61. }
  62. resp := new(response.InfosResponse[DataSourceInfo])
  63. err = c.get(token, fullUrl, map[string]string{
  64. "namespace": namespace,
  65. "name": name,
  66. "type": typeStr,
  67. "pageNo": strconv.Itoa(pageNo),
  68. "pageSize": strconv.Itoa(pageSize),
  69. }, resp)
  70. if err != nil {
  71. return nil, err
  72. }
  73. if !resp.Success {
  74. return nil, fmt.Errorf(resp.Msg)
  75. }
  76. return resp.Infos, nil
  77. }
  78. func (c *Client) GetDataSourceSpec(token string, baseUrl string, namespace string, name string) (string, error) {
  79. fullUrl, err := url.JoinPath(baseUrl, getDataSourceSpec)
  80. if err != nil {
  81. return "", err
  82. }
  83. resp := new(struct {
  84. response.MsgResponse
  85. Spec string `json:"spec"`
  86. })
  87. err = c.get(token, fullUrl, map[string]string{
  88. "namespace": namespace,
  89. "name": name,
  90. }, resp)
  91. if err != nil {
  92. return "", err
  93. }
  94. if !resp.Success {
  95. return "", fmt.Errorf(resp.Msg)
  96. }
  97. return resp.Spec, nil
  98. }