CloudFunctionService

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

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.

CloudFunctionService(name: str)

  • name: string - name of the service 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(correlation_id: Optional[str])

  • correlation_id: Optional[str] - (optional) transaction id used to trace execution through the 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 service.

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_correlation_id

Returns correlationId from Google Function request.

This method can be overloaded in child classes

_get_correlation_id(req: flask.Request): str

  • req: flask.Request - the function request
  • returns: str - returned correlationId 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(correlation_id: Optional[str], name: str): InstrumentTiming

  • correlation_id: Optional[str] - (optional) transaction id used to trace execution through the 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(correlation_id: Optional[str])

  • correlation_id: Optional[str] - (optional) transaction id used to trace execution through the 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 MyCloudFunctionService(CloudFunctionService):
    _controller: IMyController
   ...

   def __init__(self):
        super().__init__('v1.myservice')
        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):
        correlation_id = self._get_correlation_id(req)
        id = req.args.get('id')
        return self._controller.get_my_data(correlationId, id)

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

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

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

service.open("123")