Implements: IAzureFunctionController, IOpenable, IConfigurable, IReferenceable
Description
The AzureFunctionController class allows you to create a controller that receives remote calls via the Azure Function protocol.
Important points
- This controller 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 controller.
AzureFunctionController(name: Optional[str])
- name: Optional[str]s - name of the controller used to generate an action cmd.
Fields
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]
- action: Callable[[HttpRequest] - action
- returns: Callable[[HttpRequest] - returned result
_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(context: Optional[IContext])
- context: IContext - (optional) a context to trace execution through a 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 controller.
get_actions(): List[AzureFunctionAction[]]
- returns: List[AzureFunctionAction[]] - array with supported actions.
_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_trace_id
Returns a traceId from the Azure Function context.
This method can be overloaded in child classes.
_get_trace_id(context: HttpRequest): str
- context: HttpRequest - context.
- returns: str - returned traceId 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(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.
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(context: Optional[IContext])
- context: IContext - (optional) a context to trace execution through a 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 controller.
_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 controller routes in an HTTP endpoint.
This method is called by the controller and must be overridden in child classes.
abstractmethod
register()
Examples
class MyAzureFunctionController(AzureFunctionController):
def __init__(self):
super().__init__('v1.mycontroller')
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()
context = data.get('trace_id')
id = data.get('id')
return self.__controller.get_my_data(trace_id, id)
def register(self):
self._register_action(
'get_mydata',
None,
self.__get_mydata
)
...
controller = MyAzureFunctionController()
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")