data_source.go 2.4 KB

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