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