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
_disablePrivate
Disables 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
_initializePrivate
Initializes 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'.
Optional
opts: 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.
Optional
opts: 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.
Private
initializeInitializes 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
CloudRunAuthClass
for managing authentication and initialization of Google Cloud services, including Translation API and Vertex AI.Overview
The
CloudRunAuthClass
simplifies 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_CREDENTIALS
environment 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_KEY
environment variable to your API key.setApiKey()
to provide the API key.getGoogleApi()
to initialize the Google API clients.Class: CloudRunAuthClass
Constructor
constructor()
: Initializes theCloudRunAuthClass
with 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
: Returnstrue
if the Translation API client is ready;false
otherwise.isVertexReady
: Returnstrue
if the Vertex AI client is ready;false
otherwise.