src/longRunningCalls/longRunningApiCaller.ts
Properties |
Methods |
constructor(longrunningDescriptor: LongRunningDescriptor)
|
||||||||
Creates an API caller that performs polling on a long running operation.
Parameters :
|
longrunningDescriptor |
Type : LongRunningDescriptor
|
call | |||||||||||||||
call(apiCall: SimpleCallbackFunction, argument: literal type, settings: CallOptions, canceller: OngoingCallPromise)
|
|||||||||||||||
Parameters :
Returns :
void
|
fail | |||||||||
fail(canceller: OngoingCallPromise, err: GoogleError)
|
|||||||||
Parameters :
Returns :
void
|
init | |||||||||
init(settings: ApiCallerSettings, callback?: APICallback)
|
|||||||||
Parameters :
Returns :
OngoingCallPromise | OngoingCall
|
result | ||||||
result(canceller: OngoingCallPromise)
|
||||||
Parameters :
Returns :
any
|
wrap | ||||||
wrap(func: GRPCCall)
|
||||||
Parameters :
Returns :
GRPCCall
|
import {APICaller, ApiCallerSettings} from '../apiCaller';
import {APICallback, GRPCCall, SimpleCallbackFunction} from '../apitypes';
import {OngoingCall, OngoingCallPromise} from '../call';
import {
BackoffSettings,
CallOptions,
CallSettings,
createBackoffSettings,
createDefaultBackoffSettings,
} from '../gax';
import {GoogleError} from '../googleError';
import {Operation} from './longrunning';
import {LongRunningDescriptor} from './longRunningDescriptor';
export class LongrunningApiCaller implements APICaller {
longrunningDescriptor: LongRunningDescriptor;
/**
* Creates an API caller that performs polling on a long running operation.
*
* @private
* @constructor
* @param {LongRunningDescriptor} longrunningDescriptor - Holds the
* decoders used for unpacking responses and the operationsClient
* used for polling the operation.
*/
constructor(longrunningDescriptor: LongRunningDescriptor) {
this.longrunningDescriptor = longrunningDescriptor;
}
init(
settings: ApiCallerSettings,
callback?: APICallback
): OngoingCallPromise | OngoingCall {
if (callback) {
return new OngoingCall(callback);
}
return new OngoingCallPromise(settings.promise);
}
wrap(func: GRPCCall): GRPCCall {
return func;
}
call(
apiCall: SimpleCallbackFunction,
argument: {},
settings: CallOptions,
canceller: OngoingCallPromise
) {
canceller.call((argument, callback) => {
return this._wrapOperation(apiCall, settings, argument, callback);
}, argument);
}
private _wrapOperation(
apiCall: SimpleCallbackFunction,
settings: CallOptions,
argument: {},
callback: APICallback
) {
let backoffSettings: BackoffSettings | undefined = settings.longrunning;
if (!backoffSettings) {
backoffSettings = createDefaultBackoffSettings();
}
const longrunningDescriptor = this.longrunningDescriptor;
return apiCall(
argument,
(err: GoogleError | null, rawResponse: {} | null | undefined) => {
if (err) {
callback(err, null, null, rawResponse as Operation);
return;
}
const operation = new Operation(
rawResponse as Operation,
longrunningDescriptor,
backoffSettings!,
settings
);
callback(null, operation, rawResponse);
}
);
}
fail(canceller: OngoingCallPromise, err: GoogleError): void {
canceller.callback!(err);
}
result(canceller: OngoingCallPromise) {
return canceller.promise;
}
}