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 a2d8753 commit 12d0ead
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 161 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 resourcePost[PullZone](
ctx,
s.client,
"/pullzone",
opts,
)
}
10 changes: 3 additions & 7 deletions pullzone_add_custom_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ type PullZoneAddCustomCertificateOptions struct {
// AddCustomCertificate represents the Add Custom Certificate API Endpoint.
//
// Bunny.net API docs: https://docs.bunny.net/reference/pullzonepublic_addcertificate
func (s *PullZoneService) AddCustomCertificate(ctx context.Context, pullZoneID int64, options *PullZoneAddCustomCertificateOptions) error {
req, err := s.client.newPostRequest(fmt.Sprintf("/pullzone/%d/addCertificate", pullZoneID), options)
if err != nil {
return err
}

return s.client.sendRequest(ctx, req, nil)
func (s *PullZoneService) AddCustomCertificate(ctx context.Context, pullZoneID int64, opts *PullZoneAddCustomCertificateOptions) error {
path := fmt.Sprintf("/pullzone/%d/addCertificate", pullZoneID)
return resourcePostWith204Response(ctx, s.client, path, opts)
}
8 changes: 2 additions & 6 deletions pullzone_add_custom_hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ type AddCustomHostnameOptions struct {
//
// Bunny.net API docs: https://docs.bunny.net/reference/pullzonepublic_addhostname
func (s *PullZoneService) AddCustomHostname(ctx context.Context, pullZoneID int64, opts *AddCustomHostnameOptions) error {
req, err := s.client.newPostRequest(fmt.Sprintf("pullzone/%d/addHostname", pullZoneID), opts)
if err != nil {
return err
}

return s.client.sendRequest(ctx, req, nil)
path := fmt.Sprintf("pullzone/%d/addHostname", pullZoneID)
return resourcePostWith204Response(ctx, s.client, path, opts)
}
8 changes: 2 additions & 6 deletions pullzone_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
//
// 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)
path := fmt.Sprintf("pullzone/%d", id)
return resourceDelete(ctx, s.client, path, nil)
}
8 changes: 2 additions & 6 deletions pullzone_edgerule_add_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ type AddOrUpdateEdgeRuleOptions struct {
//
// Bunny.net API docs: https://docs.bunny.net/reference/pullzonepublic_addedgerule
func (s *PullZoneService) AddOrUpdateEdgeRule(ctx context.Context, pullZoneID int64, opts *AddOrUpdateEdgeRuleOptions) error {
req, err := s.client.newPostRequest(fmt.Sprintf("pullzone/%d/edgerules/addOrUpdate", pullZoneID), opts)
if err != nil {
return err
}

return s.client.sendRequest(ctx, req, nil)
path := fmt.Sprintf("pullzone/%d/edgerules/addOrUpdate", pullZoneID)
return resourcePostWith204Response(ctx, s.client, path, opts)
}
8 changes: 2 additions & 6 deletions pullzone_edgerule_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (
//
// Bunny.net API docs: https://docs.bunny.net/reference/pullzonepublic_deleteedgerule
func (s *PullZoneService) DeleteEdgeRule(ctx context.Context, pullZoneID int64, edgeRuleGUID string) error {
req, err := s.client.newDeleteRequest(fmt.Sprintf("pullzone/%d/edgerules/%s", pullZoneID, edgeRuleGUID), nil)
if err != nil {
return err
}

return s.client.sendRequest(ctx, req, nil)
path := fmt.Sprintf("pullzone/%d/edgerules/%s", pullZoneID, edgeRuleGUID)
return resourceDelete(ctx, s.client, path, nil)
}
8 changes: 2 additions & 6 deletions pullzone_edgerule_set_enabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ func (s *PullZoneService) SetEdgeRuleEnabled(ctx context.Context, pullZoneID int
}
}

req, err := s.client.newPostRequest(fmt.Sprintf("pullzone/%d/edgerules/%s/setEdgeRuleEnabled", pullZoneID, edgeRuleGUID), opts)
if err != nil {
return err
}

return s.client.sendRequest(ctx, req, nil)
path := fmt.Sprintf("pullzone/%d/edgerules/%s/setEdgeRuleEnabled", pullZoneID, edgeRuleGUID)
return resourcePostWith204Response(ctx, s.client, path, opts)
}
14 changes: 2 additions & 12 deletions pullzone_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,6 @@ 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
path := fmt.Sprintf("pullzone/%d", id)
return resourceGet[PullZone](ctx, s.client, path)
}
74 changes: 6 additions & 68 deletions pullzone_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,19 @@ 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
)

// 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"`
}
type PullZones PaginationReply[PullZone]

// 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)
}
10 changes: 3 additions & 7 deletions pullzone_remove_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ type RemoveCertificateOptions struct {
// RemoveCertificate represents the Remove Certificate API Endpoint.
//
// Bunny.net API docs: https://docs.bunny.net/reference/pullzonepublic_removecertificate
func (s *PullZoneService) RemoveCertificate(ctx context.Context, pullZoneID int64, options *RemoveCertificateOptions) error {
req, err := s.client.newDeleteRequest(fmt.Sprintf("/pullzone/%d/removeCertificate", pullZoneID), options)
if err != nil {
return err
}

return s.client.sendRequest(ctx, req, nil)
func (s *PullZoneService) RemoveCertificate(ctx context.Context, pullZoneID int64, opts *RemoveCertificateOptions) error {
path := fmt.Sprintf("/pullzone/%d/removeCertificate", pullZoneID)
return resourceDelete(ctx, s.client, path, opts)
}
8 changes: 2 additions & 6 deletions pullzone_remove_custom_hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ type RemoveCustomHostnameOptions struct {
//
// Bunny.net API docs: https://docs.bunny.net/reference/pullzonepublic_removehostname
func (s *PullZoneService) RemoveCustomHostname(ctx context.Context, pullZoneID int64, opts *RemoveCustomHostnameOptions) error {
req, err := s.client.newDeleteRequest(fmt.Sprintf("pullzone/%d/removeHostname", pullZoneID), opts)
if err != nil {
return err
}

return s.client.sendRequest(ctx, req, nil)
path := fmt.Sprintf("pullzone/%d/removeHostname", pullZoneID)
return resourceDelete(ctx, s.client, path, opts)
}
8 changes: 2 additions & 6 deletions pullzone_set_force_ssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ type SetForceSSLOptions struct {
//
// Bunny.net API docs: https://docs.bunny.net/reference/pullzonepublic_setforcessl
func (s *PullZoneService) SetForceSSL(ctx context.Context, pullzoneID int64, opts *SetForceSSLOptions) error {
req, err := s.client.newPostRequest(fmt.Sprintf("pullzone/%d/setForceSSL", pullzoneID), opts)
if err != nil {
return err
}

return s.client.sendRequest(ctx, req, nil)
path := fmt.Sprintf("pullzone/%d/setForceSSL", pullzoneID)
return resourcePostWith204Response(ctx, s.client, path, opts)
}
21 changes: 8 additions & 13 deletions pullzone_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,12 @@ type PullZoneUpdateOptions struct {
// Update changes the configuration the Pull-Zone with the given ID.
// 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
func (s *PullZoneService) Update(ctx context.Context, id int64, opts *PullZoneUpdateOptions) (*PullZone, error) {
path := fmt.Sprintf("pullzone/%d", id)
return resourcePost[PullZone](
ctx,
s.client,
path,
opts,
)
}

0 comments on commit 12d0ead

Please sign in to comment.