-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
179 lines (153 loc) · 4.81 KB
/
server.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
package dvls
import (
"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
}
dateParsed, err := time.Parse(serverTimeLayout, s)
if err != nil {
return err
}
z.Time = dateParsed
return nil
}
const (
serverPublicInfoEndpoint string = "api/public-instance-information"
serverPrivateInfoEndpoint string = "api/private-instance-information"
serverTimezonesEndpoint string = "/api/configuration/timezones"
serverTimeLayout string = "2006-01-02T15:04:05"
)
// GetPublicServerInfo returns Server that contains public information on the DVLS instance.
func (c *Client) GetPublicServerInfo() (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. error: %w", err)
}
resp, err := c.Request(reqUrl, http.MethodGet, nil)
if err != nil {
return Server{}, fmt.Errorf("error while fetching server info. error: %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. error: %w", err)
}
return server, nil
}
// GetPrivateServerInfo returns Server that contains private information on the DVLS instance (need authentication).
func (c *Client) GetPrivateServerInfo() (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. error: %w", err)
}
resp, err := c.Request(reqUrl, http.MethodGet, nil)
if err != nil {
return Server{}, fmt.Errorf("error while fetching server info. error: %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. error: %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) {
var timezones []Timezone
reqUrl, err := url.JoinPath(c.baseUri, serverTimezonesEndpoint)
if err != nil {
return nil, fmt.Errorf("failed to build timezone info url. error: %w", err)
}
resp, err := c.Request(reqUrl, http.MethodGet, nil)
if err != nil {
return nil, fmt.Errorf("error while fetching timezones. error: %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. error: %w", err)
}
timezones = raw.Data
return timezones, nil
}