-
Notifications
You must be signed in to change notification settings - Fork 593
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.