forked from dapr/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoke.go
152 lines (133 loc) · 4.44 KB
/
invoke.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"context"
"encoding/json"
"strings"
anypb "github.com/golang/protobuf/ptypes/any"
"github.com/pkg/errors"
v1 "github.com/dapr/go-sdk/dapr/proto/common/v1"
pb "github.com/dapr/go-sdk/dapr/proto/runtime/v1"
)
// DataContent the service invocation content.
type DataContent struct {
// Data is the input data
Data []byte
// ContentType is the type of the data content
ContentType string
}
func (c *GRPCClient) invokeServiceWithRequest(ctx context.Context, req *pb.InvokeServiceRequest) (out []byte, err error) {
if req == nil {
return nil, errors.New("nil request")
}
resp, err := c.protoClient.InvokeService(c.withAuthToken(ctx), req)
if err != nil {
return nil, err
}
// allow for service to not return any value
if resp != nil && resp.GetData() != nil {
out = resp.GetData().Value
return
}
out = nil
return
}
func queryAndVerbToHTTPExtension(query string, verb string) *v1.HTTPExtension {
if v, ok := v1.HTTPExtension_Verb_value[strings.ToUpper(verb)]; ok {
return &v1.HTTPExtension{Verb: v1.HTTPExtension_Verb(v), Querystring: query}
}
return &v1.HTTPExtension{Verb: v1.HTTPExtension_NONE}
}
func hasRequiredInvokeArgs(appID, methodName, verb string) error {
if appID == "" {
return errors.New("appID")
}
if methodName == "" {
return errors.New("methodName")
}
if verb == "" {
return errors.New("verb")
}
return nil
}
// InvokeMethod invokes service without raw data ([]byte).
func (c *GRPCClient) InvokeMethod(ctx context.Context, appID, methodName, verb string) (out []byte, err error) {
if err := hasRequiredInvokeArgs(appID, methodName, verb); err != nil {
return nil, errors.Wrap(err, "missing required parameter")
}
method, query := extractMethodAndQuery(methodName)
req := &pb.InvokeServiceRequest{
Id: appID,
Message: &v1.InvokeRequest{
Method: method,
HttpExtension: queryAndVerbToHTTPExtension(query, verb),
},
}
return c.invokeServiceWithRequest(ctx, req)
}
// InvokeMethodWithContent invokes service with content (data + content type).
func (c *GRPCClient) InvokeMethodWithContent(ctx context.Context, appID, methodName, verb string, content *DataContent) (out []byte, err error) {
if err := hasRequiredInvokeArgs(appID, methodName, verb); err != nil {
return nil, errors.Wrap(err, "missing required parameter")
}
if content == nil {
return nil, errors.New("content required")
}
method, query := extractMethodAndQuery(methodName)
req := &pb.InvokeServiceRequest{
Id: appID,
Message: &v1.InvokeRequest{
Method: method,
Data: &anypb.Any{Value: content.Data},
ContentType: content.ContentType,
HttpExtension: queryAndVerbToHTTPExtension(query, verb),
},
}
return c.invokeServiceWithRequest(ctx, req)
}
// InvokeMethodWithCustomContent invokes service with custom content (struct + content type).
func (c *GRPCClient) InvokeMethodWithCustomContent(ctx context.Context, appID, methodName, verb string, contentType string, content interface{}) ([]byte, error) {
if err := hasRequiredInvokeArgs(appID, methodName, verb); err != nil {
return nil, errors.Wrap(err, "missing required parameter")
}
if contentType == "" {
return nil, errors.New("content type required")
}
if content == nil {
return nil, errors.New("content required")
}
contentData, err := json.Marshal(content)
if err != nil {
return nil, errors.WithMessage(err, "error serializing input struct")
}
method, query := extractMethodAndQuery(methodName)
req := &pb.InvokeServiceRequest{
Id: appID,
Message: &v1.InvokeRequest{
Method: method,
Data: &anypb.Any{Value: contentData},
ContentType: contentType,
HttpExtension: queryAndVerbToHTTPExtension(query, verb),
},
}
return c.invokeServiceWithRequest(ctx, req)
}
func extractMethodAndQuery(name string) (method, query string) {
splitStr := strings.SplitN(name, "?", 2)
method = splitStr[0]
if len(splitStr) == 2 {
query = splitStr[1]
}
return
}