CloudFunctionController

Abstract controller that receives remove calls via the Google Function protocol.

Implements: ICloudFunctionController, IOpenable, IConfigurable, IReferenceable

Description

The CloudFunctionController class allows you to create a controller that receives remove calls via the Google Function protocol.

Important points

  • This controller 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 controller.

CloudFunctionController(name: str)

  • name: string - name of the controller used to generate an action cmd.

Fields

_counters

Performance counters.

_counters: CompositeCounters

_dependency_resolver

Dependency resolver.

_dependency_resolver: DependencyResolver

_logger

Logger.

_logger: CompositeLogger

_tracer

Tracer.

_tracer: CompositeTracer

Instance methods

_apply_interceptors

Applies interceptors to the action.

_apply_interceptors(action: Callable[flask.Request, Any]): Callable[flask.Request, Any]

_apply_validation

Performs a validation.

_apply_validation(schema: Schema, action: Callable[flask.Request, Any]): Callable[flask.Request, Any]

close

Closes a component and frees used resources.

close(context: Optional[IContext])

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

_apply_interceptors

Apply interceptors to the actions

_apply_interceptors(action: Callable[flask.Request, Any]) -> Callable[flask.Request, Any]:

  • action: Callable[flask.Request, Any] - configuration parameters to be set.
  • returns: Callable[flask.Request, Any] - wrapped function into interceptor

_generate_action_cmd

Adds ‘.cmd’ to a command name

_generate_action_cmd(name: str): str

  • name: str - command name
  • returns: str - command name with ‘.cmd’ added at its end.

get_actions

Get all actions supported by the controller.

get_actions(): List[CloudFunctionAction]

_get_command

Returns command from Google Function request.

This method can be overloaded in child classes

_get_command(req: flask.Request): str

  • req: flask.Request - the function request
  • returns: str - returned command from request.

_get_trace_id

Returns traceId from Google Function request.

This method can be overloaded in child classes

_get_trace_id(req: flask.Request): str

  • req: flask.Request - the function request
  • returns: str - returned traceId from request.

_instrument

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

_instrument(context: Optional[IContext], name: str): InstrumentTiming

  • context: IContext - (optional) a context to trace execution through a call chain.
  • name: str - method’s name.
  • returns: InstrumentTiming - Timing object to end the time measurement.

isOpen

Checks if the component is open.

isOpen(): bool

  • returns: bool - true if the component is open and false otherwise.

open

Opens the component.

open(context: Optional[IContext])

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

_register_action

Registers an action in Google Function function.

_register_action(name: str, schema: Schema, action: Callable[flask.Request, Any])

  • name: str - action name
  • schema: Schema - validation schema used to validate received parameters.
  • action: Callable[flask.Request, Any] - action function that is called when the operation is invoked.

_register_action_with_auth

Registers an action with authorization.

_register_action_with_auth(self, name: str, schema: Schema, authorize: Callable[[Any, Callable[flask.Request, Any]], Any], action: Callable[flask.Request, Any])

  • name: string - action’s name
  • schema: Schema - validation schema used to validate received parameters.
  • authorize: Callable[[Any, Callable[flask.Request, Any]] - authorization interceptor
  • action: Callable[flask.Request, Any] - action function that is called when the operation is invoked.

_register_interceptor

Registers a middleware for actions in Google Function service.

_register_interceptor(cmd: str, action: Callable[[flask.Request], Any])

  • cmd: str - the command name for intercept or regex.
  • action: Callable[[flask.Request], Any] - action function that is called when middleware is invoked.

set_references

Sets references to dependent components.

set_references(references: IReferences)

  • 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.

abstractmethod register()

Examples

class MyCloudFunctionController(CloudFunctionController):
    _controller: IMyController
   ...

   def __init__(self):
        super().__init__('v1.mycontroller')
        self._dependency_resolver.put(
            "controller",
            Descriptor("mygroup","controller","*","*","1.0")
        )

   def set_references(self, references: IReferences):
      super().set_references(references)
      self._controller = self._dependency_resolver.get_required("controller")

   def __action(self, req):
        trace_id = self._get_trace_id(req)
        id = req.args.get('id')
        return self._controller.get_my_data(traceId, id)

   def register(self):
       self._register_action("get_my_data", None, self.__action)
       ...

controller = MyCloudFunctionController()
controller.configure(ConfigParams.from_tuples(
    "connection.protocol", "http",
    "connection.host", "localhost",
    "connection.port", 8080
))

controller.set_references(References.from_tuples(
    Descriptor("mygroup","controller","default","default","1.0"), controller
))

controller.open("123")