nuropb.contexts.context_manager_decorator

Module Contents

Functions

method_requires_nuropb_context

This function checks if a method has been decorated with @nuropb_context

nuropb_context

This decorator function injects a NuropbContext instance into a method that has ctx:NuropbContext as an argument. The ctx parameter of the decorated method is hidden from the method’s signature visible on the service mesh.

API

nuropb.contexts.context_manager_decorator.method_requires_nuropb_context(method: Callable[..., Any]) bool[source]

This function checks if a method has been decorated with @nuropb_context

Parameters:

method – Callable

Returns:

bool

nuropb.contexts.context_manager_decorator.nuropb_context(original_method: Optional[Callable[..., Any]] = None, *, context_parameter: str = 'ctx', suppress_exceptions: bool = False) Any[source]

This decorator function injects a NuropbContext instance into a method that has ctx:NuropbContext as an argument. The ctx parameter of the decorated method is hidden from the method’s signature visible on the service mesh.

The name of the ctx parameter can be changed by passing a string to the context_parameter argument.

The caller of class_instance.method(ctx=ctx) can either pass a NuropbContext instance or a dict. If a dict is passed, a NuropbContext instance will be created from the dict.

NOTE This decorator is only for with class methods, using with functions will have unexpected results and is likely to raise a TypeException

As illustrated by the example below, @nuropb_context must always be on top of @publish_to_mesh when both decorators are used.

@nuropb_context
@publish_to_mesh(context_token_key="Authorization", authorize_func=authorise_token)
def hello_requires_auth(...)
Parameters:
  • original_method – the method to be decorated

  • context_parameter – str, (ctx) alternative context argument name

  • suppress_exceptions – bool, (True), if False then exceptions will be raised during with ctx:

Returns:

a decorated method