events.go 861 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package amo
  2. import "surdeus.su/core/amo/events"
  3. import "surdeus.su/core/amo/api"
  4. import "surdeus.su/core/ss/urlenc"
  5. import "errors"
  6. import "fmt"
  7. // Returns the events from AmoCRM by specified request.
  8. // If there are no such events returns an empty slice of events.
  9. func (client *Client) GetEvents(
  10. opts ...urlenc.Builder,
  11. ) ([]events.Event, error) {
  12. res := fmt.Sprintf(
  13. "/api/v4/events?%s",
  14. urlenc.Join(opts...).Encode(),
  15. )
  16. ret := []events.Event{}
  17. for {
  18. resp := events.EventsResponse{}
  19. err := client.API.Get(res, &resp)
  20. if err != nil {
  21. // Return empty if no content avialable.
  22. if errors.Is(err, api.ErrNoContent) {
  23. break
  24. }
  25. return nil, err
  26. }
  27. ret = append(ret, resp.Embedded.Events...)
  28. if resp.Links.Next.Href == "" {
  29. break
  30. }
  31. //time.Sleep(time.Millisecond * 300)
  32. res = resp.Links.Next.Href
  33. }
  34. return ret, nil
  35. }