Initializes the CloudRunAuthClass.
Sets the initial values of the class properties to undefined, and sets the project ID to
'your_project_id'. It also attempts to set the API key and key file using the
setApiKey() and setkeyFile() methods.
Private_accessPrivate_apiPrivate_authPrivate_credentialsPrivate_isPrivate_isPrivate_keyRetrieves the readiness status of the Google Cloud Translation API client.
This getter method provides a way to check if the Translation API client has been
successfully initialized and is ready for use. It returns true if the client is
ready, and false otherwise.
true if the Translation API client is ready; false otherwise.
Retrieves the readiness status of the Vertex AI client.
This getter method allows you to check if the Vertex AI client has been successfully
initialized and is ready for use. It returns true if the client is ready, and
false otherwise.
true if the Vertex AI client is ready; false otherwise.
Private_disablePrivateDisables the Google Cloud Translation API and Vertex AI services.
This private method is used to disable both the Translation API and Vertex AI services
by setting their respective readiness flags (_isTranslateReady and _isVertexReady)
to false. This is typically done when an error occurs during the initialization
of these services or when they are no longer needed.
Private_getRetrieves an access token by prompting the user to authenticate via a Cloud Run service.
This method guides the user through the process of obtaining an access token by directing them to a specific URL for authentication. Upon successful authentication, the user is expected to provide the received access token, which is then used to initialize the Google API clients.
Private_initializePrivateInitializes the Google Cloud Translation API client.
This method creates a new instance of the TranslationServiceClient using the provided options or the
class's stored credentials, API key, or key file. It configures the client with the necessary
authentication details and sets the library version to 'v3'.
Optionalopts: GoogleKeyOptionsOptional configuration options for the Translation API client.
Private_initializeInitializes the Vertex AI client.
This method creates a new instance of the Vertex AI client using the provided options or the class's stored credentials, API key, or key file. It configures the client with the project ID, location, and API endpoint.
Optionalopts: GoogleKeyOptionsOptional configuration options for the Vertex AI client.
Private_initializeInitializes the Google API clients using an access token.
This method uses the provided access token to initialize the Google API clients (Translation and Vertex AI)
by creating a new instance of OAuth2Client and setting the credentials using the access token.
It then uses the obtained API key to initialize the Translation and Vertex AI clients.
If an error occurs during initialization, it disables the services and logs the error.
Private_initializeInitializes the Google API clients using an API key.
This method uses the API key to initialize the Google API clients (Translation and Vertex AI)
by creating a new instance of GoogleAuth. It sets the necessary scopes for cloud
translation and proceeds to initialize the Translation and Vertex AI clients.
If an error occurs during initialization, it disables the services.
Private_initializeInitializes the Google API clients using a service account.
This method uses the service account key file and credentials to initialize the Google API clients
(Translation and Vertex AI) by creating a new instance of GoogleAuth. It sets the necessary
scopes for cloud translation and proceeds to initialize the Translation and Vertex AI clients.
If an error occurs during initialization, it disables the services.
Private_isPrivate_isPrivate_showDetermines and sets the authentication method based on the provided credentials, key file, or access token.
This method analyzes the presence of an access token, service account key file, or API key to determine the
appropriate authentication method. It sets the _authMethod property accordingly and logs warnings if multiple
authentication methods are provided, prioritizing the access token, followed by the service account key.
PrivateinitializeInitializes the Google API clients based on the determined authentication method.
This method orchestrates the initialization of Google API clients (Translation and Vertex AI)
based on the authentication method determined by _showKeyInfo(). It handles three authentication
methods: access token, service account, and API key. If no authentication method is configured,
it attempts to use Cloud Run authentication.
CloudRunAuth This class provides the
CloudRunAuthClassfor managing authentication and initialization of Google Cloud services, including Translation API and Vertex AI.Overview
The
CloudRunAuthClasssimplifies the process of authenticating and interacting with Google Cloud services. It supports multiple authentication methods, including:The class automatically detects the available authentication method and initializes the necessary Google API clients accordingly. It also provides methods to check the readiness of the Translation API and Vertex AI clients.
Features
Usage
Installation
Basic Example
Authentication Methods
Access Token
getGoogleApi()to initiate the authentication process.Service Account
GOOGLE_APPLICATION_CREDENTIALSenvironment variable to the path of your service account key file.setkeyFile()orsetCredentials()to provide the key file path or credentials.getGoogleApi()to initialize the Google API clients.API Key
GOOGLE_API_KEYenvironment variable to your API key.setApiKey()to provide the API key.getGoogleApi()to initialize the Google API clients.Class: CloudRunAuthClass
Constructor
constructor(): Initializes theCloudRunAuthClasswith default values and attempts to set the API key and key file from environment variables.Methods
getGoogleApi(): Initializes the Google API clients based on the determined authentication method.displayAuthUrl(): Displays the URL for Google authentication via a Cloud Run service.setApiKey(apiKey?: string): Sets the API key for Google API authentication.setCredentials(credentials?: any): Sets the service account credentials for Google API authentication.setkeyFile(keyFile?: string): Sets the path to the service account key file for Google API authentication.setAccessToken(accessToken: string): Sets the access token for Cloud Run authentication.Getters
isTranslateReady: Returnstrueif the Translation API client is ready;falseotherwise.isVertexReady: Returnstrueif the Vertex AI client is ready;falseotherwise.