amo/events.go
2024-05-30 13:43:10 +05:00

39 lines
861 B
Go

package amo
import "surdeus.su/core/amo/events"
import "surdeus.su/core/amo/api"
import "surdeus.su/core/ss/urlenc"
import "errors"
import "fmt"
// Returns the events from AmoCRM by specified request.
// If there are no such events returns an empty slice of events.
func (client *Client) GetEvents(
opts ...urlenc.Builder,
) ([]events.Event, error) {
res := fmt.Sprintf(
"/api/v4/events?%s",
urlenc.Join(opts...).Encode(),
)
ret := []events.Event{}
for {
resp := events.EventsResponse{}
err := client.API.Get(res, &resp)
if err != nil {
// Return empty if no content avialable.
if errors.Is(err, api.ErrNoContent) {
break
}
return nil, err
}
ret = append(ret, resp.Embedded.Events...)
if resp.Links.Next.Href == "" {
break
}
//time.Sleep(time.Millisecond * 300)
res = resp.Links.Next.Href
}
return ret, nil
}