Skip to content

Commit

Permalink
update to match library conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbednarz committed Dec 6, 2023
1 parent a217c8d commit 31a2ce2
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 54 deletions.
20 changes: 14 additions & 6 deletions page_shield.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ import (
"github.com/goccy/go-json"
)

// PageShield represents the page shield object minus any timestamps
// PageShield represents the page shield object minus any timestamps.
type PageShield struct {
Enabled *bool `json:"enabled,omitempty"`
UseCloudflareReportingEndpoint *bool `json:"use_cloudflare_reporting_endpoint,omitempty"`
UseConnectionURLPath *bool `json:"use_connection_url_path,omitempty"`
}

// PageShieldSettings represents the page shield settings for a zone
type UpdatePageShieldSettingsParams struct {
Enabled *bool `json:"enabled,omitempty"`
UseCloudflareReportingEndpoint *bool `json:"use_cloudflare_reporting_endpoint,omitempty"`
UseConnectionURLPath *bool `json:"use_connection_url_path,omitempty"`
}

// PageShieldSettings represents the page shield settings for a zone.
type PageShieldSettings struct {
PageShield
UpdatedAt string `json:"updated_at"`
Expand All @@ -27,10 +33,12 @@ type PageShieldSettingsResponse struct {
Response
}

// GetPageShieldSettings returns the page shield settings for a zone
type GetPageShieldSettingsParams struct{}

// GetPageShieldSettings returns the page shield settings for a zone.
//
// API documentation: https://developers.cloudflare.com/api/operations/page-shield-get-page-shield-settings
func (api *API) GetPageShieldSettings(ctx context.Context, rc *ResourceContainer) (*PageShieldSettingsResponse, error) {
func (api *API) GetPageShieldSettings(ctx context.Context, rc *ResourceContainer, params GetPageShieldSettingsParams) (*PageShieldSettingsResponse, error) {
uri := fmt.Sprintf("/zones/%s/page_shield", rc.Identifier)

res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
Expand All @@ -47,10 +55,10 @@ func (api *API) GetPageShieldSettings(ctx context.Context, rc *ResourceContainer
return &psResponse, nil
}

// UpdatePageShieldSettings updates the page shield settings for a zone
// UpdatePageShieldSettings updates the page shield settings for a zone.
//
// API documentation: https://developers.cloudflare.com/api/operations/page-shield-update-page-shield-settings
func (api *API) UpdatePageShieldSettings(ctx context.Context, rc *ResourceContainer, params PageShield) (*PageShieldSettingsResponse, error) {
func (api *API) UpdatePageShieldSettings(ctx context.Context, rc *ResourceContainer, params UpdatePageShieldSettingsParams) (*PageShieldSettingsResponse, error) {
uri := fmt.Sprintf("/zones/%s/page_shield", rc.Identifier)

res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
Expand Down
10 changes: 5 additions & 5 deletions page_shield_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/goccy/go-json"
)

// ListPageShieldConnectionsParams represents parameters for a page shield connection request
// ListPageShieldConnectionsParams represents parameters for a page shield connection request.
type ListPageShieldConnectionsParams struct {
Direction string `json:"direction"`
ExcludeCdnCgi *bool `json:"exclude_cdn_cgi,omitempty"`
Expand All @@ -24,7 +24,7 @@ type ListPageShieldConnectionsParams struct {
URLs string `json:"urls"`
}

// PageShieldConnection represents a page shield connection
// PageShieldConnection represents a page shield connection.
type PageShieldConnection struct {
AddedAt string `json:"added_at"`
DomainReportedMalicious *bool `json:"domain_reported_malicious,omitempty"`
Expand All @@ -38,14 +38,14 @@ type PageShieldConnection struct {
URLContainsCdnCgiPath *bool `json:"url_contains_cdn_cgi_path,omitempty"`
}

// ListPageShieldConnectionsResponse represents the response from the list page shield connections endpoint
// ListPageShieldConnectionsResponse represents the response from the list page shield connections endpoint.
type ListPageShieldConnectionsResponse struct {
Result []PageShieldConnection `json:"result"`
Response
ResultInfo `json:"result_info"`
}

// ListPageShieldConnections lists all page shield connections for a zone
// ListPageShieldConnections lists all page shield connections for a zone.
//
// API documentation: https://developers.cloudflare.com/api/operations/page-shield-list-page-shield-connections
func (api *API) ListPageShieldConnections(ctx context.Context, rc *ResourceContainer, params ListPageShieldConnectionsParams) ([]PageShieldConnection, ResultInfo, error) {
Expand All @@ -67,7 +67,7 @@ func (api *API) ListPageShieldConnections(ctx context.Context, rc *ResourceConta
return psResponse.Result, psResponse.ResultInfo, nil
}

// GetPageShieldConnection gets a page shield connection for a zone
// GetPageShieldConnection gets a page shield connection for a zone.
//
// API documentation: https://developers.cloudflare.com/api/operations/page-shield-get-a-page-shield-connection
func (api *API) GetPageShieldConnection(ctx context.Context, rc *ResourceContainer, connectionID string) (*PageShieldConnection, error) {
Expand Down
8 changes: 4 additions & 4 deletions page_shield_connections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestListPageShieldConnections(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/zones/testzone/page_shield/connections", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/zones/"+testZoneID+"/page_shield/connections", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
response := ListPageShieldConnectionsResponse{
Expand All @@ -65,7 +65,7 @@ func TestListPageShieldConnections(t *testing.T) {
t.Fatal(err)
}
})
result, _, err := client.ListPageShieldConnections(context.Background(), &ResourceContainer{Identifier: "testzone"}, ListPageShieldConnectionsParams{})
result, _, err := client.ListPageShieldConnections(context.Background(), ZoneIdentifier(testZoneID), ListPageShieldConnectionsParams{})
assert.NoError(t, err)
assert.Equal(t, mockPageShieldConnections, result)
}
Expand All @@ -75,7 +75,7 @@ func TestGetPageShieldConnection(t *testing.T) {
defer teardown()

connectionID := "c9ef84a6bf5e47138c75d95e2f933e8f" //nolint
mux.HandleFunc(fmt.Sprintf("/zones/testzone/page_shield/connections/%s", connectionID), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(fmt.Sprintf("/zones/"+testZoneID+"/page_shield/connections/%s", connectionID), func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
response := mockPageShieldConnections[0] // Assuming it's the first mock connection
Expand All @@ -84,7 +84,7 @@ func TestGetPageShieldConnection(t *testing.T) {
t.Fatal(err)
}
})
result, err := client.GetPageShieldConnection(context.Background(), &ResourceContainer{Identifier: "testzone"}, connectionID)
result, err := client.GetPageShieldConnection(context.Background(), ZoneIdentifier(testZoneID), connectionID)
assert.NoError(t, err)
assert.Equal(t, &mockPageShieldConnections[0], result)
}
42 changes: 31 additions & 11 deletions page_shield_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/goccy/go-json"
)

// PageShieldPolicy represents a page shield policy
// PageShieldPolicy represents a page shield policy.
type PageShieldPolicy struct {
Action string `json:"action"`
Description string `json:"description"`
Expand All @@ -18,17 +18,37 @@ type PageShieldPolicy struct {
Value string `json:"value"`
}

// ListPageShieldPoliciesResponse represents the response from the list page shield policies endpoint
type CreatePageShieldPolicyParams struct {
Action string `json:"action"`
Description string `json:"description"`
Enabled *bool `json:"enabled,omitempty"`
Expression string `json:"expression"`
ID string `json:"id"`
Value string `json:"value"`
}

type UpdatePageShieldPolicyParams struct {
Action string `json:"action"`
Description string `json:"description"`
Enabled *bool `json:"enabled,omitempty"`
Expression string `json:"expression"`
ID string `json:"id"`
Value string `json:"value"`
}

// ListPageShieldPoliciesResponse represents the response from the list page shield policies endpoint.
type ListPageShieldPoliciesResponse struct {
Result []PageShieldPolicy `json:"result"`
Response
ResultInfo `json:"result_info"`
}

// ListPageShieldPolicies lists all page shield policies for a zone
type ListPageShieldPoliciesParams struct{}

// ListPageShieldPolicies lists all page shield policies for a zone.
//
// API documentation: https://developers.cloudflare.com/api/operations/page-shield-list-page-shield-policies
func (api *API) ListPageShieldPolicies(ctx context.Context, rc *ResourceContainer) ([]PageShieldPolicy, ResultInfo, error) {
func (api *API) ListPageShieldPolicies(ctx context.Context, rc *ResourceContainer, params ListPageShieldPoliciesParams) ([]PageShieldPolicy, ResultInfo, error) {
path := fmt.Sprintf("/zones/%s/page_shield/policies", rc.Identifier)

res, err := api.makeRequestContext(ctx, http.MethodGet, path, nil)
Expand All @@ -45,10 +65,10 @@ func (api *API) ListPageShieldPolicies(ctx context.Context, rc *ResourceContaine
return psResponse.Result, psResponse.ResultInfo, nil
}

// CreatePageShieldPolicy creates a page shield policy for a zone
// CreatePageShieldPolicy creates a page shield policy for a zone.
//
// API documentation: https://developers.cloudflare.com/api/operations/page-shield-create-page-shield-policy
func (api *API) CreatePageShieldPolicy(ctx context.Context, rc *ResourceContainer, params PageShieldPolicy) (*PageShieldPolicy, error) {
func (api *API) CreatePageShieldPolicy(ctx context.Context, rc *ResourceContainer, params CreatePageShieldPolicyParams) (*PageShieldPolicy, error) {
path := fmt.Sprintf("/zones/%s/page_shield/policies", rc.Identifier)

res, err := api.makeRequestContext(ctx, http.MethodPost, path, params)
Expand All @@ -65,7 +85,7 @@ func (api *API) CreatePageShieldPolicy(ctx context.Context, rc *ResourceContaine
return &psResponse, nil
}

// DeletePageShieldPolicy deletes a page shield policy for a zone
// DeletePageShieldPolicy deletes a page shield policy for a zone.
//
// API documentation: https://developers.cloudflare.com/api/operations/page-shield-delete-page-shield-policy
func (api *API) DeletePageShieldPolicy(ctx context.Context, rc *ResourceContainer, policyID string) error {
Expand All @@ -79,7 +99,7 @@ func (api *API) DeletePageShieldPolicy(ctx context.Context, rc *ResourceContaine
return nil
}

// GetPageShieldPolicy gets a page shield policy for a zone
// GetPageShieldPolicy gets a page shield policy for a zone.
//
// API documentation: https://developers.cloudflare.com/api/operations/page-shield-get-page-shield-policy
func (api *API) GetPageShieldPolicy(ctx context.Context, rc *ResourceContainer, policyID string) (*PageShieldPolicy, error) {
Expand All @@ -99,11 +119,11 @@ func (api *API) GetPageShieldPolicy(ctx context.Context, rc *ResourceContainer,
return &psResponse, nil
}

// UpdatePageShieldPolicy updates a page shield policy for a zone
// UpdatePageShieldPolicy updates a page shield policy for a zone.
//
// API documentation: https://developers.cloudflare.com/api/operations/page-shield-update-page-shield-policy
func (api *API) UpdatePageShieldPolicy(ctx context.Context, rc *ResourceContainer, policyID string, params PageShieldPolicy) (*PageShieldPolicy, error) {
path := fmt.Sprintf("/zones/%s/page_shield/policies/%s", rc.Identifier, policyID)
func (api *API) UpdatePageShieldPolicy(ctx context.Context, rc *ResourceContainer, params UpdatePageShieldPolicyParams) (*PageShieldPolicy, error) {
path := fmt.Sprintf("/zones/%s/page_shield/policies/%s", rc.Identifier, params.ID)

res, err := api.makeRequestContext(ctx, http.MethodPut, path, params)
if err != nil {
Expand Down
25 changes: 13 additions & 12 deletions page_shield_policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestListPageShieldPolicies(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/zones/testzone/page_shield/policies", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/zones/"+testZoneID+"/page_shield/policies", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
response := ListPageShieldPoliciesResponse{
Expand All @@ -52,7 +52,7 @@ func TestListPageShieldPolicies(t *testing.T) {
t.Fatal(err)
}
})
result, _, err := client.ListPageShieldPolicies(context.Background(), &ResourceContainer{Identifier: "testzone"})
result, _, err := client.ListPageShieldPolicies(context.Background(), ZoneIdentifier(testZoneID), ListPageShieldPoliciesParams{})
assert.NoError(t, err)
assert.Equal(t, mockPageShieldPolicies, result)
}
Expand All @@ -61,7 +61,7 @@ func TestCreatePageShieldPolicy(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/zones/testzone/page_shield/policies", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/zones/"+testZoneID+"/page_shield/policies", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method, "Expected method 'POST', got %s", r.Method)
w.Header().Set("content-type", "application/json")
var params PageShieldPolicy
Expand All @@ -74,14 +74,14 @@ func TestCreatePageShieldPolicy(t *testing.T) {
}
})

newPolicy := PageShieldPolicy{
newPolicy := CreatePageShieldPolicyParams{
Action: "block",
Description: "New policy",
Enabled: BoolPtr(true),
Expression: "ends_with(http.request.uri.path, \"/new\")",
Value: "script-src 'self';",
}
result, err := client.CreatePageShieldPolicy(context.Background(), &ResourceContainer{Identifier: "testzone"}, newPolicy)
result, err := client.CreatePageShieldPolicy(context.Background(), ZoneIdentifier(testZoneID), newPolicy)
assert.NoError(t, err)
assert.Equal(t, "newPolicyID", result.ID)
}
Expand All @@ -91,11 +91,11 @@ func TestDeletePageShieldPolicy(t *testing.T) {
defer teardown()

policyID := "c9ef84a6bf5e47138c75d95e2f933e8f"
mux.HandleFunc(fmt.Sprintf("/zones/testzone/page_shield/policies/%s", policyID), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(fmt.Sprintf("/zones/"+testZoneID+"/page_shield/policies/%s", policyID), func(w http.ResponseWriter, r *http.Request) {

Check failure on line 94 in page_shield_policies_test.go

View workflow job for this annotation

GitHub Actions / lint

string `/page_shield/policies/%s` has 3 occurrences, make it a constant (goconst)
assert.Equal(t, http.MethodDelete, r.Method, "Expected method 'DELETE', got %s", r.Method)
w.WriteHeader(http.StatusOK) // Assuming successful deletion returns 200 OK
})
err := client.DeletePageShieldPolicy(context.Background(), &ResourceContainer{Identifier: "testzone"}, policyID)
err := client.DeletePageShieldPolicy(context.Background(), ZoneIdentifier(testZoneID), policyID)
assert.NoError(t, err)
}

Expand All @@ -104,15 +104,15 @@ func TestGetPageShieldPolicy(t *testing.T) {
defer teardown()

policyID := "c9ef84a6bf5e47138c75d95e2f933e8f"
mux.HandleFunc(fmt.Sprintf("/zones/testzone/page_shield/policies/%s", policyID), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(fmt.Sprintf("/zones/"+testZoneID+"/page_shield/policies/%s", policyID), func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
err := json.NewEncoder(w).Encode(mockPageShieldPolicies[0]) // Assuming the first mock policy
if err != nil {
t.Fatal(err)
}
})
result, err := client.GetPageShieldPolicy(context.Background(), &ResourceContainer{Identifier: "testzone"}, policyID)
result, err := client.GetPageShieldPolicy(context.Background(), ZoneIdentifier(testZoneID), policyID)
assert.NoError(t, err)
assert.Equal(t, &mockPageShieldPolicies[0], result)
}
Expand All @@ -122,7 +122,7 @@ func TestUpdatePageShieldPolicy(t *testing.T) {
defer teardown()

policyID := "c9ef84a6bf5e47138c75d95e2f933e8f"
mux.HandleFunc(fmt.Sprintf("/zones/testzone/page_shield/policies/%s", policyID), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(fmt.Sprintf("/zones/"+testZoneID+"/page_shield/policies/%s", policyID), func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method)
w.Header().Set("content-type", "application/json")

Expand All @@ -136,14 +136,15 @@ func TestUpdatePageShieldPolicy(t *testing.T) {
}
})

updatedPolicy := PageShieldPolicy{
updatedPolicy := UpdatePageShieldPolicyParams{
ID: policyID,
Action: "block",
Description: "Updated policy",
Enabled: BoolPtr(false),
Expression: "ends_with(http.request.uri.path, \"/updated\")",
Value: "script-src 'self';",
}
result, err := client.UpdatePageShieldPolicy(context.Background(), &ResourceContainer{Identifier: "testzone"}, policyID, updatedPolicy)
result, err := client.UpdatePageShieldPolicy(context.Background(), ZoneIdentifier(testZoneID), updatedPolicy)
assert.NoError(t, err)
assert.Equal(t, policyID, result.ID)
assert.Equal(t, "Updated policy", result.Description)
Expand Down
10 changes: 5 additions & 5 deletions page_shield_scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ type PageShieldScript struct {
Hash string `json:"hash"`
Host string `json:"host"`
ID string `json:"id"`
JsIntegrityScore int `json:"js_integrity_score"`
JSIntegrityScore int `json:"js_integrity_score"`
LastSeenAt string `json:"last_seen_at"`
PageURLs []string `json:"page_urls"`
URL string `json:"url"`
URLContainsCdnCgiPath *bool `json:"url_contains_cdn_cgi_path,omitempty"`
}

// ListPageShieldScriptsParams represents a PageShield Script request parameters
// ListPageShieldScriptsParams represents a PageShield Script request parameters.
//
// API reference: https://developers.cloudflare.com/api/operations/page-shield-list-page-shield-scripts#Query-Parameters
type ListPageShieldScriptsParams struct {
Expand All @@ -44,14 +44,14 @@ type ListPageShieldScriptsParams struct {
URLs string `json:"urls"`
}

// PageShieldScriptsResponse represents the response from the PageShield Script API
// PageShieldScriptsResponse represents the response from the PageShield Script API.
type PageShieldScriptsResponse struct {
Results []PageShieldScript `json:"result"`
Response
ResultInfo `json:"result_info"`
}

// PageShieldScriptResponse represents the response from the PageShield Script API
// PageShieldScriptResponse represents the response from the PageShield Script API.
type PageShieldScriptResponse struct {
Result PageShieldScript `json:"result"`
Versions []PageShieldScriptVersion `json:"versions"`
Expand All @@ -61,7 +61,7 @@ type PageShieldScriptResponse struct {
type PageShieldScriptVersion struct {
FetchedAt string `json:"fetched_at"`
Hash string `json:"hash"`
JsIntegrityScore int `json:"js_integrity_score"`
JSIntegrityScore int `json:"js_integrity_score"`
}

// ListPageShieldScripts returns a list of PageShield Scripts.
Expand Down
Loading

0 comments on commit 31a2ce2

Please sign in to comment.