Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Speed Brain #3233

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this OK to be placed in root, not under /internal ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason why it should fall under /internal?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking at different repo.

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
}
83 changes: 83 additions & 0 deletions speed_brain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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")
editable := true
want := SpeedBrainSetting{
ID: "speed_brain",
Value: "on",
Editable: &editable,
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")
editable := true
want := SpeedBrainSetting{
ID: "speed_brain",
Value: "off",
Editable: &editable,
ModifiedOn: &modifiedOn,
}

actual, err := client.PatchSpeedBrainSettings(context.Background(), testZoneID, "off")
if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}
Loading