collection.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package api
  2. import (
  3. "context"
  4. "strconv"
  5. )
  6. func (c *Client) GetCollections(ctx context.Context, sinceTime int64) ([]Collection, error) {
  7. var res struct {
  8. Collections []Collection `json:"collections"`
  9. }
  10. r, err := c.restClient.R().
  11. SetContext(ctx).
  12. SetQueryParam("sinceTime", strconv.FormatInt(sinceTime, 10)).
  13. SetResult(&res).
  14. Get("/collections/v2")
  15. if r.IsError() {
  16. return nil, &ApiError{
  17. StatusCode: r.StatusCode(),
  18. Message: r.String(),
  19. }
  20. }
  21. return res.Collections, err
  22. }
  23. func (c *Client) GetFiles(ctx context.Context, collectionID, sinceTime int64) ([]File, bool, error) {
  24. var res struct {
  25. Files []File `json:"diff"`
  26. HasMore bool `json:"hasMore"`
  27. }
  28. r, err := c.restClient.R().
  29. SetContext(ctx).
  30. SetQueryParam("sinceTime", strconv.FormatInt(sinceTime, 10)).
  31. SetQueryParam("collectionID", strconv.FormatInt(collectionID, 10)).
  32. SetResult(&res).
  33. Get("/collections/v2/diff")
  34. if r.IsError() {
  35. return nil, false, &ApiError{
  36. StatusCode: r.StatusCode(),
  37. Message: r.String(),
  38. }
  39. }
  40. return res.Files, res.HasMore, err
  41. }
  42. // GetFile ..
  43. func (c *Client) GetFile(ctx context.Context, collectionID, fileID int64) (*File, error) {
  44. var res struct {
  45. File File `json:"file"`
  46. }
  47. r, err := c.restClient.R().
  48. SetContext(ctx).
  49. SetQueryParam("collectionID", strconv.FormatInt(collectionID, 10)).
  50. SetQueryParam("fileID", strconv.FormatInt(fileID, 10)).
  51. SetResult(&res).
  52. Get("/collections/file")
  53. if r.IsError() {
  54. return nil, &ApiError{
  55. StatusCode: r.StatusCode(),
  56. Message: r.String(),
  57. }
  58. }
  59. return &res.File, err
  60. }