Skip to content

Commit

Permalink
Merge pull request #54 from 3scale/pagination
Browse files Browse the repository at this point in the history
THREESCALE-8572 Fetch multiple pages on backend endpoints implementing page and per_page
  • Loading branch information
eguzki authored Mar 30, 2023
2 parents 37a949c + 566bf9b commit 8df1f04
Show file tree
Hide file tree
Showing 2 changed files with 575 additions and 58 deletions.
111 changes: 110 additions & 1 deletion client/backend_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const (
backendUsageListResourceEndpoint = "/admin/api/services/%d/backend_usages.json"
backendUsageResourceEndpoint = "/admin/api/services/%d/backend_usages/%d.json"
BACKENDS_PER_PAGE int = 500
BACKEND_METRICS_PER_PAGE int = 500
BACKEND_MAPPINGRULES_PER_PAGE int = 500
)

// ListBackends List existing backends
Expand Down Expand Up @@ -166,12 +168,48 @@ func (c *ThreeScaleClient) UpdateBackendApi(id int64, params Params) (*BackendAp

// ListBackendapiMethods List existing backend methods
func (c *ThreeScaleClient) ListBackendapiMethods(backendapiID, hitsID int64) (*MethodList, error) {
// Keep asking until the results length is lower than "per_page" param
currentPage := 1
methodList := &MethodList{}

allResultsPerPage := false
for next := true; next; next = allResultsPerPage {
tmpList, err := c.ListBackendapiMethodsPerPage(backendapiID, hitsID, currentPage, BACKEND_METRICS_PER_PAGE)
if err != nil {
return nil, err
}

methodList.Methods = append(methodList.Methods, tmpList.Methods...)

allResultsPerPage = len(tmpList.Methods) == BACKEND_METRICS_PER_PAGE
currentPage += 1
}

return methodList, nil
}

// ListBackendapiMethodsPerPage List existing backend methods for a given page
// paginationValues[0] = Page in the paginated list. Defaults to 1 for the API, as the client will not send the page param.
// paginationValues[1] = Number of results per page. Default and max is 500 for the aPI, as the client will not send the per_page param.
func (c *ThreeScaleClient) ListBackendapiMethodsPerPage(backendapiID, hitsID int64, paginationValues ...int) (*MethodList, error) {
queryValues := url.Values{}

if len(paginationValues) > 0 {
queryValues.Add("page", strconv.Itoa(paginationValues[0]))
}

if len(paginationValues) > 1 {
queryValues.Add("per_page", strconv.Itoa(paginationValues[1]))
}

endpoint := fmt.Sprintf(backendMethodListResourceEndpoint, backendapiID, hitsID)
req, err := c.buildGetReq(endpoint)
if err != nil {
return nil, err
}

req.URL.RawQuery = queryValues.Encode()

resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
Expand Down Expand Up @@ -274,12 +312,48 @@ func (c *ThreeScaleClient) UpdateBackendApiMethod(backendapiID, hitsID, methodID

// ListBackendapiMetrics List existing backend metric
func (c *ThreeScaleClient) ListBackendapiMetrics(backendapiID int64) (*MetricJSONList, error) {
// Keep asking until the results length is lower than "per_page" param
currentPage := 1
metricList := &MetricJSONList{}

allResultsPerPage := false
for next := true; next; next = allResultsPerPage {
tmpList, err := c.ListBackendapiMetricsPerPage(backendapiID, currentPage, BACKEND_METRICS_PER_PAGE)
if err != nil {
return nil, err
}

metricList.Metrics = append(metricList.Metrics, tmpList.Metrics...)

allResultsPerPage = len(tmpList.Metrics) == BACKEND_METRICS_PER_PAGE
currentPage += 1
}

return metricList, nil
}

// ListBackendapiMetricsPerPage List existing backend metric for a given page
// paginationValues[0] = Page in the paginated list. Defaults to 1 for the API, as the client will not send the page param.
// paginationValues[1] = Number of results per page. Default and max is 500 for the aPI, as the client will not send the per_page param.
func (c *ThreeScaleClient) ListBackendapiMetricsPerPage(backendapiID int64, paginationValues ...int) (*MetricJSONList, error) {
queryValues := url.Values{}

if len(paginationValues) > 0 {
queryValues.Add("page", strconv.Itoa(paginationValues[0]))
}

if len(paginationValues) > 1 {
queryValues.Add("per_page", strconv.Itoa(paginationValues[1]))
}

endpoint := fmt.Sprintf(backendMetricListResourceEndpoint, backendapiID)
req, err := c.buildGetReq(endpoint)
if err != nil {
return nil, err
}

req.URL.RawQuery = queryValues.Encode()

resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
Expand Down Expand Up @@ -380,14 +454,49 @@ func (c *ThreeScaleClient) UpdateBackendApiMetric(backendapiID, metricID int64,
return item, err
}

// ListBackendapiMappingRules List existing backend mapping rules
func (c *ThreeScaleClient) ListBackendapiMappingRules(backendapiID int64) (*MappingRuleJSONList, error) {
// Keep asking until the results length is lower than "per_page" param
currentPage := 1
mpList := &MappingRuleJSONList{}

allResultsPerPage := false
for next := true; next; next = allResultsPerPage {
tmpList, err := c.ListBackendapiMappingRulesPerPage(backendapiID, currentPage, BACKEND_MAPPINGRULES_PER_PAGE)
if err != nil {
return nil, err
}

mpList.MappingRules = append(mpList.MappingRules, tmpList.MappingRules...)

allResultsPerPage = len(tmpList.MappingRules) == BACKEND_MAPPINGRULES_PER_PAGE
currentPage += 1
}

return mpList, nil
}

// ListBackendapiMappingRulesPerPage List existing backend mapping rules for a given page
// paginationValues[0] = Page in the paginated list. Defaults to 1 for the API, as the client will not send the page param.
// paginationValues[1] = Number of results per page. Default and max is 500 for the aPI, as the client will not send the per_page param.
func (c *ThreeScaleClient) ListBackendapiMappingRulesPerPage(backendapiID int64, paginationValues ...int) (*MappingRuleJSONList, error) {
queryValues := url.Values{}

if len(paginationValues) > 0 {
queryValues.Add("page", strconv.Itoa(paginationValues[0]))
}

if len(paginationValues) > 1 {
queryValues.Add("per_page", strconv.Itoa(paginationValues[1]))
}

endpoint := fmt.Sprintf(backendMRListResourceEndpoint, backendapiID)
req, err := c.buildGetReq(endpoint)
if err != nil {
return nil, err
}

req.URL.RawQuery = queryValues.Encode()

resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 8df1f04

Please sign in to comment.