Implements: ICloudFunctionService, IOpenable, IConfigurable, IReferenceable
Description
The CloudFunctionService class allows you to create a service that receives remove calls via the Google Function protocol.
Important points
- This service is intended to work inside an CloudFunction container that exposes registered actions externally.
Configuration parameters
- dependencies:
- controller: override for Controller dependency
References
- *:logger:*:*:1.0: (optional) ILogger components to pass log messages.
- *:counters:*:*:1.0: (optional) ICounters components to pass collected measurements.
Constructors
Creates an instance of this service.
publicCloudFunctionService(string name)
- name: string - name of the service used to generate an action cmd.
Creates an instance of this service.
publicCloudFunctionService()
Fields
Instance methods
applyInterceptors
Applies interceptors to the action.
protectedapplyInterceptors(action: (req: Request, res: Response) => Promise<any>): (req: Request, res: Response) => Promise<any>
- action: (req: Request, res: Response) => Promise<any> - action
- returns: (req: Request, res: Response) => Promise<any> - returned result
applyValidation
Performs a validation.
protectedapplyValidation(schema: Schema, action: (req: Request, res: Response) => Promise<any>): (req: Request, res: Response) => Promise<any>
- schema: Schema - schema used in the validation
- action: (req: Request, res: Response) => Promise<any> - action
- returns: (req: Request, res: Response) => Promise<any> - returned result
close
Closes a component and frees used resources.
publicclose(correlationId: string): Promise<void>
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
Configure
Configures a component by passing its configuration parameters.
publicvoid Configure(ConfigParams config)
- config: ConfigParams - configuration parameters to be set.
generateActionCmd
Adds ‘.cmd’ to a command name
protectedgenerateActionCmd(name: string): string
- name: string - command name
- returns: string - command name with ‘.cmd’ added at its end.
GetActions
Get all actions supported by the service.
publicList<CloudFunctionAction> GetActions()
- returns: List<CloudFunctionAction> - array with supported actions.
getCommand
Returns command from Google Function request.
This method can be overloaded in child classes
protectedgetCommand(req: any): string
- req: any - the function request
- returns: string - returned command from request.
getCorrelationId
Returns correlationId from Google Function request.
This method can be overloaded in child classes
protectedgetCorrelationId(req: any): string
- req: any - the function request
- returns: string - returned correlationId from request.
Instrument
Adds instrumentation to log calls and measure call time. It returns a CounterTiming object that is used to end the time measurement.
protectedCounterTiming Instrument(string correlationId, string name)
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
- name: string - method name.
- returns: CounterTiming - CounterTiming object to end the time measurement.
InstrumentError
Adds instrumentation to error handling.
protectedvoid InstrumentError(string correlationId, string methodName, Exception ex, bool rethrow = false)
- correlationId: string - (optional) transaction id to trace execution through call chain.
- methodName: string - a method name.
- ex: Exception - Error that occured during the method call.
- rethrow: bool - True to throw the exception.
IsOpen
Checks if the component is open.
publicbool IsOpen()
- returns: bool - true if the component is open and false otherwise.
OpenAsync
Opens the component.
publicTask OpenAsync(string correlationId)
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
RegisterAction
Registers an action in Google Function function.
protectedvoid RegisterAction(string name, Schema schema, Func<HttpContext, Task> action)
- name: string - action name
- schema: Schema - validation schema used to validate received parameters.
- action: Func<HttpContext, Task> - action function that is called when the operation is invoked.
registerActionWithAuth
Registers an action with authorization.
protectedvoid RegisterActionWithAuth(string name, Schema schema, Func<HttpContext, Func<HttpContext, Task>, Task> authorize, Func<HttpContext, Task> action)
- name: string - action’s name
- schema: Schema - validation schema used to validate received parameters.
- authorize: Func<HttpContext, Func<HttpContext, Task>, Task> - authorization interceptor
- action: Func<HttpContext, Task> - action function that is called when the operation is invoked.
RegisterInterceptor
Registers a middleware for actions in Google Function service.
protectedvoid RegisterInterceptor(Func<Func<HttpContext, Task>, Task> action)
- action: Func<Func<HttpContext, Task>, Task> - action function that is called when middleware is invoked.
SetReferences
Sets references to dependent components.
publicvoid SetReferences(IReferences references)
- references: IReferences - references to locate the component’s dependencies.
Abstract methods
Register
Registers all service routes in an HTTP endpoint.
This method is called by the service and must be overridden in child classes.
protected abstractvoid Register()
Examples
public class MyCloudFunctionService : CloudFunctionService
{
private IMyController _controller;
public MyCloudFunctionService(IMyController controller) : base("v1.myservice")
{
_controller = controller;
this._dependencyResolver.Put("controller", new Descriptor("mygroup", "controller", "*", "*", "1.0"));
}
public override void SetReferences(IReferences references)
{
base.SetReferences(references);
_controller = _dependencyResolver.GetRequired<IMyController>("controller");
}
public static void m()
{
var service = new MyCloudFunctionService(controller);
service.Configure(ConfigParams.FromTuples(
"connection.protocol", "http",
"connection.host", "localhost",
"connection.port", 8080
));
service.SetReferences(References.FromTuples(
new Descriptor("mygroup", "controller", "default", "default", "1.0"), controller
));
await service.OpenAsync("123");
}
protected override void Register()
{
RegisterAction("get_dummies", new ObjectSchema()
.WithOptionalProperty("body",
new ObjectSchema()
.WithOptionalProperty("filter", new FilterParamsSchema())
.WithOptionalProperty("paging", new PagingParamsSchema())
.WithRequiredProperty("cmd", TypeCode.String)
),
async (req) =>
{
var correlationId = GetCorrelationId(req);
var body = await CloudFunctionRequestHelper.GetBodyAsync(req);
var id = body.GetAsString("id");
return await this._controller.getMyData(correlationId, id);
}
);
}
}