CloudFunctionClient

Abstract client that calls Google Functions.

Implements: IOpenable, IConfigurable, IReferenceable

Description

When making calls “cmd” parameter determines which what action shall be called, while other parameters are passed to the action itself.

Configuration parameters

  • connections:
    • uri: full connection uri with specific app and function name
    • protocol: connection protocol
    • project_id: is your Google Cloud Platform project ID
    • region: is the region where your function is deployed
    • function_name: is the name of the HTTP function you deployed
  • options:
    • retries: number of retries (default: 3)
    • connect_timeout: connection timeout in milliseconds (default: 10 sec)
    • timeout: invocation timeout in milliseconds (default: 10 sec)
  • credentials:
    • auth_token: Google-generated ID token, if use custom authorization provide empty string

References

  • *:logger:*:*:1.0 - (optional) ILogger components to pass log messages.
  • *:counters:*:*:1.0 - (optional) ICounters components to pass collected measurements.
  • *:discovery:*:*:1.0 - (optional) IDiscovery services to resolve connections.
  • *:credential-store:*:*:1.0 - (optional) Credential stores to resolve credentials.

Fields

_client

The HTTP client.

_client: any

_connectTimeout

The connection timeout in milliseconds.

_connectTimeout: number = 10000

_connection

The Google Function connection parameters

_connection: GcpConnectionParams

_connectionResolver

The connection resolver.

_connectionResolver: GcpConnectionResolver

_counters

Performance counters.

_counters: CompositeCounters

_dependencyResolver

Dependencies resolver.

_dependencyResolver: DependencyResolver

_headers

The default headers to be added to every request.

_headers: any = {}

_logger

Logger.

_logger: CompositeLogger

_timeout

The invocation timeout in milliseconds.

_timeout: number = 10000

_tracer

The tracer.

_tracer: CompositeTracer

_uri

The remote service uri which is calculated on open.

_uri: string

Instance methods

call

Calls a Google Function action.

protected call<T>(cmd: string, context: IContext, params: any = {}): Promise<T>

  • cmd: string - an action name to be called.
  • context: IContext - (optional) a context to trace execution through a call chain.
  • params: any - (optional) action parameters.
  • returns: Promise<T> - action result.

close

Closes component and frees used resources.

public close(context: IContext): Promise<void>

  • context: IContext - (optional) a context to trace execution through a call chain.

configure

Configures component by passing configuration parameters.

public configure(config: ConfigParams): void

  • config: ConfigParams - configuration parameters to be set.

instrument

Adds instrumentation to log calls and measure call time. It returns a CounterTiming object that is used to end the time measurement.

protected instrument(context: IContext, name: string): InstrumentTiming

  • context: IContext - (optional) a context to trace execution through a call chain.
  • name: string - a method name.
  • return: InstrumentTiming - object to end the time measurement.

invoke

Performs Google Function invocation.

protected invoke<T>(cmd: string, context: IContext, args: any): Promise<T>

  • cmd: string - an action name to be called.
  • context: IContext - (optional) a context to trace execution through a call chain.
  • args: any - action arguments
  • returns: Promise<T> - action result.

isOpen

Checks if the component is opened.

public isOpen(): boolean

  • returns: boolean - true if the component has been opened and false otherwise.

open

Opens the component.

public open(context: IContext): Promise<void>

  • context: IContext - (optional) a context to trace execution through a call chain.

setReferences

Sets references to dependent components.

public setReferences(references: IReferences): void

  • references: IReferences - references to locate the component dependencies.

Examples

class MyCloudFunctionClient extends CloudFunctionClient implements IMyClient {
    ...
 
    public async getData(context: IContext, id: string): Promise<MyData> {
        let timing = this.instrument(context, 'myclient.get_data');
        const result = await this.call("get_data" context, { id: id });
        timing.endTiming();
        return result;
    }
    ...
}

let client = new MyCloudFunctionClient();

client.configure(ConfigParams.fromTuples(
    'connection.uri", "http://region-id.cloudfunctions.net/myfunction',
    'connection.protocol', 'http',
    'connection.region', 'region',
    'connection.function_name', 'myfunction',
    'credential.project_id', 'id',
    'credential.auth_token', 'XXX',
));
  
const result = await client.getData("123", "1");

See also