Skip to content

Commit b91519c

Browse files
feat(bindings): Pass Metadata in grpc binding invocation (#348)
* Pass Metadata in grpc binding invocation Signed-off-by: Deepanshu Agarwal <[email protected]> * Add e2e test for output binding metadata Signed-off-by: Deepanshu Agarwal <[email protected]> * Comment out E2E test Signed-off-by: Deepanshu Agarwal <[email protected]> * Fix comment Signed-off-by: Deepanshu Agarwal <[email protected]> * Uncomment test Signed-off-by: Shubham Sharma <[email protected]> * Fix tests Signed-off-by: Shubham Sharma <[email protected]> * Lint it Signed-off-by: Shubham Sharma <[email protected]> * Remove underscore Signed-off-by: Shubham Sharma <[email protected]> * 💄 Signed-off-by: Shubham Sharma <[email protected]> Signed-off-by: Deepanshu Agarwal <[email protected]> Signed-off-by: Shubham Sharma <[email protected]> Co-authored-by: Shubham Sharma <[email protected]>
1 parent 4c56ed0 commit b91519c

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

src/implementation/Client/GRPCClient/binding.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import GRPCClient from "./GRPCClient";
1515
import { InvokeBindingRequest, InvokeBindingResponse } from "../../../proto/dapr/proto/runtime/v1/dapr_pb";
1616
import IClientBinding from "../../../interfaces/Client/IClientBinding";
1717
import * as SerializerUtil from "../../../utils/Serializer.util";
18+
import { createGRPCMetadata } from "../../../utils/Client.util";
1819

1920
// https://docs.dapr.io/reference/api/bindings_api/
2021
export default class GRPCClientBinding implements IClientBinding {
@@ -27,16 +28,17 @@ export default class GRPCClientBinding implements IClientBinding {
2728
// Send an event to an external system
2829
// @todo: should pass the metadata object
2930
// @todo: should return a specific typed Promise<TypeBindingResponse> instead of Promise<object>
30-
async send(bindingName: string, operation: string, data: any, _metadata: object = {}): Promise<object> {
31+
async send(bindingName: string, operation: string, data: any, metadata: object = {}): Promise<object> {
3132
const msgService = new InvokeBindingRequest();
3233
msgService.setName(bindingName);
3334
msgService.setOperation(operation);
3435
msgService.setData(SerializerUtil.serializeGrpc(data).serializedData);
3536

37+
const grpcMetadata = createGRPCMetadata(metadata);
3638
const client = await this.client.getClient();
3739

3840
return new Promise((resolve, reject) => {
39-
client.invokeBinding(msgService, (err, res: InvokeBindingResponse) => {
41+
client.invokeBinding(msgService, grpcMetadata, (err, res: InvokeBindingResponse) => {
4042
if (err) {
4143
return reject(err);
4244
}

test/components/binding-redis.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: dapr.io/v1alpha1
2+
kind: Component
3+
metadata:
4+
name: binding-redis
5+
spec:
6+
type: bindings.redis
7+
version: v1
8+
metadata:
9+
- name: redisHost
10+
value: localhost:6379
11+
- name: redisPassword
12+
value: ""

test/e2e/grpc/server.test.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ describe("grpc/server", () => {
117117
// @ts-ignore
118118
expect(mockBindingReceive.mock.calls[0][0]["hello"]).toEqual("world");
119119
});
120+
121+
it("should be able to send metadata to output binding successfully", async () => {
122+
await server.client.binding.send("binding-redis", "create", "helloMessage", { key: "helloKey" });
123+
124+
const res = await server.client.configuration.get("config-redis", ["helloKey"]);
125+
expect(res.items["helloKey"].value).toContain("helloMessage");
126+
});
120127
});
121128

122129
describe("pubsub", () => {
@@ -174,7 +181,7 @@ describe("grpc/server", () => {
174181
}
175182
});
176183

177-
// TODO: uncomment these tests once metadata over gRPC is supported. See https://github.com/dapr/dapr/issues/2860
184+
// TODO: uncomment these tests when https://github.com/dapr/dapr/issues/2860 is resolved.
178185
// it('should be able to send cloud event and receive raw payload', async () => {
179186
// const res = await server.client.pubsub.publish('pubsub-redis', 'test-topic-ce-raw', { hello: 'world-ce-raw' });
180187
// expect(res).toEqual(true);
@@ -185,7 +192,7 @@ describe("grpc/server", () => {
185192

186193
// // Also test for receiving data
187194
// // @ts-ignore
188-
// const rawData = mockPubSubSubscribeCloudEventRaw.mock.calls[0][0]['data_base64'];
195+
// const rawData = mockPubSub.mock.calls[0][0]['data_base64'];
189196
// const data = JSON.parse(Buffer.from(rawData, 'base64').toString());
190197
// // @ts-ignore
191198
// expect(data['data']['hello']).toEqual('world-ce-raw');
@@ -201,7 +208,7 @@ describe("grpc/server", () => {
201208

202209
// // Also test for receiving data
203210
// // @ts-ignore
204-
// const rawData = mockPubSubSubscribeRawRaw.mock.calls[0][0]['data_base64'];
211+
// const rawData = mockPubSub.mock.calls[0][0]['data_base64'];
205212
// const data = JSON.parse(Buffer.from(rawData, 'base64').toString());
206213
// // @ts-ignore
207214
// expect(data['hello']).toEqual('world-raw-raw');
@@ -217,7 +224,7 @@ describe("grpc/server", () => {
217224

218225
// // Also test for receiving data
219226
// // @ts-ignore
220-
// expect(mockPubSubSubscribeRawCloudEvent.mock.calls[0][0]['hello']).toEqual('world-raw-ce');
227+
// expect(mockPubSub.mock.calls[0][0]['hello']).toEqual('world-raw-ce');
221228
// })
222229

223230
it("should receive if it was successful or not", async () => {

0 commit comments

Comments
 (0)