-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation_get_mail.go
76 lines (63 loc) · 1.71 KB
/
operation_get_mail.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
package googlemail
import (
"context"
"errors"
"google.golang.org/api/gmail/v1"
"google.golang.org/api/option"
"github.com/wakflo/go-sdk/autoform"
sdk "github.com/wakflo/go-sdk/connector"
sdkcore "github.com/wakflo/go-sdk/core"
)
type getMailByIDOperationProps struct {
MailID string `json:"id"`
}
type GetMailByIDOperation struct {
options *sdk.OperationInfo
}
func NewGetMailByIDOperation() *GetMailByIDOperation {
return &GetMailByIDOperation{
options: &sdk.OperationInfo{
Name: "Get Email",
Description: "Get an email from your Gmail account via Id",
RequireAuth: true,
Auth: sharedAuth,
Input: map[string]*sdkcore.AutoFormSchema{
"id": autoform.NewShortTextField().
SetDisplayName("Message ID").
SetDescription("The messageId of the mail to read").
SetRequired(true).
Build(),
},
ErrorSettings: sdkcore.StepErrorSettings{
ContinueOnError: false,
RetryOnError: false,
},
},
}
}
func (c *GetMailByIDOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) {
if ctx.Auth.Token == nil {
return nil, errors.New("missing google auth token")
}
input := sdk.InputToType[getMailByIDOperationProps](ctx)
gmailService, err := gmail.NewService(context.Background(), option.WithTokenSource(*ctx.Auth.TokenSource))
if err != nil {
return nil, err
}
if input.MailID == "" {
return nil, errors.New("mail ID is required")
}
mail, err := gmailService.Users.Messages.Get("me", input.MailID).
Format("full").
Do()
if err != nil {
return nil, err
}
return mail, nil
}
func (c *GetMailByIDOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) {
return c.Run(ctx)
}
func (c *GetMailByIDOperation) GetInfo() *sdk.OperationInfo {
return c.options
}