data_container.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package client
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. )
  8. const (
  9. createDataContainerUrl = "/ds/api/v1/dataContainer/create"
  10. deleteDataContainerUrl = "/ds/api/v1/dataContainer/delete"
  11. getDataContainersUrl = "/ds/api/v1/dataContainer/query"
  12. )
  13. func (c *Client) CreateDataContainer(token string, baseUrl string, namespace string, dataSource string, name string, spec map[string]any) error {
  14. fullUrl, err := url.JoinPath(baseUrl, createDataContainerUrl)
  15. if err != nil {
  16. return err
  17. }
  18. resp := new(MsgResponse)
  19. specJsonBytes, err := json.Marshal(spec)
  20. if err != nil {
  21. return err
  22. }
  23. err = c.post(token, fullUrl, map[string]any{
  24. "namespace": namespace,
  25. "dataSource": dataSource,
  26. "name": name,
  27. "spec": string(specJsonBytes),
  28. }, resp)
  29. if err != nil {
  30. return err
  31. }
  32. if !resp.Success {
  33. return fmt.Errorf(resp.Msg)
  34. }
  35. return nil
  36. }
  37. func (c *Client) DeleteDataContainer(token string, baseUrl string, namespace string, dataSource string, name string) error {
  38. fullUrl, err := url.JoinPath(baseUrl, deleteDataContainerUrl)
  39. if err != nil {
  40. return err
  41. }
  42. resp := new(MsgResponse)
  43. err = c.post(token, fullUrl, map[string]any{
  44. "namespace": namespace,
  45. "dataSource": dataSource,
  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) GetDataContainers(token string, baseUrl string, namespace string, dataSource string, name string, pageNo int, pageSize int) ([]DataContainerInfo, error) {
  57. fullUrl, err := url.JoinPath(baseUrl, getDataContainersUrl)
  58. if err != nil {
  59. return nil, err
  60. }
  61. resp := new(InfosResponse[DataContainerInfo])
  62. err = c.get(token, fullUrl, map[string]string{
  63. "namespace": namespace,
  64. "dataSource": dataSource,
  65. "name": name,
  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. }