add first commit
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import time
|
||||
|
||||
import zenoh
|
||||
|
||||
from chongming_worker import rpc_method
|
||||
from chongming_request import Client
|
||||
|
||||
from ..models.pydantic import (
|
||||
AddGetRequest,
|
||||
AddGetResponse,
|
||||
AddPostRequest,
|
||||
AddPostResponse
|
||||
)
|
||||
|
||||
@rpc_method(
|
||||
subject="example.add",
|
||||
method="GET",
|
||||
summary="Add two numbers via GET",
|
||||
response_model=AddGetResponse,
|
||||
)
|
||||
async def add(add_get_request:AddGetRequest) -> AddGetResponse:
|
||||
"""加法运算(GET)— 通过查询参数传入 a、b,返回它们的和"""
|
||||
return AddGetResponse(
|
||||
result=add_get_request.a + add_get_request.b,
|
||||
operation="add",
|
||||
timestamp=time.time()
|
||||
)
|
||||
|
||||
@rpc_method(
|
||||
subject="example.add",
|
||||
method="POST",
|
||||
summary="Add two numbers via POST",
|
||||
response_model=AddPostResponse,
|
||||
)
|
||||
async def add_post(add_post_request: AddPostRequest, session: zenoh.Session) -> AddPostResponse:
|
||||
"""加法运算(POST)— 通过 JSON Body 传入 a、b,返回它们的和"""
|
||||
async with Client(session=session) as client:
|
||||
add_result = await client.call(
|
||||
worker_type="example",
|
||||
subject="example.add",
|
||||
payload={
|
||||
"a":add_post_request.a,
|
||||
"b":add_post_request.b
|
||||
},
|
||||
method="GET"
|
||||
)
|
||||
print(add_result)
|
||||
return AddPostResponse(
|
||||
result=add_post_request.a + add_post_request.b,
|
||||
operation="add",
|
||||
timestamp=time.time(),
|
||||
)
|
||||
Reference in New Issue
Block a user