apps: Support pagination. (#443)

This commit is contained in:
Andrew Starr-Bochicchio 2021-04-09 17:23:59 -04:00 committed by GitHub
parent dd4bfeaea9
commit 6e8018121d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 5 deletions

34
apps.go
View File

@ -73,7 +73,9 @@ type appRoot struct {
} }
type appsRoot struct { type appsRoot struct {
Apps []*App `json:"apps"` Apps []*App `json:"apps"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
} }
type deploymentRoot struct { type deploymentRoot struct {
@ -82,6 +84,8 @@ type deploymentRoot struct {
type deploymentsRoot struct { type deploymentsRoot struct {
Deployments []*Deployment `json:"deployments"` Deployments []*Deployment `json:"deployments"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
} }
type appTierRoot struct { type appTierRoot struct {
@ -143,6 +147,11 @@ func (s *AppsServiceOp) Get(ctx context.Context, appID string) (*App, *Response,
// List apps. // List apps.
func (s *AppsServiceOp) List(ctx context.Context, opts *ListOptions) ([]*App, *Response, error) { func (s *AppsServiceOp) List(ctx context.Context, opts *ListOptions) ([]*App, *Response, error) {
path := appsBasePath path := appsBasePath
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil) req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -152,6 +161,15 @@ func (s *AppsServiceOp) List(ctx context.Context, opts *ListOptions) ([]*App, *R
if err != nil { if err != nil {
return nil, resp, err return nil, resp, err
} }
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Apps, resp, nil return root.Apps, resp, nil
} }
@ -219,6 +237,11 @@ func (s *AppsServiceOp) GetDeployment(ctx context.Context, appID, deploymentID s
// ListDeployments lists an app deployments. // ListDeployments lists an app deployments.
func (s *AppsServiceOp) ListDeployments(ctx context.Context, appID string, opts *ListOptions) ([]*Deployment, *Response, error) { func (s *AppsServiceOp) ListDeployments(ctx context.Context, appID string, opts *ListOptions) ([]*Deployment, *Response, error) {
path := fmt.Sprintf("%s/%s/deployments", appsBasePath, appID) path := fmt.Sprintf("%s/%s/deployments", appsBasePath, appID)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil) req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -228,6 +251,15 @@ func (s *AppsServiceOp) ListDeployments(ctx context.Context, appID string, opts
if err != nil { if err != nil {
return nil, resp, err return nil, resp, err
} }
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Deployments, resp, nil return root.Deployments, resp, nil
} }

View File

@ -217,12 +217,16 @@ func TestApps_ListApp(t *testing.T) {
mux.HandleFunc("/v2/apps", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/v2/apps", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet) testMethod(t, r, http.MethodGet)
json.NewEncoder(w).Encode(&appsRoot{Apps: []*App{&testApp}}) json.NewEncoder(w).Encode(&appsRoot{Apps: []*App{&testApp}, Meta: &Meta{Total: 1}, Links: &Links{}})
}) })
apps, _, err := client.Apps.List(ctx, nil) apps, resp, err := client.Apps.List(ctx, nil)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, []*App{&testApp}, apps) assert.Equal(t, []*App{&testApp}, apps)
assert.Equal(t, 1, resp.Meta.Total)
currentPage, err := resp.Links.CurrentPage()
require.NoError(t, err)
assert.Equal(t, 1, currentPage)
} }
func TestApps_UpdateApp(t *testing.T) { func TestApps_UpdateApp(t *testing.T) {
@ -375,12 +379,16 @@ func TestApps_ListDeployments(t *testing.T) {
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/deployments", testApp.ID), func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/deployments", testApp.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet) testMethod(t, r, http.MethodGet)
json.NewEncoder(w).Encode(&deploymentsRoot{Deployments: []*Deployment{&testDeployment}}) json.NewEncoder(w).Encode(&deploymentsRoot{Deployments: []*Deployment{&testDeployment}, Meta: &Meta{Total: 1}, Links: &Links{}})
}) })
deployments, _, err := client.Apps.ListDeployments(ctx, testApp.ID, nil) deployments, resp, err := client.Apps.ListDeployments(ctx, testApp.ID, nil)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, []*Deployment{&testDeployment}, deployments) assert.Equal(t, []*Deployment{&testDeployment}, deployments)
assert.Equal(t, 1, resp.Meta.Total)
currentPage, err := resp.Links.CurrentPage()
require.NoError(t, err)
assert.Equal(t, 1, currentPage)
} }
func TestApps_GetLogs(t *testing.T) { func TestApps_GetLogs(t *testing.T) {