I'm looking for advice on a design pattern for creating a REST API using Flask (with flask restx) handling multiple collections ("hub") which each have items ("nodes").
Example: HubA will have N nodes it manages. HubB will have M nodes it manages. ... so on and so forth.
I want to create an API that allows someone to configure a hub and populate it with nodes and then configure and manage each node on that hub. With multiple hubs being in the system.
My thoughts were to try something like this but I've been unsuccessful at breaking this up into anything other than one large python file with all the routes defined. Ideally I'd have a "hubs" routes file and a "nodes" route file. I've attempted using namespaces and nested blueprints but I seem to be flailing.
GET /api/hubs/ - Retrieve a list of hubs
POST /api/hubs/ - Create a new hub
GET /api/hubs/{idh} - Retrieve hub with ID of {idh}
PUT /api/hubs/{idh} - Update hub with ID of {idh}
DELETE /api/hubs/{idh} - Delete hub with ID of {idh}
GET /api/hubs/{idh}/nodes - Retrieve a list of nodes on hub {idh} with ID of {idn}
POST /api/hubs/{idh}/nodes - Create a new node on hub {idh} with ID of {idn}
GET /api/hubs/{idh}/nodes/{idn} - Retrieve node with ID of {idn} on hub {idh}
PUT /api/hubs/{idh}/nodes/{idn} - Update node with ID of {idn} on hub {idh}
DELETE /api/hubs/{idh}/nodes/{idn} - Delete node with ID of {idn} on hub {idh}
Thanks to anyone who helps my comprehension/confusion. I'm not married to any idea currently. Here's what I've written and the swagger UI does not render at /hubs (TypeError)
project/
├── app.py
└── apis
├── __init__.py
├── hubs
| ├── __init__.py
| └── routes.py
└── nodes
├── __init__.py
└── routes.py
app.py
from flask import Flask
from apis import hubsblueprint as api
app = Flask(__name__)
app.register_blueprint(api)
app.run(debug=True)
apis.init.py
from flask_restx import Api
from .nodes.routes import nodesblueprint
from .hubs.routes import hubsblueprint
hubsblueprint.register_blueprint(nodesblueprint)
api = Api(
hubsblueprint,
version="1.0",
title="hubs and nodes API",
description="A Rest API for managing the network")
apis.hubs.routes.py
from flask import Blueprint
from flask_restx import Resource
hubsblueprint = Blueprint('hubs', __name__, url_prefix='/hubs')
@hubsblueprint.route("/")
class HubsCollection(Resource):
def get(self):
pass
def post(self):
pass
@hubsblueprint.route('/<int:id>')
class HubsItem(Resource):
def get(self):
pass
def put(self):
pass
def delete(self):
pass
apis.nodes.routes.py
from flask import Blueprint
from flask_restx import Resource
nodesblueprint = Blueprint('nodes', __name__, url_prefix='/nodes')
@nodesblueprint.route('/')
class NodesCollection(Resource):
def get(self):
pass
def post(self):
pass
@nodesblueprint.route('/<int:id>')
class NodesItem(Resource):
def get(self):
pass
def put(self):
pass
def delete(self):
pass