-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
blockchain_integration/pi_network/PiPulse/web/api/nodes.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from fastapi import APIRouter, HTTPException | ||
from fastapi.responses import JSONResponse | ||
from pydantic import BaseModel | ||
|
||
class Node(BaseModel): | ||
id: int | ||
name: str | ||
status: str | ||
|
||
nodes = [ | ||
Node(id=1, name="Node 1", status="online"), | ||
Node(id=2, name="Node 2", status="offline"), | ||
Node(id=3, name="Node 3", status="online"), | ||
] | ||
|
||
nodes_router = APIRouter() | ||
|
||
@nodes_router.get("/nodes") | ||
async def get_nodes(): | ||
return nodes | ||
|
||
@nodes_router.get("/nodes/{node_id}") | ||
async def get_node(node_id: int): | ||
for node in nodes: | ||
if node.id == node_id: | ||
return node | ||
raise HTTPException(status_code=404, detail="Node not found") | ||
|
||
@nodes_router.post("/nodes") | ||
async def create_node(node: Node): | ||
nodes.append(node) | ||
return node | ||
|
||
@nodes_router.put("/nodes/{node_id}") | ||
async def update_node(node_id: int, node: Node): | ||
for i, existing_node in enumerate(nodes): | ||
if existing_node.id == node_id: | ||
nodes[i] = node | ||
return node | ||
raise HTTPException(status_code=404, detail="Node not found") | ||
|
||
@nodes_router.delete("/nodes/{node_id}") | ||
async def delete_node(node_id: int): | ||
for i, existing_node in enumerate(nodes): | ||
if existing_node.id == node_id: | ||
del nodes[i] | ||
return JSONResponse(status_code=204) | ||
raise HTTPException(status_code=404, detail="Node not found") |