Skip to content

Commit

Permalink
Merge pull request #67 from alexk53/GCLOUD2-16462-resource-list-method
Browse files Browse the repository at this point in the history
GCLOUD2-16462 resource list method
  • Loading branch information
andrei-lukyanchyk authored Nov 5, 2024
2 parents 34b8e6d + 15f7cd4 commit 28abd49
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type ResourceService interface {
Create(ctx context.Context, req *CreateRequest) (*Resource, error)
List(ctx context.Context, limit, offset int) ([]Resource, error)
Get(ctx context.Context, id int64) (*Resource, error)
Update(ctx context.Context, id int64, req *UpdateRequest) (*Resource, error)
Delete(ctx context.Context, resourceID int64) error
Expand Down
23 changes: 23 additions & 0 deletions resources/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"net/url"

"github.com/G-Core/gcorelabscdn-go/gcore"
)
Expand All @@ -27,6 +28,28 @@ func (s *Service) Create(ctx context.Context, req *CreateRequest) (*Resource, er
return &resource, nil
}

func (s *Service) List(ctx context.Context, limit, offset int) ([]Resource, error) {
var resources []Resource
params := url.Values{}
if limit > 0 {
params.Add("limit", fmt.Sprintf("%d", limit))
}
if offset > 0 {
params.Add("offset", fmt.Sprintf("%d", offset))
}

path := "/cdn/resources"
if len(params) > 0 {
path = fmt.Sprintf("%s?%s", path, params.Encode())
}

if err := s.r.Request(ctx, http.MethodGet, path, nil, &resources); err != nil {
return nil, fmt.Errorf("request: %w", err)
}

return resources, nil
}

func (s *Service) Get(ctx context.Context, id int64) (*Resource, error) {
var resource Resource
if err := s.r.Request(ctx, http.MethodGet, fmt.Sprintf("/cdn/resources/%d", id), nil, &resource); err != nil {
Expand Down

0 comments on commit 28abd49

Please sign in to comment.