forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_custom_page.go
127 lines (106 loc) · 3.91 KB
/
access_custom_page.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package cloudflare
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/goccy/go-json"
)
var ErrMissingUID = errors.New("required UID missing")
type AccessCustomPageType string
const (
Forbidden AccessCustomPageType = "forbidden"
IdentityDenied AccessCustomPageType = "identity_denied"
)
type AccessCustomPage struct {
// The HTML content of the custom page.
CustomHTML string `json:"custom_html,omitempty"`
Name string `json:"name,omitempty"`
AppCount int `json:"app_count,omitempty"`
Type AccessCustomPageType `json:"type,omitempty"`
UID string `json:"uid,omitempty"`
}
type AccessCustomPageListResponse struct {
Response
Result []AccessCustomPage `json:"result"`
ResultInfo `json:"result_info"`
}
type AccessCustomPageResponse struct {
Response
Result AccessCustomPage `json:"result"`
}
type ListAccessCustomPagesParams struct{}
type CreateAccessCustomPageParams struct {
CustomHTML string `json:"custom_html,omitempty"`
Name string `json:"name,omitempty"`
Type AccessCustomPageType `json:"type,omitempty"`
}
type UpdateAccessCustomPageParams struct {
CustomHTML string `json:"custom_html,omitempty"`
Name string `json:"name,omitempty"`
Type AccessCustomPageType `json:"type,omitempty"`
UID string `json:"uid,omitempty"`
}
func (api *API) ListAccessCustomPages(ctx context.Context, rc *ResourceContainer, params ListAccessCustomPagesParams) ([]AccessCustomPage, error) {
uri := buildURI(fmt.Sprintf("/%s/%s/access/custom_pages", rc.Level, rc.Identifier), params)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []AccessCustomPage{}, err
}
var customPagesResponse AccessCustomPageListResponse
err = json.Unmarshal(res, &customPagesResponse)
if err != nil {
return []AccessCustomPage{}, err
}
return customPagesResponse.Result, nil
}
func (api *API) GetAccessCustomPage(ctx context.Context, rc *ResourceContainer, id string) (AccessCustomPage, error) {
uri := fmt.Sprintf("/%s/%s/access/custom_pages/%s", rc.Level, rc.Identifier, id)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return AccessCustomPage{}, err
}
var customPageResponse AccessCustomPageResponse
err = json.Unmarshal(res, &customPageResponse)
if err != nil {
return AccessCustomPage{}, err
}
return customPageResponse.Result, nil
}
func (api *API) CreateAccessCustomPage(ctx context.Context, rc *ResourceContainer, params CreateAccessCustomPageParams) (AccessCustomPage, error) {
uri := fmt.Sprintf("/%s/%s/access/custom_pages", rc.Level, rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params)
if err != nil {
return AccessCustomPage{}, err
}
var customPageResponse AccessCustomPageResponse
err = json.Unmarshal(res, &customPageResponse)
if err != nil {
return AccessCustomPage{}, err
}
return customPageResponse.Result, nil
}
func (api *API) DeleteAccessCustomPage(ctx context.Context, rc *ResourceContainer, id string) error {
uri := fmt.Sprintf("/%s/%s/access/custom_pages/%s", rc.Level, rc.Identifier, id)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return nil
}
func (api *API) UpdateAccessCustomPage(ctx context.Context, rc *ResourceContainer, params UpdateAccessCustomPageParams) (AccessCustomPage, error) {
if params.UID == "" {
return AccessCustomPage{}, ErrMissingUID
}
uri := fmt.Sprintf("/%s/%s/access/custom_pages/%s", rc.Level, rc.Identifier, params.UID)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
if err != nil {
return AccessCustomPage{}, err
}
var customPageResponse AccessCustomPageResponse
err = json.Unmarshal(res, &customPageResponse)
if err != nil {
return AccessCustomPage{}, err
}
return customPageResponse.Result, nil
}