forked from fastly/go-fastly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.go
285 lines (235 loc) · 8 KB
/
header.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package fastly
import (
"fmt"
"sort"
)
const (
// HeaderActionSet is a header action that sets or resets a header.
HeaderActionSet HeaderAction = "set"
// HeaderActionAppend is a header action that appends to an existing header.
HeaderActionAppend HeaderAction = "append"
// HeaderActionDelete is a header action that deletes a header.
HeaderActionDelete HeaderAction = "delete"
// HeaderActionRegex is a header action that performs a single regex
// replacement on a header.
HeaderActionRegex HeaderAction = "regex"
// HeaderActionRegexRepeat is a header action that performs a global regex
// replacement on a header.
HeaderActionRegexRepeat HeaderAction = "regex_repeat"
)
// HeaderAction is a type of header action.
type HeaderAction string
const (
// HeaderTypeRequest is a header type that performs on the request before
// lookups.
HeaderTypeRequest HeaderType = "request"
// HeaderTypeFetch is a header type that performs on the request to the origin
// server.
HeaderTypeFetch HeaderType = "fetch"
// HeaderTypeCache is a header type that performs on the response before it's
// store in the cache.
HeaderTypeCache HeaderType = "cache"
// HeaderTypeResponse is a header type that performs on the response before
// delivering to the client.
HeaderTypeResponse HeaderType = "response"
)
// HeaderType is a type of header.
type HeaderType string
// Header represents a header response from the Fastly API.
type Header struct {
ServiceID string `mapstructure:"service_id"`
Version int `mapstructure:"version"`
Name string `mapstructure:"name"`
Action HeaderAction `mapstructure:"action"`
IgnoreIfSet bool `mapstructure:"ignore_if_set"`
Type HeaderType `mapstructure:"type"`
Destination string `mapstructure:"dst"`
Source string `mapstructure:"src"`
Regex string `mapstructure:"regex"`
Substitution string `mapstructure:"substitution"`
Priority uint `mapstructure:"priority"`
RequestCondition string `mapstructure:"request_condition"`
CacheCondition string `mapstructure:"cache_condition"`
ResponseCondition string `mapstructure:"response_condition"`
}
// headersByName is a sortable list of headers.
type headersByName []*Header
// Len, Swap, and Less implement the sortable interface.
func (s headersByName) Len() int { return len(s) }
func (s headersByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s headersByName) Less(i, j int) bool {
return s[i].Name < s[j].Name
}
// ListHeadersInput is used as input to the ListHeaders function.
type ListHeadersInput struct {
// Service is the ID of the service (required).
Service string
// Version is the specific configuration version (required).
Version int
}
// ListHeaders returns the list of headers for the configuration version.
func (c *Client) ListHeaders(i *ListHeadersInput) ([]*Header, error) {
if i.Service == "" {
return nil, ErrMissingService
}
if i.Version == 0 {
return nil, ErrMissingVersion
}
path := fmt.Sprintf("/service/%s/version/%d/header", i.Service, i.Version)
resp, err := c.Get(path, nil)
if err != nil {
return nil, err
}
var bs []*Header
if err := decodeJSON(&bs, resp.Body); err != nil {
return nil, err
}
sort.Stable(headersByName(bs))
return bs, nil
}
// CreateHeaderInput is used as input to the CreateHeader function.
type CreateHeaderInput struct {
// Service is the ID of the service. Version is the specific configuration
// version. Both fields are required.
Service string
Version int
Name string `form:"name,omitempty"`
Action HeaderAction `form:"action,omitempty"`
IgnoreIfSet *Compatibool `form:"ignore_if_set,omitempty"`
Type HeaderType `form:"type,omitempty"`
Destination string `form:"dst,omitempty"`
Source string `form:"src,omitempty"`
Regex string `form:"regex,omitempty"`
Substitution string `form:"substitution,omitempty"`
Priority uint `form:"priority,omitempty"`
RequestCondition string `form:"request_condition,omitempty"`
CacheCondition string `form:"cache_condition,omitempty"`
ResponseCondition string `form:"response_condition,omitempty"`
}
// CreateHeader creates a new Fastly header.
func (c *Client) CreateHeader(i *CreateHeaderInput) (*Header, error) {
if i.Service == "" {
return nil, ErrMissingService
}
if i.Version == 0 {
return nil, ErrMissingVersion
}
path := fmt.Sprintf("/service/%s/version/%d/header", i.Service, i.Version)
resp, err := c.PostForm(path, i, nil)
if err != nil {
return nil, err
}
var b *Header
if err := decodeJSON(&b, resp.Body); err != nil {
return nil, err
}
return b, nil
}
// GetHeaderInput is used as input to the GetHeader function.
type GetHeaderInput struct {
// Service is the ID of the service. Version is the specific configuration
// version. Both fields are required.
Service string
Version int
// Name is the name of the header to fetch.
Name string
}
// GetHeader gets the header configuration with the given parameters.
func (c *Client) GetHeader(i *GetHeaderInput) (*Header, error) {
if i.Service == "" {
return nil, ErrMissingService
}
if i.Version == 0 {
return nil, ErrMissingVersion
}
if i.Name == "" {
return nil, ErrMissingName
}
path := fmt.Sprintf("/service/%s/version/%d/header/%s", i.Service, i.Version, i.Name)
resp, err := c.Get(path, nil)
if err != nil {
return nil, err
}
var b *Header
if err := decodeJSON(&b, resp.Body); err != nil {
return nil, err
}
return b, nil
}
// UpdateHeaderInput is used as input to the UpdateHeader function.
type UpdateHeaderInput struct {
// Service is the ID of the service. Version is the specific configuration
// version. Both fields are required.
Service string
Version int
// Name is the name of the header to update.
Name string
NewName string `form:"name,omitempty"`
Action HeaderAction `form:"action,omitempty"`
IgnoreIfSet *Compatibool `form:"ignore_if_set,omitempty"`
Type HeaderType `form:"type,omitempty"`
Destination string `form:"dst,omitempty"`
Source string `form:"src,omitempty"`
Regex string `form:"regex,omitempty"`
Substitution string `form:"substitution,omitempty"`
Priority uint `form:"priority,omitempty"`
RequestCondition string `form:"request_condition,omitempty"`
CacheCondition string `form:"cache_condition,omitempty"`
ResponseCondition string `form:"response_condition,omitempty"`
}
// UpdateHeader updates a specific header.
func (c *Client) UpdateHeader(i *UpdateHeaderInput) (*Header, error) {
if i.Service == "" {
return nil, ErrMissingService
}
if i.Version == 0 {
return nil, ErrMissingVersion
}
if i.Name == "" {
return nil, ErrMissingName
}
path := fmt.Sprintf("/service/%s/version/%d/header/%s", i.Service, i.Version, i.Name)
resp, err := c.PutForm(path, i, nil)
if err != nil {
return nil, err
}
var b *Header
if err := decodeJSON(&b, resp.Body); err != nil {
return nil, err
}
return b, nil
}
// DeleteHeaderInput is the input parameter to DeleteHeader.
type DeleteHeaderInput struct {
// Service is the ID of the service. Version is the specific configuration
// version. Both fields are required.
Service string
Version int
// Name is the name of the header to delete (required).
Name string
}
// DeleteHeader deletes the given header version.
func (c *Client) DeleteHeader(i *DeleteHeaderInput) error {
if i.Service == "" {
return ErrMissingService
}
if i.Version == 0 {
return ErrMissingVersion
}
if i.Name == "" {
return ErrMissingName
}
path := fmt.Sprintf("/service/%s/version/%d/header/%s", i.Service, i.Version, i.Name)
resp, err := c.Delete(path, nil)
if err != nil {
return err
}
var r *statusResp
if err := decodeJSON(&r, resp.Body); err != nil {
return err
}
if !r.Ok() {
return fmt.Errorf("Not Ok")
}
return nil
}