data_source.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. )
  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(response.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(response.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(response.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. }