Debian Codegraph @

fastly/response_object.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "net/url"
6 "sort"
7 "time"
8 )
9
10 // ResponseObject represents a response object response from the Fastly API.
11 type ResponseObject struct {
12 ServiceID string `mapstructure:"service_id"`
13 Version int `mapstructure:"version"`
14
15 Name string `mapstructure:"name"`
16 Status uint `mapstructure:"status"`
17 Response string `mapstructure:"response"`
18 Content string `mapstructure:"content"`
19 ContentType string `mapstructure:"content_type"`
20 RequestCondition string `mapstructure:"request_condition"`
21 CacheCondition string `mapstructure:"cache_condition"`
22 CreatedAt *time.Time `mapstructure:"created_at"`
23 UpdatedAt *time.Time `mapstructure:"updated_at"`
24 DeletedAt *time.Time `mapstructure:"deleted_at"`
25 }
26
27 // responseObjectsByName is a sortable list of response objects.
28 type responseObjectsByName []*ResponseObject
29
30 // Len, Swap, and Less implement the sortable interface.
31 func (s responseObjectsByName) Len() int { return len(s) }
32 func (s responseObjectsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
33 func (s responseObjectsByName) Less(i, j int) bool {
34 return s[i].Name < s[j].Name
35 }
36
37 // ListResponseObjectsInput is used as input to the ListResponseObjects
38 // function.
39 type ListResponseObjectsInput struct {
40 // Service is the ID of the service (required).
41 Service string
42
43 // Version is the specific configuration version (required).
44 Version int
45 }
46
47 // ListResponseObjects returns the list of response objects for the
48 // configuration version.
49 func (c *Client) ListResponseObjects(i *ListResponseObjectsInput) ([]*ResponseObject, error) {
50 if i.Service == "" {
51 return nil, ErrMissingService
52 }
53
54 if i.Version == 0 {
55 return nil, ErrMissingVersion
56 }
57
58 path := fmt.Sprintf("/service/%s/version/%d/response_object", i.Service, i.Version)
59 resp, err := c.Get(path, nil)
60 if err != nil {
61 return nil, err
62 }
63
64 var bs []*ResponseObject
65 if err := decodeJSON(&bs, resp.Body); err != nil {
66 return nil, err
67 }
68 sort.Stable(responseObjectsByName(bs))
69 return bs, nil
70 }
71
72 // CreateResponseObjectInput is used as input to the CreateResponseObject
73 // function.
74 type CreateResponseObjectInput struct {
75 // Service is the ID of the service. Version is the specific configuration
76 // version. Both fields are required.
77 Service string
78 Version int
79
80 Name string `form:"name,omitempty"`
81 Status uint `form:"status,omitempty"`
82 Response string `form:"response,omitempty"`
83 Content string `form:"content,omitempty"`
84 ContentType string `form:"content_type,omitempty"`
85 RequestCondition string `form:"request_condition,omitempty"`
86 CacheCondition string `form:"cache_condition,omitempty"`
87 }
88
89 // CreateResponseObject creates a new Fastly response object.
90 func (c *Client) CreateResponseObject(i *CreateResponseObjectInput) (*ResponseObject, error) {
91 if i.Service == "" {
92 return nil, ErrMissingService
93 }
94
95 if i.Version == 0 {
96 return nil, ErrMissingVersion
97 }
98
99 path := fmt.Sprintf("/service/%s/version/%d/response_object", i.Service, i.Version)
100 resp, err := c.PostForm(path, i, nil)
101 if err != nil {
102 return nil, err
103 }
104
105 var b *ResponseObject
106 if err := decodeJSON(&b, resp.Body); err != nil {
107 return nil, err
108 }
109 return b, nil
110 }
111
112 // GetResponseObjectInput is used as input to the GetResponseObject function.
113 type GetResponseObjectInput struct {
114 // Service is the ID of the service. Version is the specific configuration
115 // version. Both fields are required.
116 Service string
117 Version int
118
119 // Name is the name of the response object to fetch.
120 Name string
121 }
122
123 // GetResponseObject gets the response object configuration with the given
124 // parameters.
125 func (c *Client) GetResponseObject(i *GetResponseObjectInput) (*ResponseObject, error) {
126 if i.Service == "" {
127 return nil, ErrMissingService
128 }
129
130 if i.Version == 0 {
131 return nil, ErrMissingVersion
132 }
133
134 if i.Name == "" {
135 return nil, ErrMissingName
136 }
137
138 path := fmt.Sprintf("/service/%s/version/%d/response_object/%s", i.Service, i.Version, url.PathEscape(i.Name))
139 resp, err := c.Get(path, nil)
140 if err != nil {
141 return nil, err
142 }
143
144 var b *ResponseObject
145 if err := decodeJSON(&b, resp.Body); err != nil {
146 return nil, err
147 }
148 return b, nil
149 }
150
151 // UpdateResponseObjectInput is used as input to the UpdateResponseObject
152 // function.
153 type UpdateResponseObjectInput struct {
154 // Service is the ID of the service. Version is the specific configuration
155 // version. Both fields are required.
156 Service string
157 Version int
158
159 // Name is the name of the response object to update.
160 Name string
161
162 NewName string `form:"name,omitempty"`
163 Status uint `form:"status,omitempty"`
164 Response string `form:"response,omitempty"`
165 Content string `form:"content,omitempty"`
166 ContentType string `form:"content_type,omitempty"`
167 RequestCondition string `form:"request_condition,omitempty"`
168 CacheCondition string `form:"cache_condition,omitempty"`
169 }
170
171 // UpdateResponseObject updates a specific response object.
172 func (c *Client) UpdateResponseObject(i *UpdateResponseObjectInput) (*ResponseObject, error) {
173 if i.Service == "" {
174 return nil, ErrMissingService
175 }
176
177 if i.Version == 0 {
178 return nil, ErrMissingVersion
179 }
180
181 if i.Name == "" {
182 return nil, ErrMissingName
183 }
184
185 path := fmt.Sprintf("/service/%s/version/%d/response_object/%s", i.Service, i.Version, url.PathEscape(i.Name))
186 resp, err := c.PutForm(path, i, nil)
187 if err != nil {
188 return nil, err
189 }
190
191 var b *ResponseObject
192 if err := decodeJSON(&b, resp.Body); err != nil {
193 return nil, err
194 }
195 return b, nil
196 }
197
198 // DeleteResponseObjectInput is the input parameter to DeleteResponseObject.
199 type DeleteResponseObjectInput struct {
200 // Service is the ID of the service. Version is the specific configuration
201 // version. Both fields are required.
202 Service string
203 Version int
204
205 // Name is the name of the response object to delete (required).
206 Name string
207 }
208
209 // DeleteResponseObject deletes the given response object version.
210 func (c *Client) DeleteResponseObject(i *DeleteResponseObjectInput) error {
211 if i.Service == "" {
212 return ErrMissingService
213 }
214
215 if i.Version == 0 {
216 return ErrMissingVersion
217 }
218
219 if i.Name == "" {
220 return ErrMissingName
221 }
222
223 path := fmt.Sprintf("/service/%s/version/%d/response_object/%s", i.Service, i.Version, url.PathEscape(i.Name))
224 resp, err := c.Delete(path, nil)
225 if err != nil {
226 return err
227 }
228
229 var r *statusResp
230 if err := decodeJSON(&r, resp.Body); err != nil {
231 return err
232 }
233 if !r.Ok() {
234 return fmt.Errorf("Not Ok")
235 }
236 return nil
237 }