|
3 | 3 | from datetime import datetime |
4 | 4 | from typing import Callable, Any, List, Dict |
5 | 5 |
|
6 | | -import azure.functions as func |
7 | | - |
8 | 6 |
|
9 | 7 | class InternalEntityException(Exception): |
10 | 8 | """Framework-internal Exception class (for internal use only).""" |
11 | 9 |
|
12 | 10 | pass |
13 | 11 |
|
14 | 12 |
|
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 | | - |
52 | 13 | class Entity: |
53 | 14 | """Durable Entity Class. |
54 | 15 |
|
@@ -131,10 +92,22 @@ def create(cls, fn: Callable[[DurableEntityContext], None]) -> Callable[[Any], s |
131 | 92 |
|
132 | 93 | Returns |
133 | 94 | ------- |
134 | | - EntityHandler |
135 | | - Entity Handler callable for the newly created entity client |
| 95 | + Callable[[Any], str] |
| 96 | + Handle function of the newly created entity client |
136 | 97 | """ |
137 | | - return EntityHandler(fn) |
| 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 | + |
| 108 | + handle.entity_function = fn |
| 109 | + |
| 110 | + return handle |
138 | 111 |
|
139 | 112 | def _elapsed_milliseconds_since(self, start_time: datetime) -> int: |
140 | 113 | """Calculate the elapsed time, in milliseconds, from the start_time to the present. |
|
0 commit comments