-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CRUD support for Images Variants
- Loading branch information
Showing
3 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:enhancement | ||
Images Variants: Add support for Images Variants CRUD operations | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
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 ImagesVariant{}, 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, variantId string, 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
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.MethodGet, r.Method, "Expected method '%s', got %s", http.MethodGet, r.Method) | ||
|
||
w.Header().Set("content-type", "application/json") | ||
fmt.Fprint(w, loadFixture("tunnel", "configuration")) | ||
} | ||
|
||
url := fmt.Sprintf("/account/%s/images/v1/variants/%s", testAccountID, "hero") | ||
mux.HandleFunc(url, handler) | ||
|
||
err := client.DeleteImagesVariant(context.Background(), AccountIdentifier(testAccountID), "hero") | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestImagesVariants_GetDetails(t *testing.T) { | ||
|
||
} | ||
|
||
func TestImagesVariants_CreateVariant(t *testing.T) { | ||
} | ||
|
||
func TestImagesVariants_UpdateVariant(t *testing.T) { | ||
} |