-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
138 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 | ||
speed_brain: add support for speed_brain API | ||
``` |
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,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 | ||
} |
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,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) | ||
} | ||
} |