v3/translation_service_client.js

"use strict";
// Copyright 2019 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
//
//     https://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.
//
// ** This file is automatically generated by gapic-generator-typescript. **
// ** https://github.com/googleapis/gapic-generator-typescript **
// ** All changes to this file may be overwritten. **
Object.defineProperty(exports, "__esModule", { value: true });
const gax = require("google-gax");
const path = require("path");
const gapicConfig = require("./translation_service_client_config.json");
const version = require('../../../package.json').version;
class TranslationServiceClient {
    /**
     * Construct an instance of TranslationServiceClient.
     *
     * @@param {object} [options] - The configuration object. See the subsequent
     *   parameters for more details.
     * @@param {object} [options.credentials] - Credentials object.
     * @@param {string} [options.credentials.client_email]
     * @@param {string} [options.credentials.private_key]
     * @@param {string} [options.email] - Account email address. Required when
     *     using a .pem or .p12 keyFilename.
     * @@param {string} [options.keyFilename] - Full path to the a .json, .pem, or
     *     .p12 key downloaded from the Google Developers Console. If you provide
     *     a path to a JSON file, the projectId option below is not necessary.
     *     NOTE: .pem and .p12 require you to specify options.email as well.
     * @@param {number} [options.port] - The port on which to connect to
     *     the remote host.
     * @@param {string} [options.projectId] - The project ID from the Google
     *     Developer's Console, e.g. 'grape-spaceship-123'. We will also check
     *     the environment variable GCLOUD_PROJECT for your project ID. If your
     *     app is running in an environment which supports
     *     {@@link
     * https://developers.google.com/identity/protocols/application-default-credentials
     * Application Default Credentials}, your project ID will be detected
     * automatically.
     * @@param {function} [options.promise] - Custom promise module to use instead
     *     of native Promises.
     * @@param {string} [options.apiEndpoint] - The domain name of the
     *     API remote host.
     */
    constructor(opts) {
        /*
        Provides natural language translation operations.
        */
        this._descriptors = { page: {}, stream: {}, longrunning: {} };
        // Ensure that options include the service address and port.
        const staticMembers = this.constructor;
        const servicePath = opts && opts.servicePath ?
            opts.servicePath :
            ((opts && opts.apiEndpoint) ? opts.apiEndpoint :
                staticMembers.servicePath);
        const port = opts && opts.port ? opts.port : staticMembers.port;
        if (!opts) {
            opts = { servicePath, port };
        }
        opts.servicePath = opts.servicePath || servicePath;
        opts.port = opts.port || port;
        opts.clientConfig = opts.clientConfig || {};
        const isBrowser = (typeof window !== 'undefined');
        if (isBrowser) {
            opts.fallback = true;
        }
        // If we are in browser, we are already using fallback because of the
        // "browser" field in package.json.
        // But if we were explicitly requested to use fallback, let's do it now.
        const gaxModule = !isBrowser && opts.fallback ? gax.fallback : gax;
        // Create a `gaxGrpc` object, with any grpc-specific options
        // sent to the client.
        opts.scopes = this.constructor.scopes;
        const gaxGrpc = new gaxModule.GrpcClient(opts);
        // Save the auth object to the client, for use by other methods.
        this.auth = gaxGrpc.auth;
        // Determine the client header string.
        const clientHeader = [
            `gl-node/${process.version}`, `grpc/${gaxGrpc.grpcVersion}`,
            `gax/${gaxModule.version}`, `gapic/${version}`,
            `gl-web/${gaxModule.version}`
        ];
        if (opts.libName && opts.libVersion) {
            clientHeader.push(`${opts.libName}/${opts.libVersion}`);
        }
        // Load the applicable protos.
        // For Node.js, pass the path to JSON proto file.
        // For browsers, pass the JSON content.
        const nodejsProtoPath = path.join(__dirname, '..', '..', 'protos', 'protos.json');
        const protos = gaxGrpc.loadProto(opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath);
        // Some of the methods on this service return "paged" results,
        // (e.g. 50 results at a time, with tokens to get subsequent
        // pages). Denote the keys used for pagination and results.
        this._descriptors.page = {
            listGlossaries: new gaxModule.PageDescriptor('pageToken', 'nextPageToken', 'glossaries')
        };
        // This API contains "long-running operations", which return a
        // an Operation object that allows for tracking of the operation,
        // rather than holding a request open.
        const protoFilesRoot = opts.fallback ?
            gaxModule.protobuf.Root.fromJSON(require('../../protos/protos.json')) :
            gaxModule.protobuf.loadSync(nodejsProtoPath);
        const operationsClient = gaxModule
            .lro({
            auth: this.auth,
            grpc: 'grpc' in gaxGrpc ? gaxGrpc.grpc : undefined
        })
            .operationsClient(opts);
        const batchTranslateTextResponse = protoFilesRoot.lookup('BatchTranslateResponse');
        const batchTranslateTextMetadata = protoFilesRoot.lookup('BatchTranslateMetadata');
        const createGlossaryResponse = protoFilesRoot.lookup('Glossary');
        const createGlossaryMetadata = protoFilesRoot.lookup('CreateGlossaryMetadata');
        const deleteGlossaryResponse = protoFilesRoot.lookup('DeleteGlossaryResponse');
        const deleteGlossaryMetadata = protoFilesRoot.lookup('DeleteGlossaryMetadata');
        this._descriptors.longrunning = {
            batchTranslateText: new gaxModule.LongrunningDescriptor(operationsClient, batchTranslateTextResponse.decode.bind(batchTranslateTextResponse), batchTranslateTextMetadata.decode.bind(batchTranslateTextMetadata)),
            createGlossary: new gaxModule.LongrunningDescriptor(operationsClient, createGlossaryResponse.decode.bind(createGlossaryResponse), createGlossaryMetadata.decode.bind(createGlossaryMetadata)),
            deleteGlossary: new gaxModule.LongrunningDescriptor(operationsClient, deleteGlossaryResponse.decode.bind(deleteGlossaryResponse), deleteGlossaryMetadata.decode.bind(deleteGlossaryMetadata))
        };
        // Put together the default options sent with requests.
        const defaults = gaxGrpc.constructSettings('google.cloud.translation.v3.TranslationService', gapicConfig, opts.clientConfig || {}, { 'x-goog-api-client': clientHeader.join(' ') });
        // Set up a dictionary of "inner API calls"; the core implementation
        // of calling the API is handled in `google-gax`, with this code
        // merely providing the destination and request information.
        this._innerApiCalls = {};
        // Put together the "service stub" for
        // google.showcase.v1alpha2.Echo.
        const translationServiceStub = gaxGrpc.createStub(opts.fallback ?
            // @ts-ignore Do not check types for loaded protos
            protos.lookupService('google.cloud.translation.v3.TranslationService') :
            // @ts-ignore Do not check types for loaded protos
            protos.google.cloud.translation.v3.TranslationService, opts);
        const translationServiceStubMethods = [
            'translateText', 'detectLanguage', 'getSupportedLanguages',
            'batchTranslateText', 'createGlossary', 'listGlossaries', 'getGlossary',
            'deleteGlossary'
        ];
        for (const methodName of translationServiceStubMethods) {
            const innerCallPromise = translationServiceStub.then((stub) => (...args) => {
                return stub[methodName].apply(stub, args);
            }, (err) => () => {
                throw err;
            });
            this._innerApiCalls[methodName] = gax.createApiCall(innerCallPromise, defaults[methodName], this._descriptors.page[methodName] ||
                this._descriptors.stream[methodName] ||
                this._descriptors.longrunning[methodName]);
        }
    }
    /**
     * The DNS address for this API service.
     */
    static get servicePath() {
        return 'translate.googleapis.com';
    }
    /**
     * The DNS address for this API service - same as servicePath(),
     * exists for compatibility reasons.
     */
    static get apiEndpoint() {
        return 'translate.googleapis.com';
    }
    /**
     * The port for this API service.
     */
    static get port() {
        return 443;
    }
    /**
     * The scopes needed to make gRPC calls for every method defined
     * in this service.
     */
    static get scopes() {
        return [
            'https://www.googleapis.com/auth/cloud-platform',
            'https://www.googleapis.com/auth/cloud-translation'
        ];
    }
    getProjectId(callback) {
        if (callback) {
            this.auth.getProjectId(callback);
            return;
        }
        return this.auth.getProjectId();
    }
    translateText(request, optionsOrCallback, callback) {
        request = request || {};
        let options = optionsOrCallback;
        if (typeof options === 'function' && callback === undefined) {
            callback = options;
            options = {};
        }
        options = options || {};
        return this._innerApiCalls.translateText(request, options, callback);
    }
    detectLanguage(request, optionsOrCallback, callback) {
        request = request || {};
        let options = optionsOrCallback;
        if (typeof options === 'function' && callback === undefined) {
            callback = options;
            options = {};
        }
        options = options || {};
        return this._innerApiCalls.detectLanguage(request, options, callback);
    }
    getSupportedLanguages(request, optionsOrCallback, callback) {
        request = request || {};
        let options = optionsOrCallback;
        if (typeof options === 'function' && callback === undefined) {
            callback = options;
            options = {};
        }
        options = options || {};
        return this._innerApiCalls.getSupportedLanguages(request, options, callback);
    }
    getGlossary(request, optionsOrCallback, callback) {
        request = request || {};
        let options = optionsOrCallback;
        if (typeof options === 'function' && callback === undefined) {
            callback = options;
            options = {};
        }
        options = options || {};
        return this._innerApiCalls.getGlossary(request, options, callback);
    }
    batchTranslateText(request, optionsOrCallback, callback) {
        request = request || {};
        let options = optionsOrCallback;
        if (typeof options === 'function' && callback === undefined) {
            callback = options;
            options = {};
        }
        options = options || {};
        return this._innerApiCalls.batchTranslateText(request, options, callback);
    }
    createGlossary(request, optionsOrCallback, callback) {
        request = request || {};
        let options = optionsOrCallback;
        if (typeof options === 'function' && callback === undefined) {
            callback = options;
            options = {};
        }
        options = options || {};
        return this._innerApiCalls.createGlossary(request, options, callback);
    }
    deleteGlossary(request, optionsOrCallback, callback) {
        request = request || {};
        let options = optionsOrCallback;
        if (typeof options === 'function' && callback === undefined) {
            callback = options;
            options = {};
        }
        options = options || {};
        return this._innerApiCalls.deleteGlossary(request, options, callback);
    }
    listGlossaries(request, optionsOrCallback, callback) {
        request = request || {};
        let options = optionsOrCallback;
        if (typeof options === 'function' && callback === undefined) {
            callback = options;
            options = {};
        }
        options = options || {};
        return this._innerApiCalls.listGlossaries(request, options, callback);
    }
}
exports.TranslationServiceClient = TranslationServiceClient;
//# sourceMappingURL=translation_service_client.js.map