-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_request.go
145 lines (109 loc) · 2.7 KB
/
time_request.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
package pubnub
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"github.com/pubnub/go/pnerr"
)
const timePath = "/time/0"
var emptyTimeResp *TimeResponse
type timeBuilder struct {
opts *timeOpts
}
func newTimeBuilder(pubnub *PubNub) *timeBuilder {
builder := timeBuilder{
opts: &timeOpts{
pubnub: pubnub,
},
}
return &builder
}
func newTimeBuilderWithContext(pubnub *PubNub, context Context) *timeBuilder {
builder := timeBuilder{
opts: &timeOpts{
pubnub: pubnub,
ctx: context,
},
}
return &builder
}
// Transport sets the Transport for the request.
func (b *timeBuilder) Transport(tr http.RoundTripper) *timeBuilder {
b.opts.Transport = tr
return b
}
// Execute runs the Time request and fetches the time from the server.
func (b *timeBuilder) Execute() (*TimeResponse, StatusResponse, error) {
rawJSON, status, err := executeRequest(b.opts)
if err != nil {
return emptyTimeResp, status, err
}
return newTimeResponse(rawJSON, status)
}
type timeOpts struct {
pubnub *PubNub
Transport http.RoundTripper
ctx Context
}
func (o *timeOpts) config() Config {
return *o.pubnub.Config
}
func (o *timeOpts) client() *http.Client {
return o.pubnub.GetClient()
}
func (o *timeOpts) context() Context {
return o.ctx
}
func (o *timeOpts) validate() error {
return nil
}
func (o *timeOpts) buildPath() (string, error) {
return timePath, nil
}
func (o *timeOpts) buildQuery() (*url.Values, error) {
q := defaultQuery(o.pubnub.Config.UUID, o.pubnub.telemetryManager)
return q, nil
}
func (o *timeOpts) buildBody() ([]byte, error) {
return []byte{}, nil
}
func (o *timeOpts) httpMethod() string {
return "GET"
}
func (o *timeOpts) isAuthRequired() bool {
return false
}
func (o *timeOpts) requestTimeout() int {
return o.pubnub.Config.NonSubscribeRequestTimeout
}
func (o *timeOpts) connectTimeout() int {
return o.pubnub.Config.ConnectTimeout
}
func (o *timeOpts) operationType() OperationType {
return PNTimeOperation
}
func (o *timeOpts) telemetryManager() *TelemetryManager {
return o.pubnub.telemetryManager
}
// TimeResponse is the response when Time call is executed.
type TimeResponse struct {
Timetoken int64
}
func newTimeResponse(jsonBytes []byte, status StatusResponse) (*TimeResponse, StatusResponse, error) {
resp := &TimeResponse{}
var value interface{}
err := json.Unmarshal(jsonBytes, &value)
if err != nil {
e := pnerr.NewResponseParsingError("Error unmarshalling response",
ioutil.NopCloser(bytes.NewBufferString(string(jsonBytes))), err)
return emptyTimeResp, status, e
}
if parsedValue, ok := value.([]interface{}); ok {
if tt, ok := parsedValue[0].(float64); ok {
resp.Timetoken = int64(tt)
}
}
return resp, status, nil
}