Utils

class falcon_auth2.RequestAttributes(req: falcon.request.Request, resp: falcon.response.Response, resource: Any, params: dict, is_async: bool)[source]

Named tuple that is passed to the backend authenticate() when a request is performed.

req: falcon.request.Request

The falcon request.

resp: falcon.response.Response

The falcon response.

resource: Any

The falcon responder resource.

params: dict

The parameters of passed in the url.

is_async: bool

Indicates that authenticate is running in async mode.

Async utilities

async falcon_auth2.utils.greenlet_spawn(fn: Callable, *args, **kwargs) Any[source]

Runs a sync function fn in a new greenlet.

The sync function can then use await_() to wait for async functions.

Parameters
  • fn (Callable) – The sync callable to call.

  • *args – Positional arguments to pass to the fn callable.

  • **kwargs – Keyword arguments to pass to the fn callable.

Returns

Any – The return value of fn or raises an exception if it raised one.

falcon_auth2.utils.await_(awaitable: Coroutine) Any[source]

Awaits an async function in a sync method.

The sync method must be insice a greenlet_spawn() context. await_() calls cannot be nested.

Parameters

awaitable (Coroutine) – The coroutine to call.

Raises

RuntimeError – If await_ was called outside a greenlet_spawn() context or nested in another await_ call.

Returns

Any – The return value of awaitable or raises an exception if it raised one.

falcon_auth2.utils.call_maybe_async(support_async: bool, function_is_async: Optional[bool], err_msg: str, function: Callable, *args, **kwargs) Tuple[Any, bool][source]

Calls a function and waits for the result if it is async.

Parameters
  • support_async (bool) – Can run async cuntions.

  • function_is_async (Optional[bool]) – If the function is async. This function will determine if function is async when this parameter is None.

  • err_msg (str) – Name of the function. Used in case of error.

  • function (Callable) – The function to call.

  • *args – Positional arguments to pass to the function callable.

  • **kwargs – Keyword arguments to pass to the function callable.

Raises

TypeError – if function is async and support_async=False.

Returns

Tuple[Any, bool] – Returns the result and whatever the function is async.