AzureFunctionService

Abstract service that receives remote calls via the Azure Function protocol.

Implements: IAzureFunctionService, IOpenable, IConfigurable, IReferenceable

Description

The AzureFunctionService class allows you to create a service that receives remote calls via the Azure Function protocol.

Important points

  • This service is intended to work inside an AzureFunction 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.

AzureFunctionService(name: Optional[str])

  • name: Optional[str]s - name of the service used to generate an action cmd.

Fields

_counters

Performance counters.

_counters: CompositeCounters

_dependencyResolver

Dependency resolver.

_dependencyResolver: DependencyResolver

_logger

Logger.

_logger: CompositeLogger

_tracer

Tracer.

_tracer: CompositeTracer

Instance methods

act

Calls registered actions in this Azure Function. The “cmd” parameter in the action parameters determines what action shall be called.

This method shall only be used in testing.

act(context: HttpRequest): Any

  • context: HttpRequest - context context.
  • returns: Any- returned result

_apply_interceptors

Applies interceptors to the action.

_apply_interceptors(action: Callable[[HttpRequest], Any]): Callable[[HttpRequest], Any]

_apply_validation

Performs a validation.

_apply_validation(self, schema: Schema, action: Callable[[HttpRequest], Any]): Callable[[HttpRequest], Any]

  • schema: Schema - schema used in the validation
  • action: Callable[[HttpRequest], Any] - action
  • returns: Callable[[HttpRequest], Any] - returned result

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

Configures a component by passing its configuration parameters.

_apply_interceptors(action: Callable[[HttpRequest], Any]): Callable[[HttpRequest], Any]

  • action: Callable[[HttpRequest], Any] - configuration parameters to be set.
  • action: Callable[[HttpRequest], Any] - configuration parameters to be set.

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

getActions

Get all actions supported by the service.

get_actions(): List[AzureFunctionAction[]]

_get_command

Returns a command from the Azure Function context.

This method can be overloaded in child classes.

_get_command(context: HttpRequest): str

  • context: HttpRequest - context.
  • returns: str - returned command from context.

_get_correlation_id

Returns a correlationId from the Azure Function context.

This method can be overloaded in child classes.

_get_correlation_id(context: HttpRequest): str

  • context: HttpRequest - context.
  • returns: str - returned correlationId from context.

_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: 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.

is_open

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 Azure Function function.

_register_action(name: str, schema: Schema, action: Callable[[HttpRequest], Any])

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

_register_action_with_auth

Registers an action with authorization.

_register_action_with_auth(name: str, schema: Schema, authorize: Callable[[HttpRequest, Callable[[HttpRequest], Any]], Any], action: Callable[[HttpRequest], Any]):

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

_register_interceptor

Registers a middleware for actions in Azure Function service.

_register_interceptor(action: Callable[[HttpRequest, Callable[[HttpRequest], Any]], Any])

  • action: Callable[[HttpRequest, Callable[[HttpRequest], Any]], Any] - action function that is called when middleware is invoked.

setReferences

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 MyAzureFunctionService(AzureFunctionService):
    def __init__(self):
        super().__init__('v1.myservice')
        self._dependency_resolver.put(
            "controller",
            Descriptor("mygroup", "controller", "*", "*", "1.0")
        )
        self.__controller: IMyController = None
    def set_references(self, references: IReferences):
        super().set_references(references)
        self.__controller = self._dependency_resolver.get_required("controller")
    def __get_mydata(self, context: HttpRequest):
        data = context.get_json()
        correlation_id = data.get('correlation_id')
        id = data.get('id')
        return self.__controller.get_my_data(correlation_id, id)
    def register(self):
        self._register_action(
            'get_mydata',
            None,
            self.__get_mydata
        )
        ...

service = MyAzureFunctionService()
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")