forked from dapr/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binding.go
73 lines (62 loc) · 2.03 KB
/
binding.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
package client
import (
"context"
"github.com/pkg/errors"
pb "github.com/dapr/dapr/pkg/proto/runtime/v1"
)
// InvokeBindingRequest represents binding invocation request.
type InvokeBindingRequest struct {
// Name is name of binding to invoke.
Name string
// Operation is the name of the operation type for the binding to invoke
Operation string
// Data is the input bindings sent
Data []byte
// Metadata is the input binding metadata
Metadata map[string]string
}
// BindingEvent represents the binding event handler input.
type BindingEvent struct {
// Data is the input bindings sent
Data []byte
// Metadata is the input binding metadata
Metadata map[string]string
}
// InvokeBinding invokes specific operation on the configured Dapr binding.
// This method covers input, output, and bi-directional bindings.
func (c *GRPCClient) InvokeBinding(ctx context.Context, in *InvokeBindingRequest) (*BindingEvent, error) {
if in == nil {
return nil, errors.New("binding invocation required")
}
if in.Name == "" {
return nil, errors.New("binding invocation name required")
}
if in.Operation == "" {
return nil, errors.New("binding invocation operation required")
}
req := &pb.InvokeBindingRequest{
Name: in.Name,
Operation: in.Operation,
Data: in.Data,
Metadata: in.Metadata,
}
resp, err := c.protoClient.InvokeBinding(c.withAuthToken(ctx), req)
if err != nil {
return nil, errors.Wrapf(err, "error invoking binding %s/%s", in.Name, in.Operation)
}
if resp != nil {
return &BindingEvent{
Data: resp.Data,
Metadata: resp.Metadata,
}, nil
}
return nil, nil
}
// InvokeOutputBinding invokes configured Dapr binding with data (allows nil).InvokeOutputBinding
// This method differs from InvokeBinding in that it doesn't expect any content being returned from the invoked method.
func (c *GRPCClient) InvokeOutputBinding(ctx context.Context, in *InvokeBindingRequest) error {
if _, err := c.InvokeBinding(ctx, in); err != nil {
return errors.Wrap(err, "error invoking output binding")
}
return nil
}