-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathappsettings.go
More file actions
60 lines (48 loc) · 1.63 KB
/
appsettings.go
File metadata and controls
60 lines (48 loc) · 1.63 KB
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
// Package appsettings provides methods for managing application settings of apps.
package appsettings
import (
"encoding/json"
"fmt"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
)
// List lists an app's settings.
func List(c *drycc.Client, app string) (api.AppSettings, error) {
u := fmt.Sprintf("/v2/apps/%s/settings/", app)
res, reqErr := c.Request("GET", u, nil)
if reqErr != nil {
return api.AppSettings{}, reqErr
}
defer res.Body.Close()
settings := api.AppSettings{}
if err := json.NewDecoder(res.Body).Decode(&settings); err != nil {
return api.AppSettings{}, err
}
return settings, reqErr
}
// Set sets an app's settings variables.
// This is a patching operation, which means when you call Set() with an api.AppSettings:
//
// - If the variable does not exist, it will be set.
// - If the variable exists, it will be overwritten.
// - If the variable is set to nil, it will be unset.
// - If the variable was ignored in the api.AppSettings, it will remain unchanged.
//
// Calling Set() with an empty api.AppSettings will return a drycc.ErrConflict.
func Set(c *drycc.Client, app string, appSettings api.AppSettings) (api.AppSettings, error) {
body, err := json.Marshal(appSettings)
if err != nil {
return api.AppSettings{}, err
}
u := fmt.Sprintf("/v2/apps/%s/settings/", app)
res, reqErr := c.Request("POST", u, body)
if reqErr != nil {
return api.AppSettings{}, reqErr
}
defer res.Body.Close()
newAppSettings := api.AppSettings{}
if err = json.NewDecoder(res.Body).Decode(&newAppSettings); err != nil {
return api.AppSettings{}, err
}
return newAppSettings, reqErr
}