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