1 /// <reference types="node" />
2 import { EventEmitter } from 'events';
3 import { APICall, APICallback, CancellablePromise, NormalApiCaller, PromiseCanceller } from './apiCallable';
4 import { BackoffSettings, CallOptions } from './gax';
5 import { GoogleError } from './GoogleError';
6 import { Metadata } from './grpc';
7 import { OperationsClient } from './operationsClient';
9 * A callback to upack a google.protobuf.Any message.
10 * @callback anyDecoder
11 * @param {google.protobuf.Any} message - The message to unpacked.
12 * @return {Object} - The unpacked message.
14 export interface AnyDecoder {
15 (message: {}): Metadata;
18 * @callback GetOperationCallback
19 * @param {?Error} error
20 * @param {?Object} result
21 * @param {?Object} metadata
22 * @param {?google.longrunning.Operation} rawResponse
24 export interface GetOperationCallback {
25 (err?: Error | null, result?: {}, metadata?: {}, rawResponse?: Operation): void;
27 export declare class LongrunningDescriptor {
28 operationsClient: OperationsClient;
29 responseDecoder: AnyDecoder;
30 metadataDecoder: AnyDecoder;
32 * Describes the structure of a page-streaming call.
34 * @property {OperationsClient} operationsClient
35 * @property {anyDecoder} responseDecoder
36 * @property {anyDecoder} metadataDecoder
38 * @param {OperationsClient} operationsClient - The client used to poll or
39 * cancel an operation.
40 * @param {anyDecoder=} responseDecoder - The decoder to unpack
41 * the response message.
42 * @param {anyDecoder=} metadataDecoder - The decoder to unpack
43 * the metadata message.
47 constructor(operationsClient: OperationsClient, responseDecoder: AnyDecoder, metadataDecoder: AnyDecoder);
48 apiCaller(): LongrunningApiCaller;
50 export declare class LongrunningApiCaller extends NormalApiCaller {
51 longrunningDescriptor: LongrunningDescriptor;
53 * Creates an API caller that performs polling on a long running operation.
57 * @param {LongrunningDescriptor} longrunningDescriptor - Holds the
58 * decoders used for unpacking responses and the operationsClient
59 * used for polling the operation.
61 constructor(longrunningDescriptor: LongrunningDescriptor);
62 call(apiCall: APICall, argument: {}, settings: CallOptions, canceller: PromiseCanceller): void;
63 _wrapOperation(apiCall: APICall, settings: CallOptions, argument: {}, callback: APICallback): any;
65 export declare class Operation extends EventEmitter {
66 completeListeners: number;
67 hasActiveListeners: boolean;
68 latestResponse: Operation;
69 longrunningDescriptor: LongrunningDescriptor;
71 metadata: Metadata | null;
72 backoffSettings: BackoffSettings;
73 _callOptions?: CallOptions;
74 currentCallPromise_?: CancellablePromise;
82 * Wrapper for a google.longrunnung.Operation.
86 * @param {google.longrunning.Operation} grpcOp - The operation to be wrapped.
87 * @param {LongrunningDescriptor} longrunningDescriptor - This defines the
88 * operations service client and unpacking mechanisms for the operation.
89 * @param {BackoffSettings} backoffSettings - The backoff settings used in
90 * in polling the operation.
91 * @param {CallOptions=} callOptions - CallOptions used in making get operation
94 constructor(grpcOp: Operation, longrunningDescriptor: LongrunningDescriptor, backoffSettings: BackoffSettings, callOptions?: CallOptions);
96 * Begin listening for events on the operation. This method keeps track of how
97 * many "complete" listeners are registered and removed, making sure polling
98 * is handled automatically.
100 * As long as there is one active "complete" listener, the connection is open.
101 * When there are no more listeners, the polling stops.
105 _listenForEvents(): void;
107 * Cancels current polling api call and cancels the operation.
109 * @return {Promise} the promise of the OperationsClient#cancelOperation api
114 * Get the updated status of the operation. If the Operation has previously
115 * completed, this will use the status of the cached completed operation.
117 * - callback(err): Operation failed
118 * - callback(null, result, metadata, rawResponse): Operation complete
119 * - callback(null, null, metadata, rawResponse): Operation incomplete
121 * @param {getOperationCallback} callback - Callback to handle the polled
122 * operation result and metadata.
123 * @return {Promise|undefined} - This returns a promise if a callback is not specified.
124 * The promise resolves to an array where the first element is the unpacked
125 * result, the second element is the metadata, and the third element is the
126 * raw response of the api call. The promise rejects if the operation returns
129 getOperation(): Promise<{}>;
130 getOperation(callback: GetOperationCallback): void;
131 _unpackResponse(op: Operation, callback?: GetOperationCallback): void;
133 * Poll `getOperation` to check the operation's status. This runs a loop to
134 * ping using the backoff strategy specified at initialization.
136 * Note: This method is automatically called once a "complete" event handler
137 * is registered on the operation.
141 startPolling_(): void;
143 * Wraps the `complete` and `error` events in a Promise.
145 * @return {promise} - Promise that resolves on operation completion and rejects
146 * on operation error.
148 promise(): Promise<{}>;
151 * Method used to create Operation objects.
155 * @param {google.longrunning.Operation} op - The operation to be wrapped.
156 * @param {LongrunningDescriptor} longrunningDescriptor - This defines the
157 * operations service client and unpacking mechanisms for the operation.
158 * @param {BackoffSettings} backoffSettings - The backoff settings used in
159 * in polling the operation.
160 * @param {CallOptions=} callOptions - CallOptions used in making get operation
163 export declare function operation(op: Operation, longrunningDescriptor: LongrunningDescriptor, backoffSettings: BackoffSettings, callOptions?: CallOptions): Operation;