-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
63 lines (53 loc) · 1.16 KB
/
function.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
package airpin
import (
"context"
_ "embed"
"encoding/base64"
"encoding/json"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/caarlos0/env/v8"
"github.com/cloudevents/sdk-go/v2/event"
)
type Credentials struct {
AirtableToken string `env:"AIRTABLE_TOKEN,required"`
PinterestToken string `env:"PINTEREST_TOKEN,required"`
}
type data struct {
Message struct {
Data string
}
}
func init() {
functions.CloudEvent("airpin", airpin)
}
func airpin(ctx context.Context, e event.Event) error {
period := getPeriod(e)
creds := Credentials{}
if err := env.Parse(&creds); err != nil {
panic(err)
}
pin := NewPinterest(creds.PinterestToken, period)
air := NewAirtable(creds.AirtableToken, period)
for account_id := range bases {
air.Update(account_id, pin.Reports(account_id))
}
return nil
}
func getPeriod(e event.Event) Period {
var d data
json.Unmarshal(e.Data(), &d)
data, err := base64.StdEncoding.DecodeString(d.Message.Data)
if err != nil {
panic(err)
}
var period Period
switch string(data) {
case "7-day":
period = Week
case "month":
period = Month
default:
panic("invalid period")
}
return period
}