-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation_get_thread.go
84 lines (71 loc) · 1.94 KB
/
operation_get_thread.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
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 getThreadOperationProps struct {
ThreadID string `json:"id"`
Format string `json:"format"`
}
type GetThreadOperation struct {
options *sdk.OperationInfo
}
func NewGetThreadOperation() *GetThreadOperation {
return &GetThreadOperation{
options: &sdk.OperationInfo{
Name: "Get Thread",
Description: "Get a thread from your Gmail account via Id",
RequireAuth: true,
Auth: sharedAuth,
Input: map[string]*sdkcore.AutoFormSchema{
"id": autoform.NewShortTextField().
SetDisplayName("Thread ID").
SetDescription("The thread Id of the mail to read").
SetRequired(true).
Build(),
"format": autoform.NewSelectField().
SetDisplayName("Format").
SetDescription("The format of the email to read").
SetOptions(viewMailFormat).
SetRequired(false).
Build(),
},
ErrorSettings: sdkcore.StepErrorSettings{
ContinueOnError: false,
RetryOnError: false,
},
},
}
}
func (c *GetThreadOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) {
if ctx.Auth.Token == nil {
return nil, errors.New("missing google auth token")
}
input := sdk.InputToType[getThreadOperationProps](ctx)
gmailService, err := gmail.NewService(context.Background(), option.WithTokenSource(*ctx.Auth.TokenSource))
if err != nil {
return nil, err
}
chosenFormat := input.Format
if chosenFormat == "" {
chosenFormat = "full"
}
mail, err := gmailService.Users.Messages.Get("me", input.ThreadID).
Format(chosenFormat).
Do()
if err != nil {
return nil, err
}
return mail, nil
}
func (c *GetThreadOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) {
return c.Run(ctx)
}
func (c *GetThreadOperation) GetInfo() *sdk.OperationInfo {
return c.options
}