Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
refactor(pullzone): refactor pullzon functions to utilize generic res…
Browse files Browse the repository at this point in the history
…ource functions
  • Loading branch information
jspizziri committed Jun 3, 2022
1 parent 2508992 commit 8d0d4b6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 124 deletions.
18 changes: 6 additions & 12 deletions pullzone_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,10 @@ type PullZoneAddOptions struct {
//
// Bunny.net API docs: https://docs.bunny.net/reference/pullzonepublic_add
func (s *PullZoneService) Add(ctx context.Context, opts *PullZoneAddOptions) (*PullZone, error) {
var res PullZone

req, err := s.client.newPostRequest("/pullzone", opts)
if err != nil {
return nil, err
}

if err := s.client.sendRequest(ctx, req, &res); err != nil {
return nil, err
}

return &res, nil
return resourceAdd[PullZone](
ctx,
s.client,
"/pullzone",
opts,
)
}
12 changes: 2 additions & 10 deletions pullzone_delete.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
package bunny

import (
"context"
"fmt"
)
import "context"

// Delete removes the Pull Zone with the given id.
//
// Bunny.net API docs: https://docs.bunny.net/reference/pullzonepublic_delete
func (s *PullZoneService) Delete(ctx context.Context, id int64) error {
req, err := s.client.newDeleteRequest(fmt.Sprintf("pullzone/%d", id), nil)
if err != nil {
return err
}

return s.client.sendRequest(ctx, req, nil)
return resourceDelete(ctx, s.client, "pullzone", id)
}
18 changes: 2 additions & 16 deletions pullzone_get.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package bunny

import (
"context"
"fmt"
)
import "context"

// Constants for the Type fields of a Pull Zone.
const (
Expand Down Expand Up @@ -163,16 +160,5 @@ type EdgeRule struct {
//
// Bunny.net API docs: https://docs.bunny.net/reference/pullzonepublic_index2
func (s *PullZoneService) Get(ctx context.Context, id int64) (*PullZone, error) {
var res PullZone

req, err := s.client.newGetRequest(fmt.Sprintf("pullzone/%d", id), nil)
if err != nil {
return nil, err
}

if err := s.client.sendRequest(ctx, req, &res); err != nil {
return nil, err
}

return &res, err
return resourceGet[PullZone](ctx, s.client, "pullzone", id)
}
76 changes: 6 additions & 70 deletions pullzone_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,17 @@ package bunny

import "context"

const (
// DefaultPaginationPage is the default value that is used for
// PullZonePaginationOptions.Page if it is unset.
DefaultPaginationPage = 1
// DefaultPaginationPerPage is the default value that is used for
// PullZonePaginationOptions.PerPage if it is unset.
DefaultPaginationPerPage = 1000
)
type PullZones PaginationReply[PullZone]

// PullZones represents the response of the List Pull Zone API endpoint.
//
// Bunny.net API docs: https://docs.bunny.net/reference/pullzonepublic_index
type PullZones struct {
Items []*PullZone `json:"Items,omitempty"`
PullZonePaginationReply
}

// PullZonePaginationReply represents the pagination information contained in a
// Pull Zone List API endpoint response.
//
// Bunny.net API docs: https://docs.bunny.net/reference/pullzonepublic_index
type PullZonePaginationReply struct {
CurrentPage *int32 `json:"CurrentPage"`
TotalItems *int32 `json:"TotalItems"`
HasMoreItems *bool `json:"HasMoreItems"`
}

// PullZonePaginationOptions specifies optional parameters for List APIs.
type PullZonePaginationOptions struct {
// Page the page to return
Page int32 `url:"page,omitempty"`
// PerPage how many entries to return per page
PerPage int32 `url:"per_page,omitempty"`
}

// List retrieves the Pull Zones.
// If opts is nil, DefaultPaginationPerPage and DefaultPaginationPage will be used.
// if opts.Page or or opts.PerPage is < 1, the related DefaultPagination values are used.
//
// Bunny.net API docs: https://docs.bunny.net/reference/pullzonepublic_index
func (s *PullZoneService) List(ctx context.Context, opts *PullZonePaginationOptions) (*PullZones, error) {
var res PullZones

// Ensure that opts.Page is >=1, if it isn't bunny.net will send a
// different response JSON object, that contains only a single
// PullZone, without items and paginations fields.
// Enforcing opts.page =>1 ensures that we always unmarshal into the
// same struct.
if opts == nil {
opts = &PullZonePaginationOptions{
Page: DefaultPaginationPage,
PerPage: DefaultPaginationPerPage,
}
} else {
opts.ensureConstraints()
}

req, err := s.client.newGetRequest("/pullzone", opts)
if err != nil {
return nil, err
}

if err := s.client.sendRequest(ctx, req, &res); err != nil {
return nil, err
}

return &res, nil
}

func (p *PullZonePaginationOptions) ensureConstraints() {
if p.Page < 1 {
p.Page = DefaultPaginationPage
}

if p.PerPage < 1 {
p.PerPage = DefaultPaginationPerPage
}
func (s *PullZoneService) List(
ctx context.Context,
opts *PaginationOptions,
) (*PullZones, error) {
return resourceList[PullZones](ctx, s.client, "/pullzone", opts)
}
24 changes: 8 additions & 16 deletions pullzone_update.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package bunny

import (
"context"
"fmt"
)
import "context"

// PullZoneUpdateOptions represents the request parameters for the Update Pull
// Zone API endpoint.
Expand Down Expand Up @@ -108,16 +105,11 @@ type PullZoneUpdateOptions struct {
// The updated Pull Zone is returned.
// Bunny.net API docs: https://docs.bunny.net/reference/pullzonepublic_updatepullzone
func (s *PullZoneService) Update(ctx context.Context, id int64, pullZone *PullZoneUpdateOptions) (*PullZone, error) {
var res PullZone

req, err := s.client.newPostRequest(fmt.Sprintf("pullzone/%d", id), pullZone)
if err != nil {
return nil, err
}

if err := s.client.sendRequest(ctx, req, &res); err != nil {
return nil, err
}

return &res, err
return resourceUpdate[PullZone](
ctx,
s.client,
"pullzone",
id,
pullZone,
)
}

0 comments on commit 8d0d4b6

Please sign in to comment.