forked from igungor/go-putio
-
Notifications
You must be signed in to change notification settings - Fork 8
/
config.go
107 lines (100 loc) · 2.73 KB
/
config.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
package putio
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
// ConfigService represents configuration related operations.
type ConfigService struct {
client *Client
}
// GetAll all fills config.
func (f *ConfigService) GetAll(ctx context.Context, config interface{}) error {
req, err := f.client.NewRequest(ctx, http.MethodGet, "/v2/config", nil)
if err != nil {
return fmt.Errorf("%w", err)
}
var r struct {
Config json.RawMessage `json:"config"`
}
_, err = f.client.Do(req, &r) // nolint:bodyclose
if err != nil {
return fmt.Errorf("%w", err)
}
return json.Unmarshal(r.Config, &config) // nolint:wrapcheck
}
// Get fetches config item via given key.
func (f *ConfigService) Get(ctx context.Context, key string, value interface{}) (found bool, err error) {
req, err := f.client.NewRequest(ctx, http.MethodGet, "/v2/config/"+key, nil)
if err != nil {
return false, err
}
var r struct {
Value *json.RawMessage `json:"value"`
}
_, err = f.client.Do(req, &r) // nolint:bodyclose
if err != nil {
return false, err
}
if r.Value == nil {
return false, nil
}
return true, json.Unmarshal(*r.Value, &value) // nolint:wrapcheck
}
// SetAll updates all config items.
func (f *ConfigService) SetAll(ctx context.Context, config interface{}) error {
b, err := json.Marshal(config)
if err != nil {
return fmt.Errorf("%w", err)
}
v := struct {
Config json.RawMessage `json:"config"`
}{
Config: b,
}
body, err := json.Marshal(v)
if err != nil {
return fmt.Errorf("%w", err)
}
req, err := f.client.NewRequest(ctx, http.MethodPut, "/v2/config", bytes.NewReader(body))
if err != nil {
return fmt.Errorf("%w", err)
}
req.Header.Set("content-type", "application/json")
_, err = f.client.Do(req, nil) // nolint:bodyclose
return fmt.Errorf("%w", err)
}
// Set updates given config key's value.
func (f *ConfigService) Set(ctx context.Context, key string, value interface{}) error {
b, err := json.Marshal(value)
if err != nil {
return fmt.Errorf("%w", err)
}
v := struct {
Value json.RawMessage `json:"value"`
}{
Value: b,
}
body, err := json.Marshal(v)
if err != nil {
return fmt.Errorf("%w", err)
}
req, err := f.client.NewRequest(ctx, http.MethodPut, "/v2/config/"+key, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("%w", err)
}
req.Header.Set("content-type", "application/json")
_, err = f.client.Do(req, nil) // nolint:bodyclose
return fmt.Errorf("%w", err)
}
// Del destroys config item via given key.
func (f *ConfigService) Del(ctx context.Context, key string) error {
req, err := f.client.NewRequest(ctx, http.MethodDelete, "/v2/config/"+key, nil)
if err != nil {
return fmt.Errorf("%w", err)
}
_, err = f.client.Do(req, nil) // nolint:bodyclose
return fmt.Errorf("%w", err)
}