-
Notifications
You must be signed in to change notification settings - Fork 568
/
api_shield.go
71 lines (59 loc) · 2.23 KB
/
api_shield.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package cloudflare
import (
"context"
"fmt"
"net/http"
"github.com/goccy/go-json"
)
// AuthIdCharacteristics a single option from
// configuration?properties=auth_id_characteristics.
type AuthIdCharacteristics struct {
Type string `json:"type"`
Name string `json:"name"`
}
// APIShield is all the available options under
// configuration?properties=auth_id_characteristics.
type APIShield struct {
AuthIdCharacteristics []AuthIdCharacteristics `json:"auth_id_characteristics"`
}
// APIShieldResponse represents the response from the api_gateway/configuration endpoint.
type APIShieldResponse struct {
Result APIShield `json:"result"`
Response
ResultInfo `json:"result_info"`
}
type UpdateAPIShieldParams struct {
AuthIdCharacteristics []AuthIdCharacteristics `json:"auth_id_characteristics"`
}
// GetAPIShieldConfiguration gets a zone API shield configuration.
//
// API documentation: https://api.cloudflare.com/#api-shield-settings-retrieve-information-about-specific-configuration-properties
func (api *API) GetAPIShieldConfiguration(ctx context.Context, rc *ResourceContainer) (APIShield, ResultInfo, error) {
uri := fmt.Sprintf("/zones/%s/api_gateway/configuration?properties=auth_id_characteristics", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return APIShield{}, ResultInfo{}, err
}
var asResponse APIShieldResponse
err = json.Unmarshal(res, &asResponse)
if err != nil {
return APIShield{}, ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return asResponse.Result, asResponse.ResultInfo, nil
}
// UpdateAPIShieldConfiguration sets a zone API shield configuration.
//
// API documentation: https://api.cloudflare.com/#api-shield-settings-set-configuration-properties
func (api *API) UpdateAPIShieldConfiguration(ctx context.Context, rc *ResourceContainer, params UpdateAPIShieldParams) (Response, error) {
uri := fmt.Sprintf("/zones/%s/api_gateway/configuration", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
if err != nil {
return Response{}, err
}
var asResponse Response
err = json.Unmarshal(res, &asResponse)
if err != nil {
return Response{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return asResponse, nil
}