forked from RnDity/time
/
timesource.go
75 lines (59 loc) · 1.48 KB
/
timesource.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
package time
import (
"context"
"encoding/json"
"github.com/pkg/errors"
"net/http"
"time"
)
type Source interface {
Now(ctx context.Context) (*time.Time, error)
}
func NewSystemSource() Source {
return systemSource{}
}
type systemSource struct{}
func (ss systemSource) Now(ctx context.Context) (*time.Time, error) {
now := time.Now()
return &now, nil
}
func NewRESTSource(url string) Source {
return restSource{url}
}
type restSource struct {
url string
}
func (rs restSource) Now(ctx context.Context) (*time.Time, error) {
// Build the request
req, err := http.NewRequest("GET", rs.url, nil)
if err != nil {
return nil, errors.Wrap(err, "NewRequest: ")
}
// For control over HTTP client headers,
// redirect policy, and other settings,
// create a Client
// A Client is an HTTP client
client := &http.Client{}
// Send the request via a client
// Do sends an HTTP request and
// returns an HTTP response
resp, err := client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "Do: ")
}
// Callers should close resp.Body
// when done reading from it
// Defer the closing of the body
defer resp.Body.Close()
type TimeResponse struct {
Time string
}
// Fill the record with the data from the JSON
var record TimeResponse
// Use json.Decode for reading streams of JSON data
if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {
return nil, errors.Wrap(err, "Decode: ")
}
time, err := time.Parse(time.RFC3339, record.Time)
return &time, err
}