|
3 | 3 | from datetime import datetime |
4 | 4 | from typing import Callable, Any, List, Dict |
5 | 5 |
|
| 6 | +import azure.functions as func |
6 | 7 |
|
7 | 8 | class InternalEntityException(Exception): |
8 | 9 | """Framework-internal Exception class (for internal use only).""" |
9 | 10 |
|
10 | 11 | pass |
11 | 12 |
|
| 13 | +class EntityHandler(Callable): |
| 14 | + """Durable Entity Handler. |
| 15 | + A callable class that wraps the user defined entity function for execution by the Python worker |
| 16 | + and also allows access to the original method for unit testing |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, func: Callable[[DurableEntityContext], None]): |
| 20 | + """ |
| 21 | + Create a new entity handler for the user defined entity function. |
| 22 | + |
| 23 | + Parameters |
| 24 | + ---------- |
| 25 | + func: Callable[[DurableEntityContext], None] |
| 26 | + The user defined entity function. |
| 27 | + """ |
| 28 | + self._func = func |
| 29 | + |
| 30 | + def __call__(self, context: func.EntityContext) -> str: |
| 31 | + """Handle the execution of the user defined entity function. |
| 32 | + Parameters |
| 33 | + ---------- |
| 34 | + context : func.EntityContext |
| 35 | + The DF entity context""" |
| 36 | + # It is not clear when the context JSON would be found |
| 37 | + # inside a "body"-key, but this pattern matches the |
| 38 | + # orchestrator implementation, so we keep it for safety. |
| 39 | + context_body = getattr(context, "body", None) |
| 40 | + if context_body is None: |
| 41 | + context_body = context |
| 42 | + ctx, batch = DurableEntityContext.from_json(context_body) |
| 43 | + return Entity(self._func).handle(ctx, batch) |
| 44 | + |
12 | 45 |
|
13 | 46 | class Entity: |
14 | 47 | """Durable Entity Class. |
@@ -92,19 +125,10 @@ def create(cls, fn: Callable[[DurableEntityContext], None]) -> Callable[[Any], s |
92 | 125 |
|
93 | 126 | Returns |
94 | 127 | ------- |
95 | | - Callable[[Any], str] |
96 | | - Handle function of the newly created entity client |
| 128 | + EntityHandler |
| 129 | + Entity Handler callable for the newly created entity client |
97 | 130 | """ |
98 | | - def handle(context) -> str: |
99 | | - # It is not clear when the context JSON would be found |
100 | | - # inside a "body"-key, but this pattern matches the |
101 | | - # orchestrator implementation, so we keep it for safety. |
102 | | - context_body = getattr(context, "body", None) |
103 | | - if context_body is None: |
104 | | - context_body = context |
105 | | - ctx, batch = DurableEntityContext.from_json(context_body) |
106 | | - return Entity(fn).handle(ctx, batch) |
107 | | - return handle |
| 131 | + return EntityHandler(fn) |
108 | 132 |
|
109 | 133 | def _elapsed_milliseconds_since(self, start_time: datetime) -> int: |
110 | 134 | """Calculate the elapsed time, in milliseconds, from the start_time to the present. |
|
0 commit comments