-
Notifications
You must be signed in to change notification settings - Fork 0
/
woo.go
58 lines (50 loc) · 1.32 KB
/
woo.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
package servicewoo
import (
"context"
"fmt"
"strings"
"github.com/nam-truong-le/lambda-utils-go/v3/pkg/aws/ssm"
"github.com/nam-truong-le/lambda-utils-go/v3/pkg/logger"
)
type Woo struct {
URL string
Key string
Secret string
}
func NewWoo(ctx context.Context) (*Woo, error) {
log := logger.FromContext(ctx)
log.Infof("read woo information")
shopUrl, err := ssm.GetParameter(ctx, "/shop/url", false)
if err != nil {
return nil, err
}
wooKey, err := ssm.GetParameter(ctx, "/shop/woo/key", false)
if err != nil {
return nil, err
}
wooSecret, err := ssm.GetParameter(ctx, "/shop/woo/secret", true)
if err != nil {
return nil, err
}
return &Woo{shopUrl, wooKey, wooSecret}, nil
}
func (w *Woo) NewURL(ctx context.Context, url string) string {
return w.newURL(ctx, url, "/wp-json/wc/v3")
}
func (w *Woo) NewURLAsiaLoop(ctx context.Context, url string) string {
return w.newURL(ctx, url, "/wp-json/asialoop-api")
}
func (w *Woo) newURL(ctx context.Context, url string, api string) string {
log := logger.FromContext(ctx)
log.Infof("prepare woo url: %s", url)
connector := "?"
if strings.Contains(url, "?") {
connector = "&"
}
result := fmt.Sprintf(
"%s%s%s%sconsumer_key=%s&consumer_secret=%s",
w.URL, api, url, connector, w.Key, w.Secret,
)
log.Infof("final woo url: %s", result)
return result
}