Skip to content

Commit

Permalink
append rest_api
Browse files Browse the repository at this point in the history
  • Loading branch information
panh99 committed Mar 28, 2024
1 parent cce523e commit e28cc8c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/py/flwr/client/rest_client/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,26 @@ def ping() -> None:

# Check status code and headers
if res.status_code != 200:
if not ping_stop_event.is_set():
ping_stop_event.wait(PING_CALL_TIMEOUT)
return
if "content-type" not in res.headers:
log(
WARN,
"[Node] POST /%s: missing header `Content-Type`",
PATH_PULL_TASK_INS,
)
if not ping_stop_event.is_set():
ping_stop_event.wait(PING_CALL_TIMEOUT)
return
if res.headers["content-type"] != "application/protobuf":
log(
WARN,
"[Node] POST /%s: header `Content-Type` has wrong value",
PATH_PULL_TASK_INS,
)
if not ping_stop_event.is_set():
ping_stop_event.wait(PING_CALL_TIMEOUT)
return

# Deserialize ProtoBuf from bytes
Expand Down
28 changes: 28 additions & 0 deletions src/py/flwr/server/superlink/fleet/rest_rere/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from flwr.proto.fleet_pb2 import ( # pylint: disable=E0611
CreateNodeRequest,
DeleteNodeRequest,
PingRequest,
PullTaskInsRequest,
PushTaskResRequest,
)
Expand Down Expand Up @@ -152,11 +153,38 @@ async def push_task_res(request: Request) -> Response: # Check if token is need
)


async def ping(request: Request) -> Response:
"""Ping."""
_check_headers(request.headers)

# Get the request body as raw bytes
ping_request_bytes: bytes = await request.body()

# Deserialize ProtoBuf
ping_request_proto = PingRequest()
ping_request_proto.ParseFromString(ping_request_bytes)

# Get state from app
state: State = app.state.STATE_FACTORY.state()

# Handle message
ping_response_proto = message_handler.ping(request=ping_request_proto, state=state)

# Return serialized ProtoBuf
ping_response_bytes = ping_response_proto.SerializeToString()
return Response(
status_code=200,
content=ping_response_bytes,
headers={"Content-Type": "application/protobuf"},
)


routes = [
Route("/api/v0/fleet/create-node", create_node, methods=["POST"]),
Route("/api/v0/fleet/delete-node", delete_node, methods=["POST"]),
Route("/api/v0/fleet/pull-task-ins", pull_task_ins, methods=["POST"]),
Route("/api/v0/fleet/push-task-res", push_task_res, methods=["POST"]),
Route("/api/v0/fleet/ping", ping, methods=["POST"]),
]

app: Starlette = Starlette(
Expand Down

0 comments on commit e28cc8c

Please sign in to comment.