amo/events.go

40 lines
861 B
Go
Raw Normal View History

2024-05-30 11:43:10 +03:00
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
}