-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.go
116 lines (100 loc) · 2.99 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
package directus
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/perimeterx/marshmallow"
)
type Settings struct {
ID int32 `json:"id"`
ProjectName string `json:"project_name"`
ProjectURL Nullable[string] `json:"project_url"`
ProjectDescriptor Nullable[string] `json:"project_descriptor"`
ProjectColor string `json:"project_color"`
DefaultLanguage string `json:"default_language"`
ModuleBar []ModuleBar `json:"module_bar,omitempty"`
AuthPasswordPolicy Nullable[string] `json:"auth_password_policy"`
AuthLoginAttempts int32 `json:"auth_login_attempts"`
CustomCSS Nullable[string] `json:"custom_css"`
Unknown map[string]any `json:"-"`
}
func (settings *Settings) UnmarshalJSON(data []byte) error {
values, err := marshmallow.Unmarshal(data, settings, marshmallow.WithExcludeKnownFieldsFromMap(true))
if err != nil {
return err
}
settings.Unknown = values
return nil
}
func (settings *Settings) MarshalJSON() ([]byte, error) {
type alias Settings
base, err := json.Marshal((*alias)(settings))
if err != nil {
return nil, err
}
m := make(map[string]any)
for k, v := range settings.Unknown {
m[k] = v
}
if err := json.Unmarshal(base, &m); err != nil {
return nil, err
}
return json.Marshal(m)
}
type ModuleBar struct {
ID string `json:"id"`
Locked bool `json:"locked,omitempty"`
Enabled bool `json:"enabled"`
Type ModuleBarType `json:"type"`
Name string `json:"name,omitempty"`
Icon Icon `json:"icon,omitempty"`
URL string `json:"url,omitempty"`
}
type ModuleBarType string
const (
ModuleBarTypeLink ModuleBarType = "link"
ModuleBarTypeModule ModuleBarType = "module"
)
func (tp *ModuleBarType) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
*tp = ModuleBarType(str)
return nil
}
type clientSettings struct {
client *Client
}
func (cr *clientSettings) Get(ctx context.Context) (*Settings, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, cr.client.urlf("/settings"), nil)
if err != nil {
return nil, fmt.Errorf("directus: cannot prepare request: %v", err)
}
reply := struct {
Data *Settings `json:"data"`
}{}
if err := cr.client.sendRequest(req, &reply); err != nil {
return nil, err
}
return reply.Data, nil
}
func (cr *clientSettings) Update(ctx context.Context, settings *Settings) (*Settings, error) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(settings); err != nil {
return nil, fmt.Errorf("directus: cannot encode request: %v", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, cr.client.urlf("/settings"), &buf)
if err != nil {
return nil, fmt.Errorf("directus: cannot prepare request: %v", err)
}
reply := struct {
Data *Settings `json:"data"`
}{}
if err := cr.client.sendRequest(req, &reply); err != nil {
return nil, err
}
return reply.Data, nil
}