Skip to content

Commit

Permalink
Updating GetProjects, GetResults, GetResultsForCase, GetRuns, GetSect…
Browse files Browse the repository at this point in the history
…ions to support pagination
  • Loading branch information
Renzo Robles (AWF) committed Sep 10, 2021
1 parent 2287ca0 commit 8cb37b9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
8 changes: 4 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ func (c *Client) sendRequest(method, uri string, data, v interface{}) error {
}

type Links struct {
Next string `json:"next"`
Prev string `json:"prev"`
Next *string `json:"next"`
Prev *string `json:"prev"`
}

func (c *Client) sendRequestBeta(method, uri string, data, v interface{}, itemsKeyName string) error {
Expand All @@ -134,8 +134,8 @@ func (c *Client) sendRequestBeta(method, uri string, data, v interface{}, itemsK

returnItems = tempItems

for err == nil && links.Next != "" && len(tempItems) == 250 {
nextUri := strings.TrimPrefix(links.Next, "/api/v2/")
for err == nil && links.Next != nil {
nextUri := strings.TrimPrefix(*links.Next, "/api/v2/")
err = c.sendRequest("GET", nextUri, nil, &wraperMap)
if err == nil {
json.Unmarshal(wraperMap[itemsKeyName], &tempItems)
Expand Down
7 changes: 6 additions & 1 deletion project.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ func (c *Client) GetProjects(isCompleted ...bool) ([]Project, error) {
}

returnProjects := []Project{}
err := c.sendRequest("GET", uri, nil, &returnProjects)
var err error
if c.useBetaApi {
err = c.sendRequestBeta("GET", uri, nil, &returnProjects, "projects")
} else {
err = c.sendRequest("GET", uri, nil, &returnProjects)
}
return returnProjects, err
}

Expand Down
14 changes: 12 additions & 2 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ func (c *Client) GetResults(testID int, filters ...RequestFilterForCaseResults)
if len(filters) > 0 {
uri = applyFiltersForCaseResults(uri, filters[0])
}
err := c.sendRequest("GET", uri, nil, &returnResults)
var err error
if c.useBetaApi {
err = c.sendRequestBeta("GET", uri, nil, &returnResults, "results")
} else {
err = c.sendRequest("GET", uri, nil, &returnResults)
}
return returnResults, err
}

Expand All @@ -107,7 +112,12 @@ func (c *Client) GetResultsForCase(runID, caseID int, filters ...RequestFilterFo
if len(filters) > 0 {
uri = applyFiltersForCaseResults(uri, filters[0])
}
err := c.sendRequest("GET", uri, nil, &returnResults)
var err error
if c.useBetaApi {
err = c.sendRequestBeta("GET", uri, nil, &returnResults, "results")
} else {
err = c.sendRequest("GET", uri, nil, &returnResults)
}
return returnResults, err
}

Expand Down
7 changes: 6 additions & 1 deletion run.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ func (c *Client) GetRuns(projectID int, filters ...RequestFilterForRun) ([]Run,
}

returnRun := []Run{}
err := c.sendRequest("GET", uri, nil, &returnRun)
var err error
if c.useBetaApi {
err = c.sendRequestBeta("GET", uri, nil, &returnRun, "runs")
} else {
err = c.sendRequest("GET", uri, nil, &returnRun)
}
return returnRun, err
}

Expand Down
7 changes: 6 additions & 1 deletion section.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ func (c *Client) GetSections(projectID int, suiteID ...int) ([]Section, error) {
if len(suiteID) > 0 {
uri = uri + "&suite_id=" + strconv.Itoa(suiteID[0])
}
err := c.sendRequest("GET", uri, nil, &returnSection)
var err error
if c.useBetaApi {
err = c.sendRequestBeta("GET", uri, nil, &returnSection, "sections")
} else {
err = c.sendRequest("GET", uri, nil, &returnSection)
}
return returnSection, err
}

Expand Down

0 comments on commit 8cb37b9

Please sign in to comment.