From bb4993b87d3095633260a8b5d38d2045bb252839 Mon Sep 17 00:00:00 2001 From: Alexandr Gorshunov Date: Fri, 1 Nov 2024 08:43:16 +0100 Subject: [PATCH 1/2] GCLOUD2-16462 resource list method --- resources/resources.go | 1 + resources/service.go | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/resources/resources.go b/resources/resources.go index 98f2d0c..97af68d 100644 --- a/resources/resources.go +++ b/resources/resources.go @@ -9,6 +9,7 @@ import ( type ResourceService interface { Create(ctx context.Context, req *CreateRequest) (*Resource, error) + List(ctx context.Context) ([]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 diff --git a/resources/service.go b/resources/service.go index 4896af9..86b88ef 100644 --- a/resources/service.go +++ b/resources/service.go @@ -27,6 +27,15 @@ func (s *Service) Create(ctx context.Context, req *CreateRequest) (*Resource, er return &resource, nil } +func (s *Service) List(ctx context.Context) ([]Resource, error) { + var resources []Resource + if err := s.r.Request(ctx, http.MethodGet, "/cdn/resources", 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 { From 15f7cd44746aceef136cde8ee45a2708d6d37585 Mon Sep 17 00:00:00 2001 From: Alexandr Gorshunov Date: Mon, 4 Nov 2024 16:32:23 +0100 Subject: [PATCH 2/2] GCLOUD2-16462 pagination added --- resources/resources.go | 2 +- resources/service.go | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/resources/resources.go b/resources/resources.go index 97af68d..7761794 100644 --- a/resources/resources.go +++ b/resources/resources.go @@ -9,7 +9,7 @@ import ( type ResourceService interface { Create(ctx context.Context, req *CreateRequest) (*Resource, error) - List(ctx context.Context) ([]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 diff --git a/resources/service.go b/resources/service.go index 86b88ef..cab8ac0 100644 --- a/resources/service.go +++ b/resources/service.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "net/url" "github.com/G-Core/gcorelabscdn-go/gcore" ) @@ -27,9 +28,22 @@ func (s *Service) Create(ctx context.Context, req *CreateRequest) (*Resource, er return &resource, nil } -func (s *Service) List(ctx context.Context) ([]Resource, error) { +func (s *Service) List(ctx context.Context, limit, offset int) ([]Resource, error) { var resources []Resource - if err := s.r.Request(ctx, http.MethodGet, "/cdn/resources", nil, &resources); err != nil { + 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) }