This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlink.go
66 lines (58 loc) · 1.51 KB
/
link.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
package chatbase
import (
"context"
"io"
)
var (
redirectURL = "https://chatbase.com/r"
clickEndpoint = "https://chatbase.com/api/click"
)
// Link describes a hyperlink to be tracked using Chatbase
type Link struct {
APIKey string `json:"api_key"`
URL string `json:"url"`
Platform string `json:"platform"`
Version string `json:"version,omitempty"`
}
// LinkResponse contains the response to submitting a link
type LinkResponse struct {
Status Status `json:"status"`
Reason string `json:"reason,omitempty"`
}
// SetVersion adds an optional "version" parameter to a link
func (l *Link) SetVersion(v string) *Link {
l.Version = v
return l
}
// Submit tries to send the link to Chatbase
func (l *Link) Submit() (*LinkResponse, error) {
return newLinkResponse(func() (io.ReadCloser, error) {
return apiPost(clickEndpoint, l)
})
}
// SubmitWithContext tries to send the link to Chatbase
// while considering the given context's deadline
func (l *Link) SubmitWithContext(ctx context.Context) (*LinkResponse, error) {
data, err := resultWithContext(ctx, func() (interface{}, error) {
return l.Submit()
})
if err != nil {
return nil, err
}
if res, ok := data.(*LinkResponse); ok {
return res, nil
}
return nil, errBadData
}
// Encode turns the link object into a URL
func (l *Link) Encode() (string, error) {
params := map[string]string{
"api_key": l.APIKey,
"url": l.URL,
"platform": l.Platform,
}
if l.Version != "" {
params["version"] = l.Version
}
return augmentURL(redirectURL, params)
}