Home > Net >  best praticle to use the client from an google api
best praticle to use the client from an google api

Time:10-07

i'm using the google API, to be specific, the textToSpeech API. i made a function to do this conversion, in an separate archive. And this API require that i create an client.

import * as textToSpeech from '@google-cloud/text-to-speech'
const clientGoogle = new textToSpeech.TextToSpeechClient();

but now is my question: is better that i create this client outside the function or inside?

like this:

import * as textToSpeech from '@google-cloud/text-to-speech'

const clientGoogle = new textToSpeech.TextToSpeechClient();
const createAudio = async ()=>{
    const [response] = await clientGoogle.synthesizeSpeech(request);
}

or

import * as textToSpeech from '@google-cloud/text-to-speech'
    
const createAudio = async ()=>{
     const clientGoogle = new textToSpeech.TextToSpeechClient();
     const [response] = await clientGoogle.synthesizeSpeech(request);
}

what of this usage is better for my application?

CodePudding user response:

In general, you should create the client once in the global space. Then access the client member functions as required. Translation, use your first example.

However, I recommend referencing the package like this:

const textToSpeech = require('@google-cloud/text-to-speech');
  • Related