Debian Codegraph @

fastly/settings.go

raw symbols SCIP
1 package fastly
2
3 import "fmt"
4
5 // Settings represents a backend response from the Fastly API.
6 type Settings struct {
7 ServiceID string `mapstructure:"service_id"`
8 Version int `mapstructure:"version"`
9
10 DefaultTTL uint `mapstructure:"general.default_ttl"`
11 DefaultHost string `mapstructure:"general.default_host"`
12 StaleIfError bool `mapstructure:"general.stale_if_error"`
13 StaleIfErrorTTL uint `mapstructure:"general.stale_if_error_ttl"`
14 }
15
16 // GetSettingsInput is used as input to the GetSettings function.
17 type GetSettingsInput struct {
18 // Service is the ID of the service. Version is the specific configuration
19 // version. Both fields are required.
20 Service string
21 Version int
22 }
23
24 // GetSettings gets the backend configuration with the given parameters.
25 func (c *Client) GetSettings(i *GetSettingsInput) (*Settings, error) {
26 if i.Service == "" {
27 return nil, ErrMissingService
28 }
29
30 if i.Version == 0 {
31 return nil, ErrMissingVersion
32 }
33
34 path := fmt.Sprintf("/service/%s/version/%d/settings", i.Service, i.Version)
35 resp, err := c.Get(path, nil)
36 if err != nil {
37 return nil, err
38 }
39
40 var b *Settings
41 if err := decodeJSON(&b, resp.Body); err != nil {
42 return nil, err
43 }
44 return b, nil
45 }
46
47 // UpdateSettingsInput is used as input to the UpdateSettings function.
48 type UpdateSettingsInput struct {
49 // Service is the ID of the service. Version is the specific configuration
50 // version. Both fields are required.
51 Service string
52 Version int
53
54 DefaultTTL uint `form:"general.default_ttl"`
55 DefaultHost string `form:"general.default_host,omitempty"`
56 StaleIfError bool `form:"general.stale_if_error,omitempty"`
57 StaleIfErrorTTL uint `form:"general.stale_if_error_ttl,omitempty"`
58 }
59
60 // UpdateSettings updates a specific backend.
61 func (c *Client) UpdateSettings(i *UpdateSettingsInput) (*Settings, error) {
62 if i.Service == "" {
63 return nil, ErrMissingService
64 }
65
66 if i.Version == 0 {
67 return nil, ErrMissingVersion
68 }
69
70 path := fmt.Sprintf("/service/%s/version/%d/settings", i.Service, i.Version)
71 resp, err := c.PutForm(path, i, nil)
72 if err != nil {
73 return nil, err
74 }
75
76 var b *Settings
77 if err := decodeJSON(&b, resp.Body); err != nil {
78 return nil, err
79 }
80 return b, nil
81 }