Debian Codegraph @

fastly/header.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "net/url"
6 "sort"
7 "time"
8 )
9
10 const (
11 // HeaderActionSet is a header action that sets or resets a header.
12 HeaderActionSet HeaderAction = "set"
13
14 // HeaderActionAppend is a header action that appends to an existing header.
15 HeaderActionAppend HeaderAction = "append"
16
17 // HeaderActionDelete is a header action that deletes a header.
18 HeaderActionDelete HeaderAction = "delete"
19
20 // HeaderActionRegex is a header action that performs a single regex
21 // replacement on a header.
22 HeaderActionRegex HeaderAction = "regex"
23
24 // HeaderActionRegexRepeat is a header action that performs a global regex
25 // replacement on a header.
26 HeaderActionRegexRepeat HeaderAction = "regex_repeat"
27 )
28
29 // HeaderAction is a type of header action.
30 type HeaderAction string
31
32 const (
33 // HeaderTypeRequest is a header type that performs on the request before
34 // lookups.
35 HeaderTypeRequest HeaderType = "request"
36
37 // HeaderTypeFetch is a header type that performs on the request to the origin
38 // server.
39 HeaderTypeFetch HeaderType = "fetch"
40
41 // HeaderTypeCache is a header type that performs on the response before it's
42 // store in the cache.
43 HeaderTypeCache HeaderType = "cache"
44
45 // HeaderTypeResponse is a header type that performs on the response before
46 // delivering to the client.
47 HeaderTypeResponse HeaderType = "response"
48 )
49
50 // HeaderType is a type of header.
51 type HeaderType string
52
53 // Header represents a header response from the Fastly API.
54 type Header struct {
55 ServiceID string `mapstructure:"service_id"`
56 Version int `mapstructure:"version"`
57
58 Name string `mapstructure:"name"`
59 Action HeaderAction `mapstructure:"action"`
60 IgnoreIfSet bool `mapstructure:"ignore_if_set"`
61 Type HeaderType `mapstructure:"type"`
62 Destination string `mapstructure:"dst"`
63 Source string `mapstructure:"src"`
64 Regex string `mapstructure:"regex"`
65 Substitution string `mapstructure:"substitution"`
66 Priority uint `mapstructure:"priority"`
67 RequestCondition string `mapstructure:"request_condition"`
68 CacheCondition string `mapstructure:"cache_condition"`
69 ResponseCondition string `mapstructure:"response_condition"`
70 CreatedAt *time.Time `mapstructure:"created_at"`
71 UpdatedAt *time.Time `mapstructure:"updated_at"`
72 DeletedAt *time.Time `mapstructure:"deleted_at"`
73 }
74
75 // headersByName is a sortable list of headers.
76 type headersByName []*Header
77
78 // Len, Swap, and Less implement the sortable interface.
79 func (s headersByName) Len() int { return len(s) }
80 func (s headersByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
81 func (s headersByName) Less(i, j int) bool {
82 return s[i].Name < s[j].Name
83 }
84
85 // ListHeadersInput is used as input to the ListHeaders function.
86 type ListHeadersInput struct {
87 // Service is the ID of the service (required).
88 Service string
89
90 // Version is the specific configuration version (required).
91 Version int
92 }
93
94 // ListHeaders returns the list of headers for the configuration version.
95 func (c *Client) ListHeaders(i *ListHeadersInput) ([]*Header, error) {
96 if i.Service == "" {
97 return nil, ErrMissingService
98 }
99
100 if i.Version == 0 {
101 return nil, ErrMissingVersion
102 }
103
104 path := fmt.Sprintf("/service/%s/version/%d/header", i.Service, i.Version)
105 resp, err := c.Get(path, nil)
106 if err != nil {
107 return nil, err
108 }
109
110 var bs []*Header
111 if err := decodeJSON(&bs, resp.Body); err != nil {
112 return nil, err
113 }
114 sort.Stable(headersByName(bs))
115 return bs, nil
116 }
117
118 // CreateHeaderInput is used as input to the CreateHeader function.
119 type CreateHeaderInput struct {
120 // Service is the ID of the service. Version is the specific configuration
121 // version. Both fields are required.
122 Service string
123 Version int
124
125 Name string `form:"name,omitempty"`
126 Action HeaderAction `form:"action,omitempty"`
127 IgnoreIfSet *Compatibool `form:"ignore_if_set,omitempty"`
128 Type HeaderType `form:"type,omitempty"`
129 Destination string `form:"dst,omitempty"`
130 Source string `form:"src,omitempty"`
131 Regex string `form:"regex,omitempty"`
132 Substitution string `form:"substitution,omitempty"`
133 Priority uint `form:"priority,omitempty"`
134 RequestCondition string `form:"request_condition,omitempty"`
135 CacheCondition string `form:"cache_condition,omitempty"`
136 ResponseCondition string `form:"response_condition,omitempty"`
137 }
138
139 // CreateHeader creates a new Fastly header.
140 func (c *Client) CreateHeader(i *CreateHeaderInput) (*Header, error) {
141 if i.Service == "" {
142 return nil, ErrMissingService
143 }
144
145 if i.Version == 0 {
146 return nil, ErrMissingVersion
147 }
148
149 path := fmt.Sprintf("/service/%s/version/%d/header", i.Service, i.Version)
150 resp, err := c.PostForm(path, i, nil)
151 if err != nil {
152 return nil, err
153 }
154
155 var b *Header
156 if err := decodeJSON(&b, resp.Body); err != nil {
157 return nil, err
158 }
159 return b, nil
160 }
161
162 // GetHeaderInput is used as input to the GetHeader function.
163 type GetHeaderInput struct {
164 // Service is the ID of the service. Version is the specific configuration
165 // version. Both fields are required.
166 Service string
167 Version int
168
169 // Name is the name of the header to fetch.
170 Name string
171 }
172
173 // GetHeader gets the header configuration with the given parameters.
174 func (c *Client) GetHeader(i *GetHeaderInput) (*Header, error) {
175 if i.Service == "" {
176 return nil, ErrMissingService
177 }
178
179 if i.Version == 0 {
180 return nil, ErrMissingVersion
181 }
182
183 if i.Name == "" {
184 return nil, ErrMissingName
185 }
186
187 path := fmt.Sprintf("/service/%s/version/%d/header/%s", i.Service, i.Version, url.PathEscape(i.Name))
188 resp, err := c.Get(path, nil)
189 if err != nil {
190 return nil, err
191 }
192
193 var b *Header
194 if err := decodeJSON(&b, resp.Body); err != nil {
195 return nil, err
196 }
197 return b, nil
198 }
199
200 // UpdateHeaderInput is used as input to the UpdateHeader function.
201 type UpdateHeaderInput struct {
202 // Service is the ID of the service. Version is the specific configuration
203 // version. Both fields are required.
204 Service string
205 Version int
206
207 // Name is the name of the header to update.
208 Name string
209
210 NewName string `form:"name,omitempty"`
211 Action HeaderAction `form:"action,omitempty"`
212 IgnoreIfSet *Compatibool `form:"ignore_if_set,omitempty"`
213 Type HeaderType `form:"type,omitempty"`
214 Destination string `form:"dst,omitempty"`
215 Source string `form:"src,omitempty"`
216 Regex string `form:"regex,omitempty"`
217 Substitution string `form:"substitution,omitempty"`
218 Priority uint `form:"priority,omitempty"`
219 RequestCondition string `form:"request_condition,omitempty"`
220 CacheCondition string `form:"cache_condition,omitempty"`
221 ResponseCondition string `form:"response_condition,omitempty"`
222 }
223
224 // UpdateHeader updates a specific header.
225 func (c *Client) UpdateHeader(i *UpdateHeaderInput) (*Header, error) {
226 if i.Service == "" {
227 return nil, ErrMissingService
228 }
229
230 if i.Version == 0 {
231 return nil, ErrMissingVersion
232 }
233
234 if i.Name == "" {
235 return nil, ErrMissingName
236 }
237
238 path := fmt.Sprintf("/service/%s/version/%d/header/%s", i.Service, i.Version, url.PathEscape(i.Name))
239 resp, err := c.PutForm(path, i, nil)
240 if err != nil {
241 return nil, err
242 }
243
244 var b *Header
245 if err := decodeJSON(&b, resp.Body); err != nil {
246 return nil, err
247 }
248 return b, nil
249 }
250
251 // DeleteHeaderInput is the input parameter to DeleteHeader.
252 type DeleteHeaderInput struct {
253 // Service is the ID of the service. Version is the specific configuration
254 // version. Both fields are required.
255 Service string
256 Version int
257
258 // Name is the name of the header to delete (required).
259 Name string
260 }
261
262 // DeleteHeader deletes the given header version.
263 func (c *Client) DeleteHeader(i *DeleteHeaderInput) error {
264 if i.Service == "" {
265 return ErrMissingService
266 }
267
268 if i.Version == 0 {
269 return ErrMissingVersion
270 }
271
272 if i.Name == "" {
273 return ErrMissingName
274 }
275
276 path := fmt.Sprintf("/service/%s/version/%d/header/%s", i.Service, i.Version, url.PathEscape(i.Name))
277 resp, err := c.Delete(path, nil)
278 if err != nil {
279 return err
280 }
281
282 var r *statusResp
283 if err := decodeJSON(&r, resp.Body); err != nil {
284 return err
285 }
286 if !r.Ok() {
287 return fmt.Errorf("Not Ok")
288 }
289 return nil
290 }