-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathclient.js
110 lines (98 loc) · 2.99 KB
/
client.js
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
// Copyright 2017 Google LLC
//
// 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.
'use strict';
// [START endpoints_make_grpc_request]
const makeGrpcRequest = (JWT_AUTH_TOKEN, API_KEY, HOST, GREETEE) => {
// Uncomment these lines to set their values
// const JWT_AUTH_TOKEN = 'YOUR_JWT_AUTH_TOKEN';
// const API_KEY = 'YOUR_API_KEY';
// const HOST = 'localhost:50051'; // The IP address of your endpoints host
// const GREETEE = 'world';
// Import required libraries
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const path = require('path');
// Load protobuf spec for an example API
const PROTO_PATH = path.join(__dirname, '/protos/helloworld.proto');
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
});
const protoObj = grpc.loadPackageDefinition(packageDefinition).helloworld;
// Create a client for the protobuf spec
const client = new protoObj.Greeter(HOST, grpc.credentials.createInsecure());
// Build gRPC request
const metadata = new grpc.Metadata();
if (API_KEY) {
metadata.add('x-api-key', API_KEY);
} else if (JWT_AUTH_TOKEN) {
metadata.add('authorization', `Bearer ${JWT_AUTH_TOKEN}`);
}
// Execute gRPC request
client.sayHello({name: GREETEE}, metadata, (err, response) => {
if (err) {
console.error(err);
}
if (response) {
console.log(response.message);
}
});
};
// [END endpoints_make_grpc_request]
// The command-line program
const {argv} = require('yargs')
.usage(
'Usage: node $0 {-k YOUR_API_KEY>, <-j YOUR_JWT_AUTH_TOKEN} [-h YOUR_ENDPOINTS_HOST] [-g GREETEE_NAME]'
)
.option('jwtAuthToken', {
alias: 'j',
type: 'string',
global: true,
default: '',
})
.option('apiKey', {
alias: 'k',
type: 'string',
global: true,
default: '',
})
.option('host', {
alias: 'h',
type: 'string',
default: 'localhost:50051',
global: true,
})
.option('greetee', {
alias: 'g',
type: 'string',
default: 'world',
global: true,
})
.check(argv => {
const valid = !!(argv.jwtAuthToken || argv.apiKey);
if (!valid) {
console.error('One of API_KEY or JWT_AUTH_TOKEN must be set.');
}
return valid;
})
.wrap(120)
.help()
.strict()
.epilogue(
'For more information, see https://cloud.google.com/endpoints/docs'
);
makeGrpcRequest(argv.jwtAuthToken, argv.apiKey, argv.host, argv.greetee);