-
Notifications
You must be signed in to change notification settings - Fork 0
/
short_links.go
35 lines (30 loc) · 1015 Bytes
/
short_links.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
package firebasetools
import (
"context"
"fmt"
"github.com/savannahghi/serverutils"
"google.golang.org/api/firebasedynamiclinks/v1"
)
// ShortenLink shortens an FDL link
func ShortenLink(ctx context.Context, longLink string) (string, error) {
fdlService, err := firebasedynamiclinks.NewService(ctx)
if err != nil {
return "", fmt.Errorf("unable to initialize Firebase Dynamic Links service: %w", err)
}
dynamicLinkDomain, err := serverutils.GetEnvVar(FDLDomainEnvironmentVariableName)
if err != nil {
return "", fmt.Errorf("environment variable %s missing", FDLDomainEnvironmentVariableName)
}
linkRequest := &firebasedynamiclinks.CreateShortDynamicLinkRequest{
DynamicLinkInfo: &firebasedynamiclinks.DynamicLinkInfo{
DomainUriPrefix: dynamicLinkDomain,
Link: longLink,
},
}
linkReq := fdlService.ShortLinks.Create(linkRequest)
linkResp, err := linkReq.Do()
if err != nil {
return "", fmt.Errorf("unable to shorten link: %w", err)
}
return linkResp.ShortLink, nil
}