Debian Codegraph @

fastly/dictionary.go

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