-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.go
182 lines (145 loc) · 4.79 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2022 Ralf Geschke. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package golrackpi
import (
"encoding/json"
"strings"
"errors"
"io/ioutil"
"net/http"
)
// SettingsDataValues specifies the structure of a setting element returned by a request to the "settings" endpoint
type SettingsDataValues struct {
Id string `json:"id"`
Max string `json:"max"`
Min string `json:"min"`
Unit string `json:"unit"`
Type interface{} `json:"type"`
Access interface{} `json:"access"`
Default string `json:"default"`
}
// SettingsData specifies the structure of the response returned by a request to the "settings" endpoint.
// It embeds a slice of SettingsDataValues type.
type SettingsData struct {
ModuleId string `json:"moduleid"`
Settings []SettingsDataValues `json:"settings"`
}
// SettingsValues specifies the structure of the response returned by a request to the "settings/moduleid/..." endpoint.
// The structure defines a settingid and its value.
type SettingsValues struct {
Id string `json:"id"`
Value string `json:"value"`
}
// Settings returns a list of all modules with their setting identifiers and further parameters of the setting, i.e. max, min, default etc.
// Warning: The request returns a lot of data, so it takes some time.
func (c *AuthClient) Settings() ([]SettingsData, error) {
jsonResult := []SettingsData{}
client := http.Client{}
request, err := http.NewRequest("GET", c.getUrl("/api/v1/settings"), nil)
if err != nil {
return jsonResult, err
}
request.Header.Add("authorization", "Session "+c.SessionId)
response, err := client.Do(request)
if err != nil {
return jsonResult, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return jsonResult, err
}
err = json.Unmarshal(body, &jsonResult)
if err != nil {
return jsonResult, err
}
return jsonResult, nil
}
// SettingsModule returns a list of settings with settingids and their values of a moduleid
func (c *AuthClient) SettingsModule(moduleid string) ([]SettingsValues, error) {
jsonResult := []SettingsValues{}
client := http.Client{}
request, err := http.NewRequest("GET", c.getUrl("/api/v1/settings/"+moduleid), nil)
if err != nil {
return jsonResult, err
}
request.Header.Add("authorization", "Session "+c.SessionId)
response, err := client.Do(request)
if err != nil {
return jsonResult, err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return jsonResult, errors.New("module or setting not found")
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return jsonResult, err
}
err = json.Unmarshal(body, &jsonResult)
if err != nil {
return jsonResult, err
}
return jsonResult, nil
}
// SettingsModuleSetting returns a SettingsValues slice with length 1 according to the submitted
// moduleid and settingid parameter.
func (c *AuthClient) SettingsModuleSetting(moduleid string, settingid string) ([]SettingsValues, error) {
jsonResult := []SettingsValues{}
client := http.Client{}
request, err := http.NewRequest("GET", c.getUrl("/api/v1/settings/"+moduleid+"/"+settingid), nil)
if err != nil {
return jsonResult, err
}
request.Header.Add("authorization", "Session "+c.SessionId)
response, err := client.Do(request)
if err != nil {
return jsonResult, err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return jsonResult, errors.New("module or setting not found")
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return jsonResult, err
}
err = json.Unmarshal(body, &jsonResult)
if err != nil {
return jsonResult, err
}
return jsonResult, nil
}
// SettingsModuleSettings returns a SettingsValues slice according to the submitted
// moduleid and settingids parameter. This function takes an arbitrary number of setting ids as arguments.
func (c *AuthClient) SettingsModuleSettings(moduleid string, settingids ...string) ([]SettingsValues, error) {
jsonResult := []SettingsValues{}
client := http.Client{}
for i, settingid := range settingids {
settingids[i] = strings.TrimSpace(settingid)
}
csvSettings := strings.Join(settingids, ",")
request, err := http.NewRequest("GET", c.getUrl("/api/v1/settings/"+moduleid+"/"+csvSettings), nil)
if err != nil {
return jsonResult, err
}
request.Header.Add("authorization", "Session "+c.SessionId)
response, err := client.Do(request)
if err != nil {
return jsonResult, err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return jsonResult, errors.New("module or setting not found")
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return jsonResult, err
}
err = json.Unmarshal(body, &jsonResult)
if err != nil {
return jsonResult, err
}
return jsonResult, nil
}