Skip to content

Commit

Permalink
Add support for Speed Brain
Browse files Browse the repository at this point in the history
  • Loading branch information
cf-ypark committed Sep 23, 2024
1 parent 654a92f commit 33cbbda
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/3233.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
speed_brain: add support for speed_brain API
```
54 changes: 54 additions & 0 deletions speed_brain.go
Original file line number Diff line number Diff line change
@@ -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
}
81 changes: 81 additions & 0 deletions speed_brain_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 33cbbda

Please sign in to comment.