-
Notifications
You must be signed in to change notification settings - Fork 14
/
dynamic_link.go
123 lines (102 loc) · 3.57 KB
/
dynamic_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
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
package firebase
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
dynamiclink "github.com/coretrix/hitrix/service/component/dynamic_link"
)
const (
dynamicLinkGenerateURL = "https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key="
suffixOptionShort = "SHORT"
suffixOptionUnguessable = "UNGUESSABLE"
)
var ValidSuffixOptionsMap = map[string]*struct{}{
suffixOptionShort: {},
suffixOptionUnguessable: {},
}
type Generator struct {
ApiKey string `json:"-"`
DynamicLinkInfo *DynamicLinkInfo `json:"dynamicLinkInfo"`
Suffix *Suffix `json:"suffix,omitempty"`
}
type DynamicLinkInfo struct {
DomainUriPrefix string `json:"domainUriPrefix"`
Link string `json:"link"`
AndroidInfo *AndroidInfo `json:"androidInfo,omitempty"`
IosInfo *IosInfo `json:"iosInfo,omitempty"`
NavigationInfo *NavigationInfo `json:"navigationInfo,omitempty"`
AnalyticsInfo *AnalyticsInfo `json:"analyticsInfo,omitempty"`
SocialMetaTagInfo *SocialMetaTagInfo `json:"socialMetaTagInfo,omitempty"`
}
type AndroidInfo struct {
AndroidPackageName string `json:"androidPackageName,omitempty"`
AndroidFallbackLink string `json:"androidFallbackLink,omitempty"`
AndroidMinPackageVersionCode string `json:"androidMinPackageVersionCode,omitempty"`
}
type IosInfo struct {
IosBundleID string `json:"iosBundleId,omitempty"`
IosFallbackLink string `json:"iosFallbackLink,omitempty"`
IosCustomScheme string `json:"iosCustomScheme,omitempty"`
IosIpadFallbackLink string `json:"iosIpadFallbackLink,omitempty"`
IosIpadBundleID string `json:"iosIpadBundleId,omitempty"`
IosAppStoreID string `json:"iosAppStoreId,omitempty"`
}
type NavigationInfo struct {
EnableForcedRedirect bool `json:"enableForcedRedirect"`
}
type AnalyticsInfo struct {
GooglePlayAnalytics *GooglePlayAnalytics `json:"googlePlayAnalytics"`
ItunesConnectAnalytics *ItunesConnectAnalytics `json:"itunesConnectAnalytics"`
}
type GooglePlayAnalytics struct {
UtmSource string `json:"utmSource"`
UtmMedium string `json:"utmMedium"`
UtmCampaign string `json:"utmCampaign"`
UtmTerm string `json:"utmTerm"`
UtmContent string `json:"utmContent"`
GclID string `json:"gclid"`
}
type ItunesConnectAnalytics struct {
At string `json:"at"`
Ct string `json:"ct"`
Mt string `json:"mt"`
Pt string `json:"pt"`
}
type SocialMetaTagInfo struct {
SocialTitle string `json:"socialTitle"`
SocialDescription string `json:"socialDescription"`
SocialImageLink string `json:"socialImageLink"`
}
type Suffix struct {
Option string `json:"option"`
}
// GenerateDynamicLink hash must be URL safe
func (g *Generator) GenerateDynamicLink(hash string) (*dynamiclink.GenerateResponse, error) {
g.DynamicLinkInfo.Link += fmt.Sprintf("?hash=%s", hash)
marshaled, err := json.Marshal(g)
if err != nil {
return &dynamiclink.GenerateResponse{}, err
}
respRaw, err := http.Post(fmt.Sprintf("%s%s", dynamicLinkGenerateURL, g.ApiKey), "application/json", bytes.NewReader(marshaled))
if err != nil {
return &dynamiclink.GenerateResponse{}, err
}
defer func() {
if err := respRaw.Body.Close(); err != nil {
panic(err)
}
}()
resp := &response{}
if err := json.NewDecoder(respRaw.Body).Decode(resp); err != nil {
return &dynamiclink.GenerateResponse{}, err
}
return &dynamiclink.GenerateResponse{
Link: resp.ShortLink,
PreviewLink: resp.PreviewLink,
}, nil
}
type response struct {
ShortLink string `json:"shortLink"`
PreviewLink string `json:"previewLink"`
}