add first commit
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
from .add import add, add_post
|
||||
from .subtract import subtract
|
||||
from .multiply import multiply
|
||||
from .divide import divide
|
||||
from .count import count_get
|
||||
|
||||
__all__ = [
|
||||
"add",
|
||||
"add_post",
|
||||
"subtract",
|
||||
"multiply",
|
||||
"divide",
|
||||
"count_get"
|
||||
]
|
||||
@@ -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(),
|
||||
)
|
||||
@@ -0,0 +1,33 @@
|
||||
import modelvault
|
||||
|
||||
from chongming_worker import rpc_method
|
||||
|
||||
from ..models.pydantic import CountGetResponse
|
||||
from ..models.counter import Counter
|
||||
|
||||
|
||||
@rpc_method(
|
||||
subject="example.count",
|
||||
method="GET",
|
||||
summary="Increment and get counter value",
|
||||
response_model=CountGetResponse, # 需要定义 CountResponse
|
||||
)
|
||||
async def count_get() -> CountGetResponse:
|
||||
from ..global_var import db
|
||||
|
||||
# 确保数据库已初始化(如果未初始化,则同步初始化,但这里假设已就绪)
|
||||
if db is None:
|
||||
raise RuntimeError("Database not initialized")
|
||||
|
||||
counters = modelvault.models.async_collection(db, Counter) # type: ignore
|
||||
async with db.transaction():
|
||||
counter = await counters.get("visits")
|
||||
if counter is None:
|
||||
counter = Counter(name="visits", value=0)
|
||||
await counters.insert(counter)
|
||||
else:
|
||||
new_value = counter.value + 1
|
||||
await counters.update("visits", {"value": new_value})
|
||||
counter.value = new_value # 更新本地对象以便返回
|
||||
|
||||
return CountGetResponse(name=counter.name, value=counter.value) # type: ignore
|
||||
@@ -0,0 +1,29 @@
|
||||
import time
|
||||
|
||||
from chongming_worker import rpc_method
|
||||
|
||||
from ..models.pydantic import (
|
||||
DivideGetResponse,
|
||||
DivideGetRequest,
|
||||
)
|
||||
|
||||
@rpc_method(
|
||||
subject="example.divide",
|
||||
method="GET",
|
||||
summary="Divide two numbers",
|
||||
response_model=DivideGetResponse,
|
||||
)
|
||||
async def divide(divide_get_request: DivideGetRequest) -> DivideGetResponse:
|
||||
"""除法运算:a / b(b 不能为 0)"""
|
||||
if divide_get_request.b == 0:
|
||||
return DivideGetResponse(
|
||||
error="除数不能为零",
|
||||
operation="divide",
|
||||
result=0.0,
|
||||
timestamp=time.time(),
|
||||
)
|
||||
return DivideGetResponse(
|
||||
result=divide_get_request.a / divide_get_request.b,
|
||||
operation="divide",
|
||||
timestamp=time.time(),
|
||||
)
|
||||
@@ -0,0 +1,22 @@
|
||||
import time
|
||||
|
||||
from chongming_worker import rpc_method
|
||||
|
||||
from ..models.pydantic import (
|
||||
MultiplyGetRequest,
|
||||
MultiplyGetResponse,
|
||||
)
|
||||
|
||||
@rpc_method(
|
||||
subject="example.multiply",
|
||||
method="GET",
|
||||
summary="Multiply two numbers",
|
||||
response_model=MultiplyGetResponse,
|
||||
)
|
||||
async def multiply(multiply_get_request=MultiplyGetRequest) -> MultiplyGetResponse:
|
||||
"""乘法运算:a * b"""
|
||||
return MultiplyGetResponse(
|
||||
result=multiply_get_request.a * multiply_get_request.b,
|
||||
operation="multiply",
|
||||
timestamp=time.time(),
|
||||
)
|
||||
@@ -0,0 +1,22 @@
|
||||
import time
|
||||
|
||||
from chongming_worker import rpc_method
|
||||
|
||||
from ..models.pydantic import (
|
||||
SubtractGetResponse,
|
||||
SubtractGetRequest,
|
||||
)
|
||||
|
||||
@rpc_method(
|
||||
subject="example.subtract",
|
||||
method="GET",
|
||||
summary="Subtract two numbers",
|
||||
response_model=SubtractGetResponse,
|
||||
)
|
||||
async def subtract(subtract_get_request: SubtractGetRequest) -> SubtractGetResponse:
|
||||
"""减法运算:a - b"""
|
||||
return SubtractGetResponse(
|
||||
result=subtract_get_request.a - subtract_get_request.b,
|
||||
operation="subtract",
|
||||
timestamp=time.time(),
|
||||
)
|
||||
Reference in New Issue
Block a user