Nexus query sdk ergonomics (#352) · temporalio/samples-python@7174ed7 · GitHub
Skip to content

Commit 7174ed7

Browse files
Nexus query sdk ergonomics (#352)
* Update Nexus messaging samples to use Temporal operation handlers * fixing comment
1 parent 05070f6 commit 7174ed7

4 files changed

Lines changed: 84 additions & 46 deletions

File tree

‎nexus_messaging/callerpattern/README.md‎

Lines changed: 2 additions & 2 deletions

‎nexus_messaging/callerpattern/handler/service_handler.py‎

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import nexusrpc
1010
from temporalio import nexus
11-
from temporalio.client import WorkflowHandle
11+
from temporalio.client import Client, WorkflowHandle
1212

1313
from nexus_messaging.callerpattern.handler.workflows import GreetingWorkflow
1414
from nexus_messaging.callerpattern.service import (
@@ -38,43 +38,61 @@ def get_workflow_id(user_id: str) -> str:
3838
@nexusrpc.handler.service_handler(service=NexusGreetingService)
3939
class NexusGreetingServiceHandler:
4040
def _get_workflow_handle(
41-
self, user_id: str
41+
self, client: Client, user_id: str
4242
) -> WorkflowHandle[GreetingWorkflow, str]:
43-
return nexus.client().get_workflow_handle_for(
43+
return client.get_workflow_handle_for(
4444
GreetingWorkflow.run, get_workflow_id(user_id)
4545
)
4646

47-
@nexusrpc.handler.sync_operation
47+
@nexus.temporal_operation
4848
async def get_languages(
49-
self, ctx: nexusrpc.handler.StartOperationContext, input: GetLanguagesInput
50-
) -> GetLanguagesOutput:
51-
return await self._get_workflow_handle(input.user_id).query(
49+
self,
50+
_ctx: nexus.TemporalStartOperationContext,
51+
client: nexus.TemporalNexusClient,
52+
input: GetLanguagesInput,
53+
) -> nexus.TemporalOperationResult[GetLanguagesOutput]:
54+
result = await self._get_workflow_handle(client.client, input.user_id).query(
5255
GreetingWorkflow.get_languages, input
5356
)
57+
return nexus.TemporalOperationResult.sync(result)
5458

55-
@nexusrpc.handler.sync_operation
59+
@nexus.temporal_operation
5660
async def get_language(
57-
self, ctx: nexusrpc.handler.StartOperationContext, input: GetLanguageInput
58-
) -> Language:
59-
return await self._get_workflow_handle(input.user_id).query(
61+
self,
62+
_ctx: nexus.TemporalStartOperationContext,
63+
client: nexus.TemporalNexusClient,
64+
input: GetLanguageInput,
65+
) -> nexus.TemporalOperationResult[Language]:
66+
result = await self._get_workflow_handle(client.client, input.user_id).query(
6067
GreetingWorkflow.get_language
6168
)
69+
return nexus.TemporalOperationResult.sync(result)
6270

6371
# Routes to set_language_using_activity (not set_language) so that new languages not
6472
# already in the greetings map can be fetched via an activity.
65-
@nexusrpc.handler.sync_operation
73+
@nexus.temporal_operation
6674
async def set_language(
67-
self, ctx: nexusrpc.handler.StartOperationContext, input: SetLanguageInput
68-
) -> Language:
69-
return await self._get_workflow_handle(input.user_id).execute_update(
70-
GreetingWorkflow.set_language_using_activity, input
75+
self,
76+
_ctx: nexus.TemporalStartOperationContext,
77+
client: nexus.TemporalNexusClient,
78+
input: SetLanguageInput,
79+
) -> nexus.TemporalOperationResult[Language]:
80+
result = await self._get_workflow_handle(
81+
client.client, input.user_id
82+
).execute_update(
83+
GreetingWorkflow.set_language_using_activity,
84+
input,
7185
)
86+
return nexus.TemporalOperationResult.sync(result)
7287

73-
@nexusrpc.handler.sync_operation
88+
@nexus.temporal_operation
7489
async def approve(
75-
self, ctx: nexusrpc.handler.StartOperationContext, input: ApproveInput
76-
) -> ApproveOutput:
77-
await self._get_workflow_handle(input.user_id).signal(
90+
self,
91+
_ctx: nexus.TemporalStartOperationContext,
92+
client: nexus.TemporalNexusClient,
93+
input: ApproveInput,
94+
) -> nexus.TemporalOperationResult[ApproveOutput]:
95+
await self._get_workflow_handle(client.client, input.user_id).signal(
7896
GreetingWorkflow.approve, input
7997
)
80-
return ApproveOutput()
98+
return nexus.TemporalOperationResult.sync(ApproveOutput())

‎nexus_messaging/ondemandpattern/handler/service_handler.py‎

Lines changed: 42 additions & 22 deletions

‎uv.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)