namespace.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package client
  2. import (
  3. "fmt"
  4. "net/url"
  5. "strconv"
  6. )
  7. const (
  8. createNamespaceUrl = "/ds/api/v1/namespace/create"
  9. deleteNamespaceUrl = "/ds/api/v1/namespace/delete"
  10. getNamespacesUrl = "/ds/api/v1/namespace/query"
  11. )
  12. func (c *Client) CreateNamespace(token string, baseUrl string, name string) error {
  13. fullUrl, err := url.JoinPath(baseUrl, createNamespaceUrl)
  14. if err != nil {
  15. return err
  16. }
  17. resp := new(MsgResponse)
  18. err = c.post(token, fullUrl, map[string]any{
  19. "name": name,
  20. }, resp)
  21. if err != nil {
  22. return err
  23. }
  24. if !resp.Success {
  25. return fmt.Errorf(resp.Msg)
  26. }
  27. return nil
  28. }
  29. func (c *Client) DeleteNamespace(token string, baseUrl string, name string) error {
  30. fullUrl, err := url.JoinPath(baseUrl, deleteNamespaceUrl)
  31. if err != nil {
  32. return err
  33. }
  34. resp := new(MsgResponse)
  35. err = c.post(token, fullUrl, map[string]any{
  36. "name": name,
  37. }, resp)
  38. if err != nil {
  39. return err
  40. }
  41. if !resp.Success {
  42. return fmt.Errorf(resp.Msg)
  43. }
  44. return nil
  45. }
  46. func (c *Client) GetNamespaces(token string, baseUrl string, name string, pageNo int, pageSize int) ([]NamespaceInfo, error) {
  47. fullUrl, err := url.JoinPath(baseUrl, getNamespacesUrl)
  48. if err != nil {
  49. return nil, err
  50. }
  51. resp := new(InfosResponse[NamespaceInfo])
  52. err = c.get(token, fullUrl, map[string]string{
  53. "name": name,
  54. "pageNo": strconv.Itoa(pageNo),
  55. "pageSize": strconv.Itoa(pageSize),
  56. }, resp)
  57. if err != nil {
  58. return nil, err
  59. }
  60. if !resp.Success {
  61. return nil, fmt.Errorf(resp.Msg)
  62. }
  63. return resp.Infos, nil
  64. }