File: settings.go

package info (click to toggle)
golang-github-sethvargo-go-fastly 1.2.1%2Bgit20190805.5c6c8bd-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,584 kB
  • sloc: makefile: 71
file content (81 lines) | stat: -rw-r--r-- 2,228 bytes parent folder | download | duplicates (2)
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
package fastly

import "fmt"

// Settings represents a backend response from the Fastly API.
type Settings struct {
	ServiceID string `mapstructure:"service_id"`
	Version   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 {
	// Service is the ID of the service. Version is the specific configuration
	// version. Both fields are required.
	Service string
	Version int
}

// GetSettings gets the backend configuration with the given parameters.
func (c *Client) GetSettings(i *GetSettingsInput) (*Settings, error) {
	if i.Service == "" {
		return nil, ErrMissingService
	}

	if i.Version == 0 {
		return nil, ErrMissingVersion
	}

	path := fmt.Sprintf("/service/%s/version/%d/settings", i.Service, i.Version)
	resp, err := c.Get(path, nil)
	if err != nil {
		return nil, err
	}

	var b *Settings
	if err := decodeJSON(&b, resp.Body); err != nil {
		return nil, err
	}
	return b, nil
}

// UpdateSettingsInput is used as input to the UpdateSettings function.
type UpdateSettingsInput struct {
	// Service is the ID of the service. Version is the specific configuration
	// version. Both fields are required.
	Service string
	Version 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.Service == "" {
		return nil, ErrMissingService
	}

	if i.Version == 0 {
		return nil, ErrMissingVersion
	}

	path := fmt.Sprintf("/service/%s/version/%d/settings", i.Service, i.Version)
	resp, err := c.PutForm(path, i, nil)
	if err != nil {
		return nil, err
	}

	var b *Settings
	if err := decodeJSON(&b, resp.Body); err != nil {
		return nil, err
	}
	return b, nil
}