Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mechanism to list more than 100 incidents #429

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pagerduty
import (
"context"
"fmt"
"net/http"

"github.com/google/go-querystring/query"
)
Expand Down Expand Up @@ -163,6 +164,10 @@ func (c *Client) ListIncidents(o ListIncidentsOptions) (*ListIncidentsResponse,
return c.ListIncidentsWithContext(context.Background(), o)
}

func (c *Client) ListIncidentsPaginated(o ListIncidentsOptions) ([]Incident, error) {
Copy link
Collaborator

@theckman theckman Mar 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should be removed, as we don't want to introduce new methods that don't accept a context.Contex.

return c.ListIncidentsPaginatedWithContext(context.Background(), o)
}

// ListIncidentsWithContext lists existing incidents.
func (c *Client) ListIncidentsWithContext(ctx context.Context, o ListIncidentsOptions) (*ListIncidentsResponse, error) {
v, err := query.Values(o)
Expand All @@ -183,6 +188,37 @@ func (c *Client) ListIncidentsWithContext(ctx context.Context, o ListIncidentsOp
return &result, nil
}

// ListIncidentsPaginated lists existing services processing paginated responses
func (c *Client) ListIncidentsPaginatedWithContext(ctx context.Context, o ListIncidentsOptions) ([]Incident, error) {
Copy link
Collaborator

@theckman theckman Mar 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this method signature aligns with future plans of the project. The WithContext should be removed from the name, as this is a new method and we have no reason to support a non-context APIs for new things. Also, it should accept an include function that allows filtering of unwanted items.

See #295 and #296 for more info.

v, err := query.Values(o)
if err != nil {
return nil, err
}

var incidents []Incident

responseHandler := func(response *http.Response) (APIListObject, error) {
var result ListIncidentsResponse
if err := c.decodeJSON(response, &result); err != nil {
return APIListObject{}, err
}

incidents = append(incidents, result.Incidents...)

return APIListObject{
More: result.More,
Offset: result.Offset,
Limit: result.Limit,
}, nil
}

if err := c.pagedGet(ctx, "/incidents?"+v.Encode(), responseHandler); err != nil {
return nil, err
}

return incidents, nil
}

// createIncidentResponse is returned from the API when creating a response.
type createIncidentResponse struct {
Incident Incident `json:"incident"`
Expand Down
54 changes: 54 additions & 0 deletions incident_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"testing"
)

Expand Down Expand Up @@ -41,6 +42,59 @@ func TestIncident_List(t *testing.T) {
testEqual(t, want, res)
}

func TestIncident_ListPaginated(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/incidents", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
offsetStr := r.URL.Query()["offset"][0]
offset, _ := strconv.ParseInt(offsetStr, 10, 32)

var more string
if offset == 0 {
more = "true"
} else {
more = "false"
}
resp := fmt.Sprintf(`{"incidents": [{"id": "%d"}],
"More": %s,
"Offset": %d,
"Limit": 1}`, offset, more, offset)
_, _ = w.Write([]byte(resp))
})

listObj := APIListObject{Limit: 1, Offset: 0, More: false, Total: 0}
client := defaultTestClient(server.URL, "foo")
opts := ListIncidentsOptions{
Limit: listObj.Limit,
Offset: listObj.Offset,
TeamIDs: []string{},
TimeZone: "foo",
SortBy: "bar",
Includes: []string{},
}
res, err := client.ListIncidentsPaginated(opts)

want := []Incident{
{
APIObject: APIObject{
ID: "0",
},
},
{
APIObject: APIObject{
ID: "1",
},
},
}

if err != nil {
t.Fatal(err)
}
testEqual(t, want, res)
}

func TestIncident_Create(t *testing.T) {
setup()
defer teardown()
Expand Down