-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.go
More file actions
206 lines (176 loc) · 5.96 KB
/
server.go
File metadata and controls
206 lines (176 loc) · 5.96 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package dvls
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
// Server represents the available server instance information.
type Server struct {
AccessUri string
TimeZone string
ServerName string `json:"servername"`
Version string
SystemMessage string
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (s *Server) UnmarshalJSON(d []byte) error {
raw := struct {
Data struct {
AccessUri string
SelectedTimeZoneId string
ServerName string
Version string
SystemMessage string
}
}{}
err := json.Unmarshal(d, &raw)
if err != nil {
return err
}
s.TimeZone = raw.Data.SelectedTimeZoneId
s.AccessUri = raw.Data.AccessUri
s.ServerName = raw.Data.ServerName
s.Version = raw.Data.Version
s.SystemMessage = raw.Data.SystemMessage
return nil
}
// Timezone represents a Server timezone.
type Timezone struct {
Id string
DisplayName string
StandardName string
DaylightName string
BaseUtcOffset string
AdjustmentRules []TimezoneAdjustmentRule
SupportsDaylightSavingTime bool
}
// TimezoneAdjustmentRule represents a Timezone Adjustment Rule.
type TimezoneAdjustmentRule struct {
DateStart ServerTime
DateEnd ServerTime
DaylightDelta string
DaylightTransitionStart TimezoneAdjustmentRuleTransitionTime
DaylightTransitionEnd TimezoneAdjustmentRuleTransitionTime
BaseUtcOffsetDelta string
NoDaylightTransitions bool
}
// TimezoneAdjustmentRuleTransitionTime represents a Timezone Adjustment Rule Transition Time.
type TimezoneAdjustmentRuleTransitionTime struct {
TimeOfDay ServerTime
Month int
Week int
Day int
DayOfWeek int
IsFixedDateRule bool
}
// ServerTime represents a time.Time that parses the correct server time layout.
type ServerTime struct {
time.Time
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (z *ServerTime) UnmarshalJSON(d []byte) error {
s := strings.Trim(string(d), "\"")
if s == "null" {
return nil
}
for _, layout := range serverTimeLayouts {
if dateParsed, err := time.Parse(layout, s); err == nil {
z.Time = dateParsed
return nil
}
}
return fmt.Errorf("cannot parse server time %q", s)
}
const (
serverPublicInfoEndpoint string = "api/public-instance-information"
serverPrivateInfoEndpoint string = "api/private-instance-information"
serverTimezonesEndpoint string = "/api/configuration/timezones"
)
var serverTimeLayouts = []string{
time.RFC3339Nano,
"2006-01-02T15:04:05.9999999Z07:00",
"2006-01-02T15:04:05.9999999Z",
"2006-01-02T15:04:05",
}
// GetPublicServerInfo returns Server that contains public information on the DVLS instance.
func (c *Client) GetPublicServerInfo() (Server, error) {
return c.GetPublicServerInfoWithContext(context.Background())
}
// GetPublicServerInfoWithContext returns Server that contains public information on the DVLS instance.
// The provided context can be used to cancel the request.
func (c *Client) GetPublicServerInfoWithContext(ctx context.Context) (Server, error) {
var server Server
reqUrl, err := url.JoinPath(c.baseUri, serverPublicInfoEndpoint)
if err != nil {
return Server{}, fmt.Errorf("failed to build server info url: %w", err)
}
resp, err := c.RequestWithContext(ctx, reqUrl, http.MethodGet, nil)
if err != nil {
return Server{}, fmt.Errorf("error while fetching server info: %w", err)
} else if err = resp.CheckRespSaveResult(); err != nil {
return Server{}, err
}
err = json.Unmarshal(resp.Response, &server)
if err != nil {
return Server{}, fmt.Errorf("failed to unmarshal response body: %w", err)
}
return server, nil
}
// GetPrivateServerInfo returns Server that contains private information on the DVLS instance (need authentication).
func (c *Client) GetPrivateServerInfo() (Server, error) {
return c.GetPrivateServerInfoWithContext(context.Background())
}
// GetPrivateServerInfoWithContext returns Server that contains private information on the DVLS instance (need authentication).
// The provided context can be used to cancel the request.
func (c *Client) GetPrivateServerInfoWithContext(ctx context.Context) (Server, error) {
var server Server
reqUrl, err := url.JoinPath(c.baseUri, serverPrivateInfoEndpoint)
if err != nil {
return Server{}, fmt.Errorf("failed to build server info url: %w", err)
}
resp, err := c.RequestWithContext(ctx, reqUrl, http.MethodGet, nil)
if err != nil {
return Server{}, fmt.Errorf("error while fetching server info: %w", err)
} else if err = resp.CheckRespSaveResult(); err != nil {
return Server{}, err
}
err = json.Unmarshal(resp.Response, &server)
if err != nil {
return Server{}, fmt.Errorf("failed to unmarshal response body: %w", err)
}
return server, nil
}
// GetServerTimezones returns an array of Timezone that contains all of the available timezones on
// the DVLS instance.
func (c *Client) GetServerTimezones() ([]Timezone, error) {
return c.GetServerTimezonesWithContext(context.Background())
}
// GetServerTimezonesWithContext returns an array of Timezone that contains all of the available timezones on
// the DVLS instance.
// The provided context can be used to cancel the request.
func (c *Client) GetServerTimezonesWithContext(ctx context.Context) ([]Timezone, error) {
var timezones []Timezone
reqUrl, err := url.JoinPath(c.baseUri, serverTimezonesEndpoint)
if err != nil {
return nil, fmt.Errorf("failed to build timezone info url: %w", err)
}
resp, err := c.RequestWithContext(ctx, reqUrl, http.MethodGet, nil)
if err != nil {
return nil, fmt.Errorf("error while fetching timezones: %w", err)
} else if err = resp.CheckRespSaveResult(); err != nil {
return nil, err
}
raw := struct {
Data []Timezone
}{}
err = json.Unmarshal(resp.Response, &raw)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response body: %w", err)
}
timezones = raw.Data
return timezones, nil
}