| 1 |
package fastly |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/url" |
| 6 |
"sort" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
type DictionaryItem struct { |
| 12 |
ServiceID string `mapstructure:"service_id"` |
| 13 |
DictionaryID string `mapstructure:"dictionary_id"` |
| 14 |
ItemKey string `mapstructure:"item_key"` |
| 15 |
|
| 16 |
ItemValue string `mapstructure:"item_value"` |
| 17 |
CreatedAt *time.Time `mapstructure:"created_at"` |
| 18 |
UpdatedAt *time.Time `mapstructure:"updated_at"` |
| 19 |
DeletedAt *time.Time `mapstructure:"deleted_at"` |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
type dictionaryItemsByKey []*DictionaryItem |
| 24 |
|
| 25 |
|
| 26 |
func (s dictionaryItemsByKey) Len() int { return len(s) } |
| 27 |
func (s dictionaryItemsByKey) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 28 |
func (s dictionaryItemsByKey) Less(i, j int) bool { |
| 29 |
return s[i].ItemKey < s[j].ItemKey |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
type ListDictionaryItemsInput struct { |
| 34 |
|
| 35 |
Service string |
| 36 |
|
| 37 |
|
| 38 |
Dictionary string |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
func (c *Client) ListDictionaryItems(i *ListDictionaryItemsInput) ([]*DictionaryItem, error) { |
| 44 |
if i.Service == "" { |
| 45 |
return nil, ErrMissingService |
| 46 |
} |
| 47 |
|
| 48 |
if i.Dictionary == "" { |
| 49 |
return nil, ErrMissingDictionary |
| 50 |
} |
| 51 |
|
| 52 |
path := fmt.Sprintf("/service/%s/dictionary/%s/items", i.Service, i.Dictionary) |
| 53 |
resp, err := c.Get(path, nil) |
| 54 |
if err != nil { |
| 55 |
return nil, err |
| 56 |
} |
| 57 |
|
| 58 |
var bs []*DictionaryItem |
| 59 |
if err := decodeJSON(&bs, resp.Body); err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
sort.Stable(dictionaryItemsByKey(bs)) |
| 63 |
return bs, nil |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
type CreateDictionaryItemInput struct { |
| 68 |
|
| 69 |
|
| 70 |
Service string |
| 71 |
Dictionary string |
| 72 |
|
| 73 |
ItemKey string `form:"item_key,omitempty"` |
| 74 |
ItemValue string `form:"item_value,omitempty"` |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
func (c *Client) CreateDictionaryItem(i *CreateDictionaryItemInput) (*DictionaryItem, error) { |
| 79 |
if i.Service == "" { |
| 80 |
return nil, ErrMissingService |
| 81 |
} |
| 82 |
|
| 83 |
if i.Dictionary == "" { |
| 84 |
return nil, ErrMissingDictionary |
| 85 |
} |
| 86 |
|
| 87 |
path := fmt.Sprintf("/service/%s/dictionary/%s/item", i.Service, i.Dictionary) |
| 88 |
resp, err := c.PostForm(path, i, nil) |
| 89 |
if err != nil { |
| 90 |
return nil, err |
| 91 |
} |
| 92 |
|
| 93 |
var b *DictionaryItem |
| 94 |
if err := decodeJSON(&b, resp.Body); err != nil { |
| 95 |
return nil, err |
| 96 |
} |
| 97 |
return b, nil |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
func (c *Client) CreateDictionaryItems(i []CreateDictionaryItemInput) ([]DictionaryItem, error) { |
| 102 |
|
| 103 |
var b []DictionaryItem |
| 104 |
for _, cdii := range i { |
| 105 |
di, err := c.CreateDictionaryItem(&cdii) |
| 106 |
if err != nil { |
| 107 |
return nil, err |
| 108 |
} |
| 109 |
b = append(b, *di) |
| 110 |
} |
| 111 |
return b, nil |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
type GetDictionaryItemInput struct { |
| 116 |
|
| 117 |
|
| 118 |
Service string |
| 119 |
Dictionary string |
| 120 |
|
| 121 |
|
| 122 |
ItemKey string |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
func (c *Client) GetDictionaryItem(i *GetDictionaryItemInput) (*DictionaryItem, error) { |
| 127 |
if i.Service == "" { |
| 128 |
return nil, ErrMissingService |
| 129 |
} |
| 130 |
|
| 131 |
if i.Dictionary == "" { |
| 132 |
return nil, ErrMissingDictionary |
| 133 |
} |
| 134 |
|
| 135 |
if i.ItemKey == "" { |
| 136 |
return nil, ErrMissingItemKey |
| 137 |
} |
| 138 |
|
| 139 |
path := fmt.Sprintf("/service/%s/dictionary/%s/item/%s", i.Service, i.Dictionary, url.PathEscape(i.ItemKey)) |
| 140 |
resp, err := c.Get(path, nil) |
| 141 |
if err != nil { |
| 142 |
return nil, err |
| 143 |
} |
| 144 |
|
| 145 |
var b *DictionaryItem |
| 146 |
if err := decodeJSON(&b, resp.Body); err != nil { |
| 147 |
return nil, err |
| 148 |
} |
| 149 |
return b, nil |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
type UpdateDictionaryItemInput struct { |
| 154 |
|
| 155 |
|
| 156 |
Service string |
| 157 |
Dictionary string |
| 158 |
|
| 159 |
|
| 160 |
ItemKey string |
| 161 |
|
| 162 |
ItemValue string `form:"item_value,omitempty"` |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
func (c *Client) UpdateDictionaryItem(i *UpdateDictionaryItemInput) (*DictionaryItem, error) { |
| 167 |
if i.Service == "" { |
| 168 |
return nil, ErrMissingService |
| 169 |
} |
| 170 |
|
| 171 |
if i.Dictionary == "" { |
| 172 |
return nil, ErrMissingDictionary |
| 173 |
} |
| 174 |
|
| 175 |
if i.ItemKey == "" { |
| 176 |
return nil, ErrMissingItemKey |
| 177 |
} |
| 178 |
|
| 179 |
path := fmt.Sprintf("/service/%s/dictionary/%s/item/%s", i.Service, i.Dictionary, url.PathEscape(i.ItemKey)) |
| 180 |
resp, err := c.PutForm(path, i, nil) |
| 181 |
if err != nil { |
| 182 |
return nil, err |
| 183 |
} |
| 184 |
|
| 185 |
var b *DictionaryItem |
| 186 |
if err := decodeJSON(&b, resp.Body); err != nil { |
| 187 |
return nil, err |
| 188 |
} |
| 189 |
return b, nil |
| 190 |
} |
| 191 |
|
| 192 |
type BatchModifyDictionaryItemsInput struct { |
| 193 |
Service string `json:"-,"` |
| 194 |
Dictionary string `json:"-,"` |
| 195 |
|
| 196 |
Items []*BatchDictionaryItem `json:"items"` |
| 197 |
} |
| 198 |
|
| 199 |
type BatchDictionaryItem struct { |
| 200 |
|
| 201 |
|
| 202 |
Operation BatchOperation `json:"op"` |
| 203 |
ItemKey string `json:"item_key"` |
| 204 |
ItemValue string `json:"item_value"` |
| 205 |
} |
| 206 |
|
| 207 |
func (c *Client) BatchModifyDictionaryItems(i *BatchModifyDictionaryItemsInput) error { |
| 208 |
|
| 209 |
if i.Service == "" { |
| 210 |
return ErrMissingService |
| 211 |
} |
| 212 |
|
| 213 |
if i.Dictionary == "" { |
| 214 |
return ErrMissingDictionary |
| 215 |
} |
| 216 |
|
| 217 |
if len(i.Items) > BatchModifyMaximumOperations { |
| 218 |
return ErrBatchUpdateMaximumOperationsExceeded |
| 219 |
} |
| 220 |
|
| 221 |
path := fmt.Sprintf("/service/%s/dictionary/%s/items", i.Service, i.Dictionary) |
| 222 |
resp, err := c.PatchJSON(path, i, nil) |
| 223 |
if err != nil { |
| 224 |
return err |
| 225 |
} |
| 226 |
|
| 227 |
var batchModifyResult map[string]string |
| 228 |
if err := decodeJSON(&batchModifyResult, resp.Body); err != nil { |
| 229 |
return err |
| 230 |
} |
| 231 |
|
| 232 |
return nil |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
type DeleteDictionaryItemInput struct { |
| 237 |
|
| 238 |
|
| 239 |
Service string |
| 240 |
Dictionary string |
| 241 |
|
| 242 |
|
| 243 |
ItemKey string |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
func (c *Client) DeleteDictionaryItem(i *DeleteDictionaryItemInput) error { |
| 248 |
if i.Service == "" { |
| 249 |
return ErrMissingService |
| 250 |
} |
| 251 |
|
| 252 |
if i.Dictionary == "" { |
| 253 |
return ErrMissingDictionary |
| 254 |
} |
| 255 |
|
| 256 |
if i.ItemKey == "" { |
| 257 |
return ErrMissingItemKey |
| 258 |
} |
| 259 |
|
| 260 |
path := fmt.Sprintf("/service/%s/dictionary/%s/item/%s", i.Service, i.Dictionary, url.PathEscape(i.ItemKey)) |
| 261 |
resp, err := c.Delete(path, nil) |
| 262 |
if err != nil { |
| 263 |
return err |
| 264 |
} |
| 265 |
defer resp.Body.Close() |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
return nil |
| 270 |
} |