import {GoogleAuth} from 'google-auth-library';
import {ProjectIdCallback} from 'google-auth-library/build/src/auth/googleauth';
import * as path from 'path';
import {GaxCall} from './apitypes';
import {createApiCall} from './createApiCall';
import {PageDescriptor} from './descriptor';
import * as gax from './gax';
import {ClientStubOptions, GrpcClient} from './grpc';
const configData = require('./operations_client_config');
export const SERVICE_ADDRESS = 'longrunning.googleapis.com';
const version = require('../../package.json').version;
const DEFAULT_SERVICE_PORT = 443;
const CODE_GEN_NAME_VERSION = 'gapic/0.7.1';
const PAGE_DESCRIPTORS = {
listOperations: new PageDescriptor(
'pageToken',
'nextPageToken',
'operations'
),
};
export const ALL_SCOPES: string[] = [];
export interface OperationsClientOptions {
libName?: string;
libVersion?: string;
clientConfig: gax.ClientConfig;
}
export class OperationsClient {
auth: GoogleAuth;
private _getOperation!: GaxCall;
private _listOperations!: GaxCall;
private _cancelOperation!: GaxCall;
private _deleteOperation!: GaxCall;
constructor(
gaxGrpc: GrpcClient,
grpcClients: any,
options: OperationsClientOptions
) {
const opts: OperationsClientOptions & ClientStubOptions = Object.assign(
{
servicePath: SERVICE_ADDRESS,
port: DEFAULT_SERVICE_PORT,
clientConfig: {},
},
options
);
const googleApiClient = ['gl-node/' + process.versions.node];
if (opts.libName && opts.libVersion) {
googleApiClient.push(opts.libName + '/' + opts.libVersion);
}
googleApiClient.push(
CODE_GEN_NAME_VERSION,
'gax/' + version,
'grpc/' + gaxGrpc.grpcVersion
);
const defaults = gaxGrpc.constructSettings(
'google.longrunning.Operations',
configData,
opts.clientConfig,
{'x-goog-api-client': googleApiClient.join(' ')}
);
this.auth = gaxGrpc.auth;
const operationsStub = gaxGrpc.createStub(
grpcClients.google.longrunning.Operations,
opts
);
const operationsStubMethods = [
'getOperation',
'listOperations',
'cancelOperation',
'deleteOperation',
];
operationsStubMethods.forEach(methodName => {
this['_' + methodName] = createApiCall(
operationsStub.then(operationsStub => {
return (...args: Array<{}>) => {
return operationsStub[methodName].apply(operationsStub, args);
};
}),
defaults[methodName],
PAGE_DESCRIPTORS[methodName]
);
});
}
getProjectId(): Promise<string>;
getProjectId(callback: ProjectIdCallback): void;
getProjectId(callback?: ProjectIdCallback): void | Promise<string> {
return this.auth.getProjectId(callback!);
}
getOperation(request: {}, options: {}, callback?) {
if (options instanceof Function && callback === undefined) {
callback = options;
options = {};
}
options = options || {};
return this._getOperation(request, options, callback);
}
listOperations(request, options, callback) {
if (options instanceof Function && callback === undefined) {
callback = options;
options = {};
}
options = options || {};
return this['_listOperations'](request, options, callback);
}
listOperationsStream(request: {}, options: gax.CallSettings) {
return PAGE_DESCRIPTORS.listOperations.createStream(
this._listOperations,
request,
options
);
}
cancelOperation(request, options?, callback?) {
if (options instanceof Function && callback === undefined) {
callback = options;
options = {};
}
options = options || {};
return this._cancelOperation(request, options, callback);
}
deleteOperation(request, options, callback) {
if (options instanceof Function && callback === undefined) {
callback = options;
options = {};
}
options = options || {};
return this._deleteOperation(request, options, callback);
}
}
export class OperationsClientBuilder {
operationsClient: (opts: OperationsClientOptions) => OperationsClient;
constructor(gaxGrpc: GrpcClient) {
const protoFilesRoot = path.join(__dirname, '..', '..');
const operationsClient: any = gaxGrpc.loadProto(
protoFilesRoot,
'google/longrunning/operations.proto'
);
Object.assign(this, operationsClient.google.longrunning);
this.operationsClient = opts => {
return new OperationsClient(gaxGrpc, operationsClient, opts);
};
Object.assign(this.operationsClient, OperationsClient);
}
}