Skip to content

Commit

Permalink
fix(products): Improve API usability. (#572)
Browse files Browse the repository at this point in the history
* Add 'EnableOutput' type aliases in each product, so that users of
  the API don't need to know whether that product has its own
  EnableOutput or not. Add 'NewEnableOutput' function so that users of
  the API can easily construct mock output instances.

* Add 'ProductName' constant in each product, so that users of the API
  will have access to the canonical name of the product along with its
  product_id.

* Change package names to conform to Go guidelines (remove underscores).

* Change APIs to accept and return by-value instead of by-pointer, as
  using by-pointer structures can be difficult in generic parameter
  contexts and the structures involved are small (so there is no real
  benefit to avoiding by-value passing).
  • Loading branch information
kpfleming authored Jan 24, 2025
1 parent a99e42e commit b657a3f
Show file tree
Hide file tree
Showing 118 changed files with 733 additions and 570 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package image_optimizer_default_settings_test
package imageoptimizerdefaultsettings_test

import (
"strings"
"testing"

"github.com/fastly/go-fastly/v9/fastly"
"github.com/fastly/go-fastly/v9/fastly/products/image_optimizer"
"github.com/fastly/go-fastly/v9/fastly/products/imageoptimizer"
)

// TestClient_ImageOptimizerDefaultSettings tests the Image Optimizer Default Settings API
Expand All @@ -23,7 +23,7 @@ func TestClient_ImageOptimizerDefaultSettings(t *testing.T) {

// Enable IO
fastly.Record(t, fixtureBase+"enable_product", func(c *fastly.Client) {
_, err = image_optimizer.Enable(c, fastly.TestDeliveryServiceID)
_, err = imageoptimizer.Enable(c, fastly.TestDeliveryServiceID)
})
if err != nil {
t.Fatal(err)
Expand All @@ -32,7 +32,7 @@ func TestClient_ImageOptimizerDefaultSettings(t *testing.T) {
// Ensure we disable IO on the service after the test
defer func() {
fastly.Record(t, fixtureBase+"disable_product", func(c *fastly.Client) {
_, err = image_optimizer.Enable(c, fastly.TestDeliveryServiceID)
_, err = imageoptimizer.Enable(c, fastly.TestDeliveryServiceID)

if err != nil {
t.Fatal(err)
Expand Down
38 changes: 0 additions & 38 deletions fastly/products/api_output.go

This file was deleted.

36 changes: 0 additions & 36 deletions fastly/products/bot_management/api.go

This file was deleted.

3 changes: 0 additions & 3 deletions fastly/products/bot_management/doc.go

This file was deleted.

50 changes: 50 additions & 0 deletions fastly/products/botmanagement/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package botmanagement

import (
"github.com/fastly/go-fastly/v9/fastly"
"github.com/fastly/go-fastly/v9/fastly/products"
"github.com/fastly/go-fastly/v9/internal/productcore"
)

const (
ProductID = "bot_management"
ProductName = "Bot Management"
)

// EnableOutput holds the details returned by the API from 'Get' and
// 'Enable' operations; this alias exists to ensure that users of this
// package will have a stable name to reference.
type EnableOutput = products.EnableOutput

// Get gets the status of the Bot Management product on the service.
func Get(c *fastly.Client, serviceID string) (EnableOutput, error) {
return productcore.Get[EnableOutput](&productcore.GetInput{
Client: c,
ProductID: ProductID,
ServiceID: serviceID,
})
}

// Enable enables the Bot Management product on the service.
func Enable(c *fastly.Client, serviceID string) (EnableOutput, error) {
return productcore.Put[EnableOutput](&productcore.PutInput[products.NullInput]{
Client: c,
ProductID: ProductID,
ServiceID: serviceID,
})
}

// Disable disables the Bot Management product on the service.
func Disable(c *fastly.Client, serviceID string) error {
return productcore.Delete(&productcore.DeleteInput{
Client: c,
ProductID: ProductID,
ServiceID: serviceID,
})
}

// NewEnableOutput is used to construct mock API output structures for
// use in tests.
func NewEnableOutput(serviceID string) EnableOutput {
return products.NewEnableOutput(ProductID, serviceID)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package bot_management_test
package botmanagement_test

import (
"testing"

"github.com/fastly/go-fastly/v9/fastly"
"github.com/fastly/go-fastly/v9/fastly/products"
"github.com/fastly/go-fastly/v9/fastly/products/bot_management"
"github.com/fastly/go-fastly/v9/fastly/products/botmanagement"
"github.com/fastly/go-fastly/v9/internal/productcore"
"github.com/fastly/go-fastly/v9/internal/test_utils"
)
Expand All @@ -15,36 +15,36 @@ var serviceID = fastly.TestDeliveryServiceID
var functionalTests = []*test_utils.FunctionalTest{
productcore.NewDisableTest(&productcore.DisableTestInput{
Phase: "ensure disabled before testing",
OpFn: bot_management.Disable,
OpFn: botmanagement.Disable,
ServiceID: serviceID,
IgnoreFailure: true,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[botmanagement.EnableOutput]{
Phase: "before enablement",
OpFn: bot_management.Get,
ProductID: bot_management.ProductID,
OpFn: botmanagement.Get,
ProductID: botmanagement.ProductID,
ServiceID: serviceID,
ExpectFailure: true,
}),
productcore.NewEnableTest(&productcore.EnableTestInput[*products.EnableOutput, *productcore.NullInput]{
OpNoInputFn: bot_management.Enable,
ProductID: bot_management.ProductID,
productcore.NewEnableTest(&productcore.EnableTestInput[botmanagement.EnableOutput, products.NullInput]{
OpNoInputFn: botmanagement.Enable,
ProductID: botmanagement.ProductID,
ServiceID: serviceID,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[botmanagement.EnableOutput]{
Phase: "after enablement",
OpFn: bot_management.Get,
ProductID: bot_management.ProductID,
OpFn: botmanagement.Get,
ProductID: botmanagement.ProductID,
ServiceID: serviceID,
}),
productcore.NewDisableTest(&productcore.DisableTestInput{
OpFn: bot_management.Disable,
OpFn: botmanagement.Disable,
ServiceID: serviceID,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[botmanagement.EnableOutput]{
Phase: "after disablement",
OpFn: bot_management.Get,
ProductID: bot_management.ProductID,
OpFn: botmanagement.Get,
ProductID: botmanagement.ProductID,
ServiceID: serviceID,
ExpectFailure: true,
}),
Expand Down
3 changes: 3 additions & 0 deletions fastly/products/botmanagement/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package botmanagement contains API operations to enable and
// disable the Bot Management product on a service
package botmanagement
36 changes: 0 additions & 36 deletions fastly/products/brotli_compression/api.go

This file was deleted.

3 changes: 0 additions & 3 deletions fastly/products/brotli_compression/doc.go

This file was deleted.

50 changes: 50 additions & 0 deletions fastly/products/brotlicompression/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package brotlicompression

import (
"github.com/fastly/go-fastly/v9/fastly"
"github.com/fastly/go-fastly/v9/fastly/products"
"github.com/fastly/go-fastly/v9/internal/productcore"
)

const (
ProductID = "brotli_compression"
ProductName = "Brotli Compression"
)

// EnableOutput holds the details returned by the API from 'Get' and
// 'Enable' operations; this alias exists to ensure that users of this
// package will have a stable name to reference.
type EnableOutput = products.EnableOutput

// Get gets the status of the Brotli Compression product on the service.
func Get(c *fastly.Client, serviceID string) (EnableOutput, error) {
return productcore.Get[EnableOutput](&productcore.GetInput{
Client: c,
ProductID: ProductID,
ServiceID: serviceID,
})
}

// Enable enables the Brotli Compression product on the service.
func Enable(c *fastly.Client, serviceID string) (EnableOutput, error) {
return productcore.Put[EnableOutput](&productcore.PutInput[products.NullInput]{
Client: c,
ProductID: ProductID,
ServiceID: serviceID,
})
}

// Disable disables the Brotli Compression product on the service.
func Disable(c *fastly.Client, serviceID string) error {
return productcore.Delete(&productcore.DeleteInput{
Client: c,
ProductID: ProductID,
ServiceID: serviceID,
})
}

// NewEnableOutput is used to construct mock API output structures for
// use in tests.
func NewEnableOutput(serviceID string) EnableOutput {
return products.NewEnableOutput(ProductID, serviceID)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package brotli_compression_test
package brotlicompression_test

import (
"testing"

"github.com/fastly/go-fastly/v9/fastly"
"github.com/fastly/go-fastly/v9/fastly/products"
"github.com/fastly/go-fastly/v9/fastly/products/brotli_compression"
"github.com/fastly/go-fastly/v9/fastly/products/brotlicompression"
"github.com/fastly/go-fastly/v9/internal/productcore"
"github.com/fastly/go-fastly/v9/internal/test_utils"
)
Expand All @@ -15,36 +15,36 @@ var serviceID = fastly.TestDeliveryServiceID
var functionalTests = []*test_utils.FunctionalTest{
productcore.NewDisableTest(&productcore.DisableTestInput{
Phase: "ensure disabled before testing",
OpFn: brotli_compression.Disable,
OpFn: brotlicompression.Disable,
ServiceID: serviceID,
IgnoreFailure: true,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[brotlicompression.EnableOutput]{
Phase: "before enablement",
OpFn: brotli_compression.Get,
ProductID: brotli_compression.ProductID,
OpFn: brotlicompression.Get,
ProductID: brotlicompression.ProductID,
ServiceID: serviceID,
ExpectFailure: true,
}),
productcore.NewEnableTest(&productcore.EnableTestInput[*products.EnableOutput, *productcore.NullInput]{
OpNoInputFn: brotli_compression.Enable,
ProductID: brotli_compression.ProductID,
productcore.NewEnableTest(&productcore.EnableTestInput[brotlicompression.EnableOutput, products.NullInput]{
OpNoInputFn: brotlicompression.Enable,
ProductID: brotlicompression.ProductID,
ServiceID: serviceID,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[brotlicompression.EnableOutput]{
Phase: "after enablement",
OpFn: brotli_compression.Get,
ProductID: brotli_compression.ProductID,
OpFn: brotlicompression.Get,
ProductID: brotlicompression.ProductID,
ServiceID: serviceID,
}),
productcore.NewDisableTest(&productcore.DisableTestInput{
OpFn: brotli_compression.Disable,
OpFn: brotlicompression.Disable,
ServiceID: serviceID,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[brotlicompression.EnableOutput]{
Phase: "after disablement",
OpFn: brotli_compression.Get,
ProductID: brotli_compression.ProductID,
OpFn: brotlicompression.Get,
ProductID: brotlicompression.ProductID,
ServiceID: serviceID,
ExpectFailure: true,
}),
Expand Down
3 changes: 3 additions & 0 deletions fastly/products/brotlicompression/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package brotlicompression contains API operations to enable and
// disable the Brotli Compression product on a service
package brotlicompression
3 changes: 0 additions & 3 deletions fastly/products/ddos_protection/doc.go

This file was deleted.

Loading

0 comments on commit b657a3f

Please sign in to comment.