Skip to content

Commit

Permalink
[FIX] pagination fix (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-yudovich authored Sep 29, 2022
1 parent facf6f3 commit e6f6d8a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
19 changes: 10 additions & 9 deletions cf/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,25 @@ type CCListOrganizationsResponse struct {

func (pc *PlatformClient) ListOrganizationsByQuery(ctx context.Context, query url.Values) ([]CCOrganization, error) {
var organizations []CCOrganization
var organizationsResponse CCListOrganizationsResponse
request := PlatformClientRequest{
CTX: ctx,
URL: "/v3/organizations?" + query.Encode(),
Method: http.MethodGet,
ResponseBody: &organizationsResponse,
}

requestUrl := "/v3/organizations?" + query.Encode()
for {
var organizationsResponse CCListOrganizationsResponse
request := PlatformClientRequest{
CTX: ctx,
URL: requestUrl,
Method: http.MethodGet,
ResponseBody: &organizationsResponse,
}
_, err := pc.MakeRequest(request)
if err != nil {
return []CCOrganization{}, errors.Wrap(err, "Error requesting organizations")
}

organizations = append(organizations, organizationsResponse.Resources...)

request.URL = organizationsResponse.Pagination.Next.Href
if request.URL == "" {
requestUrl = organizationsResponse.Pagination.Next.Href
if requestUrl == "" {
break
}
}
Expand Down
19 changes: 10 additions & 9 deletions cf/service_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,23 +280,24 @@ func (pc *PlatformClient) UpdateBroker(ctx context.Context, r *platform.UpdateSe

func (pc *PlatformClient) ListServiceBrokersByQuery(ctx context.Context, query url.Values) ([]CCServiceBroker, error) {
var serviceBrokers []CCServiceBroker
var serviceBrokersResponse CCListServiceBrokersResponse
request := PlatformClientRequest{
CTX: ctx,
URL: "/v3/service_brokers?" + query.Encode(),
Method: http.MethodGet,
ResponseBody: &serviceBrokersResponse,
}

requestUrl := "/v3/service_brokers?" + query.Encode()
for {
var serviceBrokersResponse CCListServiceBrokersResponse
request := PlatformClientRequest{
CTX: ctx,
URL: requestUrl,
Method: http.MethodGet,
ResponseBody: &serviceBrokersResponse,
}
_, err := pc.MakeRequest(request)
if err != nil {
return []CCServiceBroker{}, err
}

serviceBrokers = append(serviceBrokers, serviceBrokersResponse.Resources...)
request.URL = serviceBrokersResponse.Pagination.Next.Href
if request.URL == "" {
requestUrl = serviceBrokersResponse.Pagination.Next.Href
if requestUrl == "" {
break
}
}
Expand Down
19 changes: 10 additions & 9 deletions cf/service_offerings.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ type CCListServiceOfferingsResponse struct {

func (pc *PlatformClient) ListServiceOfferingsByQuery(ctx context.Context, query url.Values) ([]ServiceOffering, error) {
var serviceOfferings []ServiceOffering
var serviceOfferingsResponse CCListServiceOfferingsResponse
request := PlatformClientRequest{
CTX: ctx,
URL: "/v3/service_offerings?" + query.Encode(),
Method: http.MethodGet,
ResponseBody: &serviceOfferingsResponse,
}

requestUrl := "/v3/service_offerings?" + query.Encode()
for {
var serviceOfferingsResponse CCListServiceOfferingsResponse
request := PlatformClientRequest{
CTX: ctx,
URL: requestUrl,
Method: http.MethodGet,
ResponseBody: &serviceOfferingsResponse,
}
_, err := pc.MakeRequest(request)
if err != nil {
return []ServiceOffering{}, errors.Wrap(err, "Error requesting service offerings")
Expand All @@ -57,8 +58,8 @@ func (pc *PlatformClient) ListServiceOfferingsByQuery(ctx context.Context, query
})
}

request.URL = serviceOfferingsResponse.Pagination.Next.Href
if request.URL == "" {
requestUrl = serviceOfferingsResponse.Pagination.Next.Href
if requestUrl == "" {
break
}
}
Expand Down
19 changes: 10 additions & 9 deletions cf/service_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ type CCListServicePlansResponse struct {

func (pc *PlatformClient) ListServicePlansByQuery(ctx context.Context, query url.Values) ([]ServicePlan, error) {
var servicePlans []ServicePlan
var servicePlansResponse CCListServicePlansResponse
request := PlatformClientRequest{
CTX: ctx,
URL: "/v3/service_plans?" + query.Encode(),
Method: http.MethodGet,
ResponseBody: &servicePlansResponse,
}

requestUrl := "/v3/service_plans?" + query.Encode()
for {
var servicePlansResponse CCListServicePlansResponse
request := PlatformClientRequest{
CTX: ctx,
URL: requestUrl,
Method: http.MethodGet,
ResponseBody: &servicePlansResponse,
}
_, err := pc.MakeRequest(request)
if err != nil {
return []ServicePlan{}, errors.Wrap(err, "Error requesting service plans")
Expand All @@ -68,8 +69,8 @@ func (pc *PlatformClient) ListServicePlansByQuery(ctx context.Context, query url
})
}

request.URL = servicePlansResponse.Pagination.Next.Href
if request.URL == "" {
requestUrl = servicePlansResponse.Pagination.Next.Href
if requestUrl == "" {
break
}
}
Expand Down

0 comments on commit e6f6d8a

Please sign in to comment.