From 6f285355bf3c540ea736e1093bd88ceee0974485 Mon Sep 17 00:00:00 2001 From: "Taylor H. Perkins" Date: Sun, 4 Feb 2024 15:28:09 -0600 Subject: [PATCH 1/8] Add CRUD support for Images Variants --- .changelog/1494.txt | 3 + images_variants.go | 161 +++++++++++++++++ images_variants_test.go | 164 ++++++++++++++++++ .../fixtures/images_variants/single_full.json | 17 ++ .../fixtures/images_variants/single_list.json | 19 ++ 5 files changed, 364 insertions(+) create mode 100644 .changelog/1494.txt create mode 100644 images_variants.go create mode 100644 images_variants_test.go create mode 100644 testdata/fixtures/images_variants/single_full.json create mode 100644 testdata/fixtures/images_variants/single_list.json diff --git a/.changelog/1494.txt b/.changelog/1494.txt new file mode 100644 index 00000000000..ad6abdd66ae --- /dev/null +++ b/.changelog/1494.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +images_variants: Add support for Images Variants CRUD operations +``` diff --git a/images_variants.go b/images_variants.go new file mode 100644 index 00000000000..977ff650142 --- /dev/null +++ b/images_variants.go @@ -0,0 +1,161 @@ +package cloudflare + +import ( + "context" + "fmt" + "net/http" + + "github.com/goccy/go-json" +) + +type ImagesVariant struct { + ID string `json:"id,omitempty"` + NeverRequireSignedURLs bool `json:"neverRequireSignedURLs,omitempty"` + Options ImagesVariantsOptions `json:"options,omitempty"` +} + +type ImagesVariantsOptions struct { + Fit string `json:"fit,omitempty"` + Height int `json:"height,omitempty"` + Metadata string `json:"metadata,omitempty"` + Width int `json:"width,omitempty"` +} + +type ListImageVariantsParams struct{} + +type ListImagesVariantsResponse struct { + Result ListImageVariantsResult `json:"result,omitempty"` + Response +} + +type ListImageVariantsResult struct { + ImagesVariants map[string]ImagesVariant `json:"variants,omitempty"` +} + +type CreateImagesVariantParams struct { + ImagesVariant +} + +type UpdateImagesVariantParams struct { + ID string `json:"-"` + NeverRequireSignedURLs bool `json:"neverRequireSignedURLs,omitempty"` + Options ImagesVariantsOptions `json:"options,omitempty"` +} + +type ImagesVariantResult struct { + Variant ImagesVariant `json:"variant,omitempty"` +} + +type ImagesVariantResponse struct { + Result ImagesVariantResult `json:"result,omitempty"` + Response +} + +// Lists existing variants. +// +// API Reference: https://developers.cloudflare.com/api/operations/cloudflare-images-variants-list-variants +func (api *API) ListImagesVariants(ctx context.Context, rc *ResourceContainer, params ListImageVariantsParams) (map[string]ImagesVariant, error) { + if rc.Identifier == "" { + return map[string]ImagesVariant{}, ErrMissingAccountID + } + + baseURL := fmt.Sprintf("/accounts/%s/images/v1/variants", rc.Identifier) + res, err := api.makeRequestContext(ctx, http.MethodGet, baseURL, nil) + if err != nil { + return map[string]ImagesVariant{}, err + } + + var listImageVariantsResponse ListImagesVariantsResponse + err = json.Unmarshal(res, &listImageVariantsResponse) + if err != nil { + return map[string]ImagesVariant{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + + return listImageVariantsResponse.Result.ImagesVariants, nil +} + +// Fetch details for a single variant. +// +// API Reference: https://developers.cloudflare.com/api/operations/cloudflare-images-variants-variant-details +func (api *API) GetImagesVariantDetails(ctx context.Context, rc *ResourceContainer, variantID string) (ImagesVariant, error) { + if rc.Identifier == "" { + return ImagesVariant{}, ErrMissingAccountID + } + + baseURL := fmt.Sprintf("/accounts/%s/images/v1/variants/%s", rc.Identifier, variantID) + res, err := api.makeRequestContext(ctx, http.MethodGet, baseURL, nil) + if err != nil { + return ImagesVariant{}, err + } + + var imagesVariantDetailResponse ImagesVariantResponse + err = json.Unmarshal(res, &imagesVariantDetailResponse) + if err != nil { + return ImagesVariant{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + + return imagesVariantDetailResponse.Result.Variant, nil +} + +// Specify variants that allow you to resize images for different use cases. +// +// API Reference: https://developers.cloudflare.com/api/operations/cloudflare-images-variants-create-a-variant +func (api *API) CreateImagesVariant(ctx context.Context, rc *ResourceContainer, params CreateImagesVariantParams) (ImagesVariant, error) { + if rc.Identifier == "" { + return ImagesVariant{}, ErrMissingAccountID + } + + baseURL := fmt.Sprintf("/accounts/%s/images/v1/variants", rc.Identifier) + res, err := api.makeRequestContext(ctx, http.MethodPost, baseURL, params.ImagesVariant) + if err != nil { + return ImagesVariant{}, err + } + + var createImagesVariantResponse ImagesVariantResponse + err = json.Unmarshal(res, &createImagesVariantResponse) + if err != nil { + return ImagesVariant{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + + return createImagesVariantResponse.Result.Variant, nil +} + +// Deleting a variant purges the cache for all images associated with the variant. +// +// API Reference: https://developers.cloudflare.com/api/operations/cloudflare-images-variants-variant-details +func (api *API) DeleteImagesVariant(ctx context.Context, rc *ResourceContainer, variantID string) error { + if rc.Identifier == "" { + return ErrMissingAccountID + } + + baseURL := fmt.Sprintf("/accounts/%s/images/v1/variants/%s", rc.Identifier, variantID) + _, err := api.makeRequestContext(ctx, http.MethodDelete, baseURL, nil) + if err != nil { + return fmt.Errorf("%s: %w", errMakeRequestError, err) + } + + return nil +} + +// Updating a variant purges the cache for all images associated with the variant. +// +// API Reference: https://developers.cloudflare.com/api/operations/cloudflare-images-variants-variant-details +func (api *API) UpdateImagesVariant(ctx context.Context, rc *ResourceContainer, params UpdateImagesVariantParams) (ImagesVariant, error) { + if rc.Identifier == "" { + return ImagesVariant{}, ErrMissingAccountID + } + + baseURL := fmt.Sprintf("/accounts/%s/images/v1/variants/%s", rc.Identifier, params.ID) + res, err := api.makeRequestContext(ctx, http.MethodPatch, baseURL, params) + if err != nil { + return ImagesVariant{}, err + } + + var imagesVariantDetailResponse ImagesVariantResponse + err = json.Unmarshal(res, &imagesVariantDetailResponse) + if err != nil { + return ImagesVariant{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + + return imagesVariantDetailResponse.Result.Variant, nil +} diff --git a/images_variants_test.go b/images_variants_test.go new file mode 100644 index 00000000000..1a4a42a9c6f --- /dev/null +++ b/images_variants_test.go @@ -0,0 +1,164 @@ +package cloudflare + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +const ( + testImagesVariantID = "hero" +) + +func TestImageVariants_ListVariants(t *testing.T) { + setup() + defer teardown() + + handler := func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method) + w.Header().Set("Content-Type", "application/json") + fmt.Fprint(w, loadFixture("images_variants", "single_list")) + } + + mux.HandleFunc("/accounts/"+testAccountID+"/images/v1/variants", handler) + + want := map[string]ImagesVariant{ + "hero": { + ID: "hero", + NeverRequireSignedURLs: true, + Options: ImagesVariantsOptions{ + Fit: "scale-down", + Height: 768, + Width: 1366, + Metadata: "none", + }, + }, + } + + got, err := client.ListImagesVariants(context.Background(), AccountIdentifier(testAccountID), ListImageVariantsParams{}) + if assert.NoError(t, err) { + assert.Equal(t, want, got) + } +} + +func TestImageVariants_Delete(t *testing.T) { + setup() + defer teardown() + + handler := func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodDelete, r.Method, "Expected method '%s', got %s", http.MethodDelete, r.Method) + w.Header().Set("content-type", "application/json") + fmt.Fprint(w, `{ + "success": true, + "errors": [], + "messages": [], + "result": {} + }`) + } + + url := fmt.Sprintf("/accounts/%s/images/v1/variants/%s", testAccountID, testImagesVariantID) + mux.HandleFunc(url, handler) + + err := client.DeleteImagesVariant(context.Background(), AccountIdentifier(testAccountID), testImagesVariantID) + assert.NoError(t, err) +} + +func TestImagesVariants_GetDetails(t *testing.T) { + setup() + defer teardown() + + handler := func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method, "Expected method '%s', got %s", http.MethodGet, r.Method) + w.Header().Set("content-type", "application/json") + fmt.Fprint(w, loadFixture("images_variants", "single_full")) + } + + url := fmt.Sprintf("/accounts/%s/images/v1/variants/%s", testAccountID, testImagesVariantID) + mux.HandleFunc(url, handler) + + want := ImagesVariant{ + ID: "hero", + NeverRequireSignedURLs: true, + Options: ImagesVariantsOptions{ + Fit: "scale-down", + Height: 768, + Width: 1366, + Metadata: "none", + }, + } + + got, err := client.GetImagesVariantDetails(context.Background(), AccountIdentifier(testAccountID), testImagesVariantID) + if assert.NoError(t, err) { + assert.Equal(t, want, got) + } +} + +func TestImagesVariants_CreateVariant(t *testing.T) { + setup() + defer teardown() + + handler := func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method, "Expected method '%s', got %s", http.MethodPost, r.Method) + w.Header().Set("content-type", "application/json") + fmt.Fprint(w, loadFixture("images_variants", "single_full")) + } + + url := fmt.Sprintf("/accounts/%s/images/v1/variants", testAccountID) + mux.HandleFunc(url, handler) + + want := ImagesVariant{ + ID: "hero", + NeverRequireSignedURLs: true, + Options: ImagesVariantsOptions{ + Fit: "scale-down", + Height: 768, + Width: 1366, + Metadata: "none", + }, + } + + got, err := client.CreateImagesVariant(context.Background(), AccountIdentifier(testAccountID), CreateImagesVariantParams{ + ImagesVariant: want, + }) + if assert.NoError(t, err) { + assert.Equal(t, want, got) + } +} + +func TestImagesVariants_UpdateVariant(t *testing.T) { + setup() + defer teardown() + + handler := func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPatch, r.Method, "Expected method '%s', got %s", http.MethodPatch, r.Method) + w.Header().Set("content-type", "application/json") + fmt.Fprint(w, loadFixture("images_variants", "single_full")) + } + + url := fmt.Sprintf("/accounts/%s/images/v1/variants/%s", testAccountID, testImagesVariantID) + mux.HandleFunc(url, handler) + + want := ImagesVariant{ + ID: "hero", + NeverRequireSignedURLs: true, + Options: ImagesVariantsOptions{ + Fit: "scale-down", + Height: 768, + Width: 1366, + Metadata: "none", + }, + } + + got, err := client.UpdateImagesVariant(context.Background(), AccountIdentifier(testAccountID), UpdateImagesVariantParams{ + ID: want.ID, + NeverRequireSignedURLs: want.NeverRequireSignedURLs, + Options: want.Options, + }) + + if assert.NoError(t, err) { + assert.Equal(t, want, got) + } +} diff --git a/testdata/fixtures/images_variants/single_full.json b/testdata/fixtures/images_variants/single_full.json new file mode 100644 index 00000000000..f8bc039b5c8 --- /dev/null +++ b/testdata/fixtures/images_variants/single_full.json @@ -0,0 +1,17 @@ +{ + "errors": [], + "messages": [], + "result": { + "variant": { + "id": "hero", + "neverRequireSignedURLs": true, + "options": { + "fit": "scale-down", + "height": 768, + "metadata": "none", + "width": 1366 + } + } + }, + "success": true +} diff --git a/testdata/fixtures/images_variants/single_list.json b/testdata/fixtures/images_variants/single_list.json new file mode 100644 index 00000000000..1838aa6ce36 --- /dev/null +++ b/testdata/fixtures/images_variants/single_list.json @@ -0,0 +1,19 @@ +{ + "errors": [], + "messages": [], + "result": { + "variants": { + "hero": { + "id": "hero", + "neverRequireSignedURLs": true, + "options": { + "fit": "scale-down", + "height": 768, + "metadata": "none", + "width": 1366 + } + } + } + }, + "success": true +} From 3e80cf52f1d3d558e57f093887dc26d97d5fd074 Mon Sep 17 00:00:00 2001 From: "Taylor H. Perkins" Date: Sun, 4 Feb 2024 19:34:31 -0600 Subject: [PATCH 2/8] Add test for missing account. Use ListImageVariantsResult. --- images_variants.go | 10 ++++----- images_variants_test.go | 48 ++++++++++++++++++++++++++++++----------- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/images_variants.go b/images_variants.go index 977ff650142..fde0f58d80f 100644 --- a/images_variants.go +++ b/images_variants.go @@ -54,24 +54,24 @@ type ImagesVariantResponse struct { // Lists existing variants. // // API Reference: https://developers.cloudflare.com/api/operations/cloudflare-images-variants-list-variants -func (api *API) ListImagesVariants(ctx context.Context, rc *ResourceContainer, params ListImageVariantsParams) (map[string]ImagesVariant, error) { +func (api *API) ListImagesVariants(ctx context.Context, rc *ResourceContainer, params ListImageVariantsParams) (ListImageVariantsResult, error) { if rc.Identifier == "" { - return map[string]ImagesVariant{}, ErrMissingAccountID + return ListImageVariantsResult{}, ErrMissingAccountID } baseURL := fmt.Sprintf("/accounts/%s/images/v1/variants", rc.Identifier) res, err := api.makeRequestContext(ctx, http.MethodGet, baseURL, nil) if err != nil { - return map[string]ImagesVariant{}, err + return ListImageVariantsResult{}, err } var listImageVariantsResponse ListImagesVariantsResponse err = json.Unmarshal(res, &listImageVariantsResponse) if err != nil { - return map[string]ImagesVariant{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + return ListImageVariantsResult{}, fmt.Errorf("%s: %w", errUnmarshalError, err) } - return listImageVariantsResponse.Result.ImagesVariants, nil + return listImageVariantsResponse.Result, nil } // Fetch details for a single variant. diff --git a/images_variants_test.go b/images_variants_test.go index 1a4a42a9c6f..434a6275545 100644 --- a/images_variants_test.go +++ b/images_variants_test.go @@ -25,15 +25,17 @@ func TestImageVariants_ListVariants(t *testing.T) { mux.HandleFunc("/accounts/"+testAccountID+"/images/v1/variants", handler) - want := map[string]ImagesVariant{ - "hero": { - ID: "hero", - NeverRequireSignedURLs: true, - Options: ImagesVariantsOptions{ - Fit: "scale-down", - Height: 768, - Width: 1366, - Metadata: "none", + want := ListImageVariantsResult{ + ImagesVariants: map[string]ImagesVariant{ + "hero": { + ID: "hero", + NeverRequireSignedURLs: true, + Options: ImagesVariantsOptions{ + Fit: "scale-down", + Height: 768, + Width: 1366, + Metadata: "none", + }, }, }, } @@ -153,12 +155,34 @@ func TestImagesVariants_UpdateVariant(t *testing.T) { } got, err := client.UpdateImagesVariant(context.Background(), AccountIdentifier(testAccountID), UpdateImagesVariantParams{ - ID: want.ID, - NeverRequireSignedURLs: want.NeverRequireSignedURLs, - Options: want.Options, + ID: "hero", + NeverRequireSignedURLs: true, + Options: ImagesVariantsOptions{ + Fit: "scale-down", + Height: 768, + Width: 1366, + Metadata: "none", + }, }) if assert.NoError(t, err) { assert.Equal(t, want, got) } } + +func TestImageVariants_MissingAccountId(t *testing.T) { + _, err := client.ListImagesVariants(context.Background(), AccountIdentifier(""), ListImageVariantsParams{}) + assert.Equal(t, ErrMissingAccountID, err) + + _, err = client.GetImagesVariantDetails(context.Background(), AccountIdentifier(""), testImagesVariantID) + assert.Equal(t, ErrMissingAccountID, err) + + _, err = client.CreateImagesVariant(context.Background(), AccountIdentifier(""), CreateImagesVariantParams{}) + assert.Equal(t, ErrMissingAccountID, err) + + err = client.DeleteImagesVariant(context.Background(), AccountIdentifier(""), testImagesVariantID) + assert.Equal(t, ErrMissingAccountID, err) + + _, err = client.UpdateImagesVariant(context.Background(), AccountIdentifier(""), UpdateImagesVariantParams{}) + assert.Equal(t, ErrMissingAccountID, err) +} From 8c904488d19e31908f4f9c89c4181f8773578da3 Mon Sep 17 00:00:00 2001 From: "Taylor H. Perkins" Date: Sun, 4 Feb 2024 21:19:19 -0600 Subject: [PATCH 3/8] Dont omit boolean values if empty. --- images_variants.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/images_variants.go b/images_variants.go index fde0f58d80f..a0aeb7d3f2d 100644 --- a/images_variants.go +++ b/images_variants.go @@ -10,7 +10,7 @@ import ( type ImagesVariant struct { ID string `json:"id,omitempty"` - NeverRequireSignedURLs bool `json:"neverRequireSignedURLs,omitempty"` + NeverRequireSignedURLs bool `json:"neverRequireSignedURLs"` Options ImagesVariantsOptions `json:"options,omitempty"` } @@ -38,7 +38,7 @@ type CreateImagesVariantParams struct { type UpdateImagesVariantParams struct { ID string `json:"-"` - NeverRequireSignedURLs bool `json:"neverRequireSignedURLs,omitempty"` + NeverRequireSignedURLs bool `json:"neverRequireSignedURLs"` Options ImagesVariantsOptions `json:"options,omitempty"` } From b662e13f1275cb5cd245209db9affb4fe9d4322f Mon Sep 17 00:00:00 2001 From: "Taylor H. Perkins" Date: Mon, 5 Feb 2024 07:58:41 -0600 Subject: [PATCH 4/8] Revert "Dont omit boolean values if empty." This reverts commit 8c904488d19e31908f4f9c89c4181f8773578da3. --- images_variants.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/images_variants.go b/images_variants.go index a0aeb7d3f2d..fde0f58d80f 100644 --- a/images_variants.go +++ b/images_variants.go @@ -10,7 +10,7 @@ import ( type ImagesVariant struct { ID string `json:"id,omitempty"` - NeverRequireSignedURLs bool `json:"neverRequireSignedURLs"` + NeverRequireSignedURLs bool `json:"neverRequireSignedURLs,omitempty"` Options ImagesVariantsOptions `json:"options,omitempty"` } @@ -38,7 +38,7 @@ type CreateImagesVariantParams struct { type UpdateImagesVariantParams struct { ID string `json:"-"` - NeverRequireSignedURLs bool `json:"neverRequireSignedURLs"` + NeverRequireSignedURLs bool `json:"neverRequireSignedURLs,omitempty"` Options ImagesVariantsOptions `json:"options,omitempty"` } From 6d4ebc373f651aa9d2b20763fa709e14e5a2936c Mon Sep 17 00:00:00 2001 From: "Taylor H. Perkins" Date: Mon, 5 Feb 2024 07:59:31 -0600 Subject: [PATCH 5/8] Fix semgrep booleans. --- images_variants.go | 4 ++-- images_variants_test.go | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/images_variants.go b/images_variants.go index fde0f58d80f..9add3ca989b 100644 --- a/images_variants.go +++ b/images_variants.go @@ -10,7 +10,7 @@ import ( type ImagesVariant struct { ID string `json:"id,omitempty"` - NeverRequireSignedURLs bool `json:"neverRequireSignedURLs,omitempty"` + NeverRequireSignedURLs *bool `json:"neverRequireSignedURLs,omitempty"` Options ImagesVariantsOptions `json:"options,omitempty"` } @@ -38,7 +38,7 @@ type CreateImagesVariantParams struct { type UpdateImagesVariantParams struct { ID string `json:"-"` - NeverRequireSignedURLs bool `json:"neverRequireSignedURLs,omitempty"` + NeverRequireSignedURLs *bool `json:"neverRequireSignedURLs,omitempty"` Options ImagesVariantsOptions `json:"options,omitempty"` } diff --git a/images_variants_test.go b/images_variants_test.go index 434a6275545..a6970c7dc82 100644 --- a/images_variants_test.go +++ b/images_variants_test.go @@ -29,7 +29,7 @@ func TestImageVariants_ListVariants(t *testing.T) { ImagesVariants: map[string]ImagesVariant{ "hero": { ID: "hero", - NeverRequireSignedURLs: true, + NeverRequireSignedURLs: BoolPtr(true), Options: ImagesVariantsOptions{ Fit: "scale-down", Height: 768, @@ -83,7 +83,7 @@ func TestImagesVariants_GetDetails(t *testing.T) { want := ImagesVariant{ ID: "hero", - NeverRequireSignedURLs: true, + NeverRequireSignedURLs: BoolPtr(true), Options: ImagesVariantsOptions{ Fit: "scale-down", Height: 768, @@ -113,7 +113,7 @@ func TestImagesVariants_CreateVariant(t *testing.T) { want := ImagesVariant{ ID: "hero", - NeverRequireSignedURLs: true, + NeverRequireSignedURLs: BoolPtr(true), Options: ImagesVariantsOptions{ Fit: "scale-down", Height: 768, @@ -145,7 +145,7 @@ func TestImagesVariants_UpdateVariant(t *testing.T) { want := ImagesVariant{ ID: "hero", - NeverRequireSignedURLs: true, + NeverRequireSignedURLs: BoolPtr(true), Options: ImagesVariantsOptions{ Fit: "scale-down", Height: 768, @@ -156,7 +156,7 @@ func TestImagesVariants_UpdateVariant(t *testing.T) { got, err := client.UpdateImagesVariant(context.Background(), AccountIdentifier(testAccountID), UpdateImagesVariantParams{ ID: "hero", - NeverRequireSignedURLs: true, + NeverRequireSignedURLs: BoolPtr(true), Options: ImagesVariantsOptions{ Fit: "scale-down", Height: 768, From 658b8a1e0317b9cdf98ca76da67802fae2d41b5e Mon Sep 17 00:00:00 2001 From: "Taylor H. Perkins" Date: Mon, 5 Feb 2024 16:03:55 -0600 Subject: [PATCH 6/8] Change method name. Make CreateImagesVariantParams struct independent. --- images_variants.go | 8 +++++--- images_variants_test.go | 15 +++++++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/images_variants.go b/images_variants.go index 9add3ca989b..55481774784 100644 --- a/images_variants.go +++ b/images_variants.go @@ -33,7 +33,9 @@ type ListImageVariantsResult struct { } type CreateImagesVariantParams struct { - ImagesVariant + ID string `json:"id"` + NeverRequireSignedURLs *bool `json:"neverRequireSignedURLs,omitempty"` + Options ImagesVariantsOptions `json:"options"` } type UpdateImagesVariantParams struct { @@ -77,7 +79,7 @@ func (api *API) ListImagesVariants(ctx context.Context, rc *ResourceContainer, p // Fetch details for a single variant. // // API Reference: https://developers.cloudflare.com/api/operations/cloudflare-images-variants-variant-details -func (api *API) GetImagesVariantDetails(ctx context.Context, rc *ResourceContainer, variantID string) (ImagesVariant, error) { +func (api *API) GetImagesVariant(ctx context.Context, rc *ResourceContainer, variantID string) (ImagesVariant, error) { if rc.Identifier == "" { return ImagesVariant{}, ErrMissingAccountID } @@ -106,7 +108,7 @@ func (api *API) CreateImagesVariant(ctx context.Context, rc *ResourceContainer, } baseURL := fmt.Sprintf("/accounts/%s/images/v1/variants", rc.Identifier) - res, err := api.makeRequestContext(ctx, http.MethodPost, baseURL, params.ImagesVariant) + res, err := api.makeRequestContext(ctx, http.MethodPost, baseURL, params) if err != nil { return ImagesVariant{}, err } diff --git a/images_variants_test.go b/images_variants_test.go index a6970c7dc82..2735ed05934 100644 --- a/images_variants_test.go +++ b/images_variants_test.go @@ -68,7 +68,7 @@ func TestImageVariants_Delete(t *testing.T) { assert.NoError(t, err) } -func TestImagesVariants_GetDetails(t *testing.T) { +func TestImagesVariants_Get(t *testing.T) { setup() defer teardown() @@ -92,7 +92,7 @@ func TestImagesVariants_GetDetails(t *testing.T) { }, } - got, err := client.GetImagesVariantDetails(context.Background(), AccountIdentifier(testAccountID), testImagesVariantID) + got, err := client.GetImagesVariant(context.Background(), AccountIdentifier(testAccountID), testImagesVariantID) if assert.NoError(t, err) { assert.Equal(t, want, got) } @@ -123,7 +123,14 @@ func TestImagesVariants_CreateVariant(t *testing.T) { } got, err := client.CreateImagesVariant(context.Background(), AccountIdentifier(testAccountID), CreateImagesVariantParams{ - ImagesVariant: want, + ID: testImagesVariantID, + NeverRequireSignedURLs: BoolPtr(true), + Options: ImagesVariantsOptions{ + Fit: "scale-down", + Height: 768, + Width: 1366, + Metadata: "none", + }, }) if assert.NoError(t, err) { assert.Equal(t, want, got) @@ -174,7 +181,7 @@ func TestImageVariants_MissingAccountId(t *testing.T) { _, err := client.ListImagesVariants(context.Background(), AccountIdentifier(""), ListImageVariantsParams{}) assert.Equal(t, ErrMissingAccountID, err) - _, err = client.GetImagesVariantDetails(context.Background(), AccountIdentifier(""), testImagesVariantID) + _, err = client.GetImagesVariant(context.Background(), AccountIdentifier(""), testImagesVariantID) assert.Equal(t, ErrMissingAccountID, err) _, err = client.CreateImagesVariant(context.Background(), AccountIdentifier(""), CreateImagesVariantParams{}) From 606429112f35b23f4aff761740f0328715f3b972 Mon Sep 17 00:00:00 2001 From: "Taylor H. Perkins" Date: Mon, 5 Feb 2024 16:04:33 -0600 Subject: [PATCH 7/8] fixup! Change method name. Make CreateImagesVariantParams struct independent. --- images_variants.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/images_variants.go b/images_variants.go index 55481774784..2949551166e 100644 --- a/images_variants.go +++ b/images_variants.go @@ -33,9 +33,9 @@ type ListImageVariantsResult struct { } type CreateImagesVariantParams struct { - ID string `json:"id"` + ID string `json:"id,omitempty"` NeverRequireSignedURLs *bool `json:"neverRequireSignedURLs,omitempty"` - Options ImagesVariantsOptions `json:"options"` + Options ImagesVariantsOptions `json:"options,omitempty"` } type UpdateImagesVariantParams struct { From 2584e7be71d7e4ce24225a3efa6583c38f047c64 Mon Sep 17 00:00:00 2001 From: "Taylor H. Perkins" Date: Mon, 5 Feb 2024 16:06:12 -0600 Subject: [PATCH 8/8] Standardize test names. --- images_variants_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/images_variants_test.go b/images_variants_test.go index 2735ed05934..994eaad2c75 100644 --- a/images_variants_test.go +++ b/images_variants_test.go @@ -13,7 +13,7 @@ const ( testImagesVariantID = "hero" ) -func TestImageVariants_ListVariants(t *testing.T) { +func TestImageVariants_List(t *testing.T) { setup() defer teardown() @@ -98,7 +98,7 @@ func TestImagesVariants_Get(t *testing.T) { } } -func TestImagesVariants_CreateVariant(t *testing.T) { +func TestImagesVariants_Create(t *testing.T) { setup() defer teardown() @@ -137,7 +137,7 @@ func TestImagesVariants_CreateVariant(t *testing.T) { } } -func TestImagesVariants_UpdateVariant(t *testing.T) { +func TestImagesVariants_Update(t *testing.T) { setup() defer teardown()