|
| 1 | +package tap |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + kitlog "github.com/go-kit/log" |
| 7 | + "github.com/incident-io/singer-tap/client" |
| 8 | + "github.com/incident-io/singer-tap/model" |
| 9 | + "github.com/pkg/errors" |
| 10 | + "github.com/samber/lo" |
| 11 | +) |
| 12 | + |
| 13 | +func init() { |
| 14 | + register(&StreamIncidentUpdates{}) |
| 15 | +} |
| 16 | + |
| 17 | +type StreamIncidentUpdates struct { |
| 18 | +} |
| 19 | + |
| 20 | +func (s *StreamIncidentUpdates) Output() *Output { |
| 21 | + return &Output{ |
| 22 | + Type: OutputTypeSchema, |
| 23 | + Stream: "incident_updates", |
| 24 | + Schema: &model.Schema{ |
| 25 | + HasAdditionalProperties: false, |
| 26 | + Type: []string{"object"}, |
| 27 | + Properties: model.IncidentUpdateV2.Schema().Properties, |
| 28 | + }, |
| 29 | + KeyProperties: []string{"id"}, |
| 30 | + BookmarkProperties: []string{}, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func (s *StreamIncidentUpdates) GetRecords(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses) ([]map[string]any, error) { |
| 35 | + var ( |
| 36 | + after *string |
| 37 | + pageSize = 250 |
| 38 | + results = []map[string]any{} |
| 39 | + ) |
| 40 | + |
| 41 | + for { |
| 42 | + logger.Log("msg", "loading page", "page_size", pageSize, "after", after) |
| 43 | + page, err := cl.IncidentUpdatesV2ListWithResponse(ctx, &client.IncidentUpdatesV2ListParams{ |
| 44 | + PageSize: &pageSize, |
| 45 | + After: after, |
| 46 | + }) |
| 47 | + if err != nil { |
| 48 | + return nil, errors.Wrap(err, "listing incident updates") |
| 49 | + } |
| 50 | + |
| 51 | + for _, element := range page.JSON200.IncidentUpdates { |
| 52 | + results = append(results, model.IncidentUpdateV2.Serialize(element)) |
| 53 | + } |
| 54 | + if count := len(page.JSON200.IncidentUpdates); count == 0 { |
| 55 | + return results, nil // end pagination |
| 56 | + } else { |
| 57 | + after = lo.ToPtr(page.JSON200.IncidentUpdates[count-1].Id) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments