Skip to content

Commit

Permalink
feat: NextPage func for Resources Type (#126)
Browse files Browse the repository at this point in the history
feat: GetAllPages func for Resources Type
  • Loading branch information
domenicsim1 authored Jul 27, 2022
1 parent ebf968c commit 6bd1cc8
Show file tree
Hide file tree
Showing 53 changed files with 462 additions and 336 deletions.
11 changes: 6 additions & 5 deletions pkg/accounts/account_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api"
"github.com/dghubble/sling"
)

Expand Down Expand Up @@ -53,12 +54,12 @@ func (s *AccountService) Get(accountsQuery ...AccountsQuery) (*Accounts, error)
}
}

response, err := services.ApiGet(s.GetClient(), new(resources.Resources[AccountResource]), path)
response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*AccountResource]), path)
if err != nil {
return &Accounts{}, err
}

return ToAccounts(response.(*resources.Resources[AccountResource])), nil
return ToAccounts(response.(*resources.Resources[*AccountResource])), nil
}

// GetAll returns all accounts. If none are found or an error occurs, it
Expand All @@ -70,7 +71,7 @@ func (s *AccountService) GetAll() ([]IAccount, error) {
return ToAccountArray(items), err
}

_, err = services.ApiGet(s.GetClient(), &items, path)
_, err = api.ApiGet(s.GetClient(), &items, path)
return ToAccountArray(items), err
}

Expand All @@ -86,7 +87,7 @@ func (s *AccountService) GetByID(id string) (IAccount, error) {
return nil, err
}

resp, err := services.ApiGet(s.GetClient(), new(AccountResource), path)
resp, err := api.ApiGet(s.GetClient(), new(AccountResource), path)
if err != nil {
return nil, err
}
Expand All @@ -97,7 +98,7 @@ func (s *AccountService) GetByID(id string) (IAccount, error) {
// GetUsages lists the projects and deployments which are using an account.
func (s *AccountService) GetUsages(account IAccount) (*AccountUsage, error) {
path := account.GetLinks()[constants.LinkUsages]
resp, err := services.ApiGet(s.GetClient(), new(AccountUsage), path)
resp, err := api.ApiGet(s.GetClient(), new(AccountUsage), path)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/accounts/account_utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func ToAccount(accountResource *AccountResource) (IAccount, error) {
return account, nil
}

func ToAccounts(accountResources *resources.Resources[AccountResource]) *Accounts {
func ToAccounts(accountResources *resources.Resources[*AccountResource]) *Accounts {
return &Accounts{
Items: ToAccountArray(accountResources.Items),
PagedResults: accountResources.PagedResults,
Expand Down
37 changes: 33 additions & 4 deletions pkg/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"encoding/json"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api"
"github.com/dghubble/sling"
)

// Accounts defines a collection of accounts with built-in support for paged
// results.
type Accounts struct {
Items []IAccount `json:"Items"`
resources.PagedResults
}
type Accounts resources.Resources[IAccount]

// MarshalJSON returns an Accounts struct as its JSON encoding.
func (a *Accounts) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -127,3 +126,33 @@ func (a *Accounts) UnmarshalJSON(b []byte) error {

return nil
}

// GetNextPage retrives the next page from the links collection. If no next page
// exists it will return nill
func (r *Accounts) GetNextPage(client *sling.Sling) (*Accounts, error) {
if r.Links.PageNext == "" {
return nil, nil
}
response, err := api.ApiGet(client, new(resources.Resources[*AccountResource]), r.Links.PageNext)
if err != nil {
return nil, err
}
return ToAccounts(response.(*resources.Resources[*AccountResource])), nil
}

// GetAllPages will retrive all remaining next pages in the link collection
// and return the result as list of concatenated Items; Including the items
// from the base Resource.
func (r *Accounts) GetAllPages(client *sling.Sling) ([]IAccount, error) {
items := make([]IAccount, 0)
res := r
var err error
for res != nil {
items = append(items, res.Items...)
res, err = res.GetNextPage(client)
if err != nil {
return nil, err
}
}
return items, nil
}
11 changes: 6 additions & 5 deletions pkg/actions/community_action_template_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api"
"github.com/dghubble/sling"
"github.com/google/go-querystring/query"
)
Expand Down Expand Up @@ -42,20 +43,20 @@ func (s *CommunityActionTemplateService) getInstallationPath(resource CommunityA
// Get returns a collection of community action templates based on the criteria
// defined by its input query parameter. If an error occurs, an empty
// collection is returned along with the associated error.
func (s *CommunityActionTemplateService) Get(communityActionTemplatesQuery CommunityActionTemplatesQuery) (*resources.Resources[CommunityActionTemplate], error) {
func (s *CommunityActionTemplateService) Get(communityActionTemplatesQuery CommunityActionTemplatesQuery) (*resources.Resources[*CommunityActionTemplate], error) {
v, _ := query.Values(communityActionTemplatesQuery)
path := s.BasePath
encodedQueryString := v.Encode()
if len(encodedQueryString) > 0 {
path += "?" + encodedQueryString
}

resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[CommunityActionTemplate]), path)
resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*CommunityActionTemplate]), path)
if err != nil {
return &resources.Resources[CommunityActionTemplate]{}, err
return &resources.Resources[*CommunityActionTemplate]{}, err
}

return resp.(*resources.Resources[CommunityActionTemplate]), nil
return resp.(*resources.Resources[*CommunityActionTemplate]), nil
}

// GetAll returns all community action templates. If none can be found or an
Expand All @@ -81,7 +82,7 @@ func (s *CommunityActionTemplateService) GetByID(id string) (*CommunityActionTem
return nil, err
}

resp, err := services.ApiGet(s.GetClient(), new(CommunityActionTemplate), path)
resp, err := api.ApiGet(s.GetClient(), new(CommunityActionTemplate), path)
if err != nil {
return nil, err
}
Expand Down
17 changes: 9 additions & 8 deletions pkg/actiontemplates/action_template_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api"
"github.com/OctopusDeploy/go-octopusdeploy/v2/uritemplates"
"github.com/dghubble/sling"
"github.com/google/go-querystring/query"
Expand Down Expand Up @@ -63,20 +64,20 @@ func (s *ActionTemplateService) Add(actionTemplate *ActionTemplate) (*ActionTemp
// Get returns a collection of action templates based on the criteria defined
// by its input query parameter. If an error occurs, an empty collection is
// returned along with the associated error.
func (s *ActionTemplateService) Get(actionTemplatesQuery Query) (*resources.Resources[ActionTemplate], error) {
func (s *ActionTemplateService) Get(actionTemplatesQuery Query) (*resources.Resources[*ActionTemplate], error) {
v, _ := query.Values(actionTemplatesQuery)
path := s.BasePath
encodedQueryString := v.Encode()
if len(encodedQueryString) > 0 {
path += "?" + encodedQueryString
}

resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[ActionTemplate]), path)
resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*ActionTemplate]), path)
if err != nil {
return &resources.Resources[ActionTemplate]{}, err
return &resources.Resources[*ActionTemplate]{}, err
}

return resp.(*resources.Resources[ActionTemplate]), nil
return resp.(*resources.Resources[*ActionTemplate]), nil
}

// GetAll returns all action templates. If none can be found or an error
Expand All @@ -88,7 +89,7 @@ func (s *ActionTemplateService) GetAll() ([]*ActionTemplate, error) {
return items, err
}

_, err = services.ApiGet(s.GetClient(), &items, path)
_, err = api.ApiGet(s.GetClient(), &items, path)
return items, err
}

Expand All @@ -101,7 +102,7 @@ func (s *ActionTemplateService) GetCategories() ([]ActionTemplateCategory, error

path := s.categoriesPath

_, err := services.ApiGet(s.GetClient(), items, path)
_, err := api.ApiGet(s.GetClient(), items, path)

return *items, err
}
Expand All @@ -118,7 +119,7 @@ func (s *ActionTemplateService) GetByID(id string) (*ActionTemplate, error) {
return nil, err
}

resp, err := services.ApiGet(s.GetClient(), new(ActionTemplate), path)
resp, err := api.ApiGet(s.GetClient(), new(ActionTemplate), path)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -148,7 +149,7 @@ func (s *ActionTemplateService) Search(searchQuery string) ([]ActionTemplateSear
path = strings.Split(path, "?")[0]
}

_, err = services.ApiGet(s.GetClient(), &searchResults, path)
_, err = api.ApiGet(s.GetClient(), &searchResults, path)

return searchResults, err
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/artifacts/artifact_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api"
"github.com/dghubble/sling"
"github.com/google/go-querystring/query"
)
Expand Down Expand Up @@ -46,20 +47,20 @@ func (s *ArtifactService) Add(artifact *Artifact) (*Artifact, error) {
// Get returns a collection of artifacts based on the criteria defined by its
// input query parameter. If an error occurs, an empty collection is returned
// along with the associated error.
func (s *ArtifactService) Get(artifactsQuery Query) (*resources.Resources[Artifact], error) {
func (s *ArtifactService) Get(artifactsQuery Query) (*resources.Resources[*Artifact], error) {
v, _ := query.Values(artifactsQuery)
path := s.BasePath
encodedQueryString := v.Encode()
if len(encodedQueryString) > 0 {
path += "?" + encodedQueryString
}

resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[Artifact]), path)
resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*Artifact]), path)
if err != nil {
return &resources.Resources[Artifact]{}, err
return &resources.Resources[*Artifact]{}, err
}

