/
pubnub_request.go
59 lines (51 loc) · 1.27 KB
/
pubnub_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
package gopu
import (
"net/url"
"encoding/json"
)
type PubnubRequest struct {
operation string
ssl bool
url string
Channel string
Message interface {}
Timetoken string
UUID string
}
func NewPubnubRequest(operation string) (*PubnubRequest) {
req := new(PubnubRequest)
req.operation = operation
req.ssl = false
return req
}
func (req *PubnubRequest) Url(publishKey string, subscribeKey string, secretKey string) (string) {
if req.url != "" {
return req.url
}
var Url *url.URL
if req.ssl {
Url, _ = url.Parse("https://" + OriginHost)
} else {
Url, _ = url.Parse("http://" + OriginHost)
}
switch req.operation {
case "time":
Url.Path += "/" + req.operation + "/0"
case "here_now":
Url.Path += "/v2/presence/sub-key/" + subscribeKey + "/channel/" + req.Channel
case "publish":
messageBytes, err := json.Marshal(req.Message)
if err != nil {
panic(err)
}
Url.Path += "/" + req.operation + "/" + publishKey + "/" + subscribeKey + "/" + secretKey + "/" + req.Channel + "/0/" + string(messageBytes)
case "subscribe":
timetoken := "0"
if req.Timetoken != "" {
timetoken = req.Timetoken
}
Url.Path += "/" + req.operation + "/" + subscribeKey + "/" + req.Channel + "/0/" + timetoken + "?uuid=" + req.UUID
}
req.url = Url.String()
return req.url
}