17
17
// ** All changes to this file may be overwritten. **
18
18
19
19
/* global window */
20
- import * as gax from 'google-gax' ;
21
- import {
20
+ import type * as gax from 'google-gax' ;
21
+ import type {
22
22
Callback ,
23
23
CallOptions ,
24
24
Descriptors ,
@@ -30,7 +30,6 @@ import {
30
30
LocationsClient ,
31
31
LocationProtos ,
32
32
} from 'google-gax' ;
33
-
34
33
import { Transform } from 'stream' ;
35
34
import * as protos from '../../protos/protos' ;
36
35
import jsonProtos = require( '../../protos/protos.json' ) ;
@@ -40,7 +39,6 @@ import jsonProtos = require('../../protos/protos.json');
40
39
* This file defines retry strategy and timeouts for all API methods in this library.
41
40
*/
42
41
import * as gapicConfig from './agents_client_config.json' ;
43
- import { operationsProtos } from 'google-gax' ;
44
42
const version = require ( '../../../package.json' ) . version ;
45
43
46
44
/**
@@ -102,8 +100,18 @@ export class AgentsClient {
102
100
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
103
101
* For more information, please check the
104
102
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
105
- */
106
- constructor ( opts ?: ClientOptions ) {
103
+ * @param {gax } [gaxInstance]: loaded instance of `google-gax`. Useful if you
104
+ * need to avoid loading the default gRPC version and want to use the fallback
105
+ * HTTP implementation. Load only fallback version and pass it to the constructor:
106
+ * ```
107
+ * const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
108
+ * const client = new AgentsClient({fallback: 'rest'}, gax);
109
+ * ```
110
+ */
111
+ constructor (
112
+ opts ?: ClientOptions ,
113
+ gaxInstance ?: typeof gax | typeof gax . fallback
114
+ ) {
107
115
// Ensure that options include all the required fields.
108
116
const staticMembers = this . constructor as typeof AgentsClient ;
109
117
const servicePath =
@@ -123,8 +131,13 @@ export class AgentsClient {
123
131
opts [ 'scopes' ] = staticMembers . scopes ;
124
132
}
125
133
134
+ // Load google-gax module synchronously if needed
135
+ if ( ! gaxInstance ) {
136
+ gaxInstance = require ( 'google-gax' ) as typeof gax ;
137
+ }
138
+
126
139
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
127
- this . _gaxModule = opts . fallback ? gax . fallback : gax ;
140
+ this . _gaxModule = opts . fallback ? gaxInstance . fallback : gaxInstance ;
128
141
129
142
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
130
143
this . _gaxGrpc = new this . _gaxModule . GrpcClient ( opts ) ;
@@ -145,7 +158,10 @@ export class AgentsClient {
145
158
if ( servicePath === staticMembers . servicePath ) {
146
159
this . auth . defaultScopes = staticMembers . scopes ;
147
160
}
148
- this . locationsClient = new LocationsClient ( this . _gaxGrpc , opts ) ;
161
+ this . locationsClient = new this . _gaxModule . LocationsClient (
162
+ this . _gaxGrpc ,
163
+ opts
164
+ ) ;
149
165
150
166
// Determine the client header string.
151
167
const clientHeader = [ `gax/${ this . _gaxModule . version } ` , `gapic/${ version } ` ] ;
@@ -335,7 +351,7 @@ export class AgentsClient {
335
351
this . innerApiCalls = { } ;
336
352
337
353
// Add a warn function to the client constructor so it can be easily tested.
338
- this . warn = gax . warn ;
354
+ this . warn = this . _gaxModule . warn ;
339
355
}
340
356
341
357
/**
@@ -549,7 +565,7 @@ export class AgentsClient {
549
565
options . otherArgs = options . otherArgs || { } ;
550
566
options . otherArgs . headers = options . otherArgs . headers || { } ;
551
567
options . otherArgs . headers [ 'x-goog-request-params' ] =
552
- gax . routingHeader . fromParams ( {
568
+ this . _gaxModule . routingHeader . fromParams ( {
553
569
name : request . name || '' ,
554
570
} ) ;
555
571
this . initialize ( ) ;
@@ -647,7 +663,7 @@ export class AgentsClient {
647
663
options . otherArgs = options . otherArgs || { } ;
648
664
options . otherArgs . headers = options . otherArgs . headers || { } ;
649
665
options . otherArgs . headers [ 'x-goog-request-params' ] =
650
- gax . routingHeader . fromParams ( {
666
+ this . _gaxModule . routingHeader . fromParams ( {
651
667
parent : request . parent || '' ,
652
668
} ) ;
653
669
this . initialize ( ) ;
@@ -745,7 +761,7 @@ export class AgentsClient {
745
761
options . otherArgs = options . otherArgs || { } ;
746
762
options . otherArgs . headers = options . otherArgs . headers || { } ;
747
763
options . otherArgs . headers [ 'x-goog-request-params' ] =
748
- gax . routingHeader . fromParams ( {
764
+ this . _gaxModule . routingHeader . fromParams ( {
749
765
'agent.name' : request . agent ! . name || '' ,
750
766
} ) ;
751
767
this . initialize ( ) ;
@@ -837,7 +853,7 @@ export class AgentsClient {
837
853
options . otherArgs = options . otherArgs || { } ;
838
854
options . otherArgs . headers = options . otherArgs . headers || { } ;
839
855
options . otherArgs . headers [ 'x-goog-request-params' ] =
840
- gax . routingHeader . fromParams ( {
856
+ this . _gaxModule . routingHeader . fromParams ( {
841
857
name : request . name || '' ,
842
858
} ) ;
843
859
this . initialize ( ) ;
@@ -933,7 +949,7 @@ export class AgentsClient {
933
949
options . otherArgs = options . otherArgs || { } ;
934
950
options . otherArgs . headers = options . otherArgs . headers || { } ;
935
951
options . otherArgs . headers [ 'x-goog-request-params' ] =
936
- gax . routingHeader . fromParams ( {
952
+ this . _gaxModule . routingHeader . fromParams ( {
937
953
name : request . name || '' ,
938
954
} ) ;
939
955
this . initialize ( ) ;
@@ -1035,7 +1051,7 @@ export class AgentsClient {
1035
1051
options . otherArgs = options . otherArgs || { } ;
1036
1052
options . otherArgs . headers = options . otherArgs . headers || { } ;
1037
1053
options . otherArgs . headers [ 'x-goog-request-params' ] =
1038
- gax . routingHeader . fromParams ( {
1054
+ this . _gaxModule . routingHeader . fromParams ( {
1039
1055
name : request . name || '' ,
1040
1056
} ) ;
1041
1057
this . initialize ( ) ;
@@ -1169,7 +1185,7 @@ export class AgentsClient {
1169
1185
options . otherArgs = options . otherArgs || { } ;
1170
1186
options . otherArgs . headers = options . otherArgs . headers || { } ;
1171
1187
options . otherArgs . headers [ 'x-goog-request-params' ] =
1172
- gax . routingHeader . fromParams ( {
1188
+ this . _gaxModule . routingHeader . fromParams ( {
1173
1189
name : request . name || '' ,
1174
1190
} ) ;
1175
1191
this . initialize ( ) ;
@@ -1195,11 +1211,12 @@ export class AgentsClient {
1195
1211
protos . google . protobuf . Struct
1196
1212
>
1197
1213
> {
1198
- const request = new operationsProtos . google . longrunning . GetOperationRequest (
1199
- { name}
1200
- ) ;
1214
+ const request =
1215
+ new this . _gaxModule . operationsProtos . google . longrunning . GetOperationRequest (
1216
+ { name}
1217
+ ) ;
1201
1218
const [ operation ] = await this . operationsClient . getOperation ( request ) ;
1202
- const decodeOperation = new gax . Operation (
1219
+ const decodeOperation = new this . _gaxModule . Operation (
1203
1220
operation ,
1204
1221
this . descriptors . longrunning . exportAgent ,
1205
1222
this . _gaxModule . createDefaultBackoffSettings ( )
@@ -1337,7 +1354,7 @@ export class AgentsClient {
1337
1354
options . otherArgs = options . otherArgs || { } ;
1338
1355
options . otherArgs . headers = options . otherArgs . headers || { } ;
1339
1356
options . otherArgs . headers [ 'x-goog-request-params' ] =
1340
- gax . routingHeader . fromParams ( {
1357
+ this . _gaxModule . routingHeader . fromParams ( {
1341
1358
name : request . name || '' ,
1342
1359
} ) ;
1343
1360
this . initialize ( ) ;
@@ -1360,11 +1377,12 @@ export class AgentsClient {
1360
1377
) : Promise <
1361
1378
LROperation < protos . google . protobuf . Empty , protos . google . protobuf . Struct >
1362
1379
> {
1363
- const request = new operationsProtos . google . longrunning . GetOperationRequest (
1364
- { name}
1365
- ) ;
1380
+ const request =
1381
+ new this . _gaxModule . operationsProtos . google . longrunning . GetOperationRequest (
1382
+ { name}
1383
+ ) ;
1366
1384
const [ operation ] = await this . operationsClient . getOperation ( request ) ;
1367
- const decodeOperation = new gax . Operation (
1385
+ const decodeOperation = new this . _gaxModule . Operation (
1368
1386
operation ,
1369
1387
this . descriptors . longrunning . restoreAgent ,
1370
1388
this . _gaxModule . createDefaultBackoffSettings ( )
@@ -1468,7 +1486,7 @@ export class AgentsClient {
1468
1486
options . otherArgs = options . otherArgs || { } ;
1469
1487
options . otherArgs . headers = options . otherArgs . headers || { } ;
1470
1488
options . otherArgs . headers [ 'x-goog-request-params' ] =
1471
- gax . routingHeader . fromParams ( {
1489
+ this . _gaxModule . routingHeader . fromParams ( {
1472
1490
parent : request . parent || '' ,
1473
1491
} ) ;
1474
1492
this . initialize ( ) ;
@@ -1508,7 +1526,7 @@ export class AgentsClient {
1508
1526
options . otherArgs = options . otherArgs || { } ;
1509
1527
options . otherArgs . headers = options . otherArgs . headers || { } ;
1510
1528
options . otherArgs . headers [ 'x-goog-request-params' ] =
1511
- gax . routingHeader . fromParams ( {
1529
+ this . _gaxModule . routingHeader . fromParams ( {
1512
1530
parent : request . parent || '' ,
1513
1531
} ) ;
1514
1532
const defaultCallSettings = this . _defaults [ 'listAgents' ] ;
@@ -1557,7 +1575,7 @@ export class AgentsClient {
1557
1575
options . otherArgs = options . otherArgs || { } ;
1558
1576
options . otherArgs . headers = options . otherArgs . headers || { } ;
1559
1577
options . otherArgs . headers [ 'x-goog-request-params' ] =
1560
- gax . routingHeader . fromParams ( {
1578
+ this . _gaxModule . routingHeader . fromParams ( {
1561
1579
parent : request . parent || '' ,
1562
1580
} ) ;
1563
1581
const defaultCallSettings = this . _defaults [ 'listAgents' ] ;
0 commit comments