return resp.(*resources.Resources[Artifact]), nil
return resp.(*resources.Resources[*Artifact]), nil
}

// GetAll returns all artifacts. If none can be found or an error occurs, it
Expand All @@ -85,7 +86,7 @@ func (s *ArtifactService) GetByID(id string) (*Artifact, error) {
return nil, err
}

resp, err := services.ApiGet(s.GetClient(), new(Artifact), path)
resp, err := api.ApiGet(s.GetClient(), new(Artifact), path)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/authentication/authentication_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package authentication
import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api"
"github.com/dghubble/sling"
)

Expand All @@ -27,7 +28,7 @@ func (s *AuthenticationService) Get() (*Authentication, error) {
return nil, err
}

resp, err := services.ApiGet(s.GetClient(), new(Authentication), path)
resp, err := api.ApiGet(s.GetClient(), new(Authentication), path)
if err != nil {
return nil, err
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/certificates/certificate_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api"
"github.com/dghubble/sling"
)

Expand Down Expand Up @@ -62,18 +63,18 @@ func (s *CertificateService) Archive(resource *CertificateResource) (*Certificat
// Get returns a collection of certificates based on the criteria defined by its input
// query parameter. If an error occurs, an empty collection is returned along
// with the associated error.
func (s *CertificateService) Get(certificatesQuery CertificatesQuery) (*resources.Resources[CertificateResource], error) {
func (s *CertificateService) Get(certificatesQuery CertificatesQuery) (*resources.Resources[*CertificateResource], error) {
path, err := s.GetURITemplate().Expand(certificatesQuery)
if err != nil {
return &resources.Resources[CertificateResource]{}, err
return &resources.Resources[*CertificateResource]{}, err
}

response, err := services.ApiGet(s.GetClient(), new(resources.Resources[CertificateResource]), path)
response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*CertificateResource]), path)
if err != nil {
return &resources.Resources[CertificateResource]{}, err
return &resources.Resources[*CertificateResource]{}, err
}

return response.(*resources.Resources[CertificateResource]), nil
return response.(*resources.Resources[*CertificateResource]), nil
}

// GetAll returns all certificates. If none are found or an error occurs, it
Expand All @@ -85,7 +86,7 @@ func (s *CertificateService) GetAll() ([]*CertificateResource, error) {
return items, err
}

_, err = services.ApiGet(s.GetClient(), &items, path)
_, err = api.ApiGet(s.GetClient(), &items, path)
return items, err
}

Expand All @@ -101,7 +102,7 @@ func (s *CertificateService) GetByID(id string) (*CertificateResource, error) {
return nil, err
}

resp, err := services.ApiGet(s.GetClient(), new(CertificateResource), path)
resp, err := api.ApiGet(s.GetClient(), new(CertificateResource), path)
if err != nil {
return nil, err
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/channels/channel_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api"
"github.com/dghubble/sling"
)

Expand Down Expand Up @@ -49,18 +50,18 @@ func (s *ChannelService) Add(channel *Channel) (*Channel, error) {
// Get returns a collection of channels based on the criteria defined by its
// input query parameter. If an error occurs, an empty collection is returned
// along with the associated error.
func (s *ChannelService) Get(channelsQuery Query) (*resources.Resources[Channel], error) {
func (s *ChannelService) Get(channelsQuery Query) (*resources.Resources[*Channel], error) {
path, err := s.GetURITemplate().Expand(channelsQuery)
if err != nil {
return &resources.Resources[Channel]{}, err
return &resources.Resources[*Channel]{}, err
}

response, err := services.ApiGet(s.GetClient(), new(resources.Resources[Channel]), path)
response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*Channel]), path)
if err != nil {
return &resources.Resources[Channel]{}, err
return &resources.Resources[*Channel]{}, err
}

return response.(*resources.Resources[Channel]), nil
return response.(*resources.Resources[*Channel]), nil
}

// GetAll returns all channels. If none can be found or an error occurs, it
Expand All @@ -72,7 +73,7 @@ func (s *ChannelService) GetAll() ([]*Channel, error) {
return items, err
}

_, err = services.ApiGet(s.GetClient(), &items, path)
_, err = api.ApiGet(s.GetClient(), &items, path)
return items, err
}

Expand All @@ -84,7 +85,7 @@ func (s *ChannelService) GetByID(id string) (*Channel, error) {
}

path := s.BasePath + "/" + id
resp, err := services.ApiGet(s.GetClient(), new(Channel), path)
resp, err := api.ApiGet(s.GetClient(), new(Channel), path)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 6bd1cc8

Please sign in to comment.