diff --git a/.changelog/3233.txt b/.changelog/3233.txt new file mode 100644 index 00000000000..4de1e060363 --- /dev/null +++ b/.changelog/3233.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +speed_brain: add support for speed_brain API +``` \ No newline at end of file diff --git a/speed_brain.go b/speed_brain.go new file mode 100644 index 00000000000..b42b287006a --- /dev/null +++ b/speed_brain.go @@ -0,0 +1,54 @@ +package cloudflare + +import ( + "context" + "fmt" + "net/http" + "time" + + "github.com/goccy/go-json" +) + +// SpeedBrainSetting defines the structure for speed brain settings. +type SpeedBrainSetting struct { + ID string `json:"id"` + Editable bool `json:"editable,omitempty"` + Value string `json:"value"` + ModifiedOn *time.Time `json:"modified_on"` +} + +// SpeedBrainSettingResponse wraps the response from the API containing the result. +type SpeedBrainSettingResponse struct { + Response + Result SpeedBrainSetting `json:"result"` +} + +// GetSpeedBrainSettings retrieves the speed brain settings for a specific zone. +func (api *API) GetSpeedBrainSettings(ctx context.Context, zoneID string) (SpeedBrainSetting, error) { + uri := fmt.Sprintf("/zones/%s/settings/speed_brain", zoneID) + res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + if err != nil { + return SpeedBrainSetting{}, err + } + var r SpeedBrainSettingResponse + err = json.Unmarshal(res, &r) + if err != nil { + return SpeedBrainSetting{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + return r.Result, nil +} + +// PatchSpeedBrainSettings updates the speed brain settings for a specific zone. +func (api *API) PatchSpeedBrainSettings(ctx context.Context, zoneID string, value string) (SpeedBrainSetting, error) { + uri := fmt.Sprintf("/zones/%s/settings/speed_brain", zoneID) + res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, SpeedBrainSetting{Value: value}) + if err != nil { + return SpeedBrainSetting{}, err + } + var r SpeedBrainSettingResponse + err = json.Unmarshal(res, &r) + if err != nil { + return SpeedBrainSetting{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + return r.Result, nil +} diff --git a/speed_brain_test.go b/speed_brain_test.go new file mode 100644 index 00000000000..629cabfd0fb --- /dev/null +++ b/speed_brain_test.go @@ -0,0 +1,81 @@ +package cloudflare + +import ( + "context" + "fmt" + "net/http" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestGetSpeedBrainSettings(t *testing.T) { + setup() + defer teardown() + + handler := func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("content-type", "application/json") + fmt.Fprint(w, `{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "speed_brain", + "value": "on", + "editable": true, + "modified_on": "2014-01-01T05:20:00.12345Z" + } + }`) + } + mux.HandleFunc("/zones/"+testZoneID+"/settings/speed_brain", handler) + + modifiedOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00.12345Z") + want := SpeedBrainSetting{ + ID: "speed_brain", + Value: "on", + Editable: true, + ModifiedOn: &modifiedOn, + } + + actual, err := client.GetSpeedBrainSettings(context.Background(), testZoneID) + if assert.NoError(t, err) { + assert.Equal(t, want, actual) + } +} + +func TestPatchSpeedBrainSettings(t *testing.T) { + setup() + defer teardown() + + handler := func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPatch, r.Method, "Expected method 'PATCH', got %s", r.Method) + + w.Header().Set("content-type", "application/json") + fmt.Fprint(w, `{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "speed_brain", + "value": "off", + "editable": true, + "modified_on": "2014-01-01T05:20:00.12345Z" + } + }`) + } + mux.HandleFunc("/zones/"+testZoneID+"/settings/speed_brain", handler) + + modifiedOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00.12345Z") + want := SpeedBrainSetting{ + ID: "speed_brain", + Value: "off", + Editable: true, + ModifiedOn: &modifiedOn, + } + + actual, err := client.PatchSpeedBrainSettings(context.Background(), testZoneID, "off") + if assert.NoError(t, err) { + assert.Equal(t, want, actual) + } +}