As the codebase grew, Batman wanted to have a way to organise the code. Robyn told him that he had the super power to organise the code in a better way. He told him about the concept of views and subrouters.
Views
To organise your code in a better way - either to group by responsibility or for code splitting, you can use views.
A view, simply is a function with a collection of other closures. e.g.
Request
def sample_view():
def get():
return "Hello, world!"
def post(request):
body = request.body
return {"status_code": 200, "description": body}
The above view contains two closures for the get and the post request.
You can serve views in two ways:
Using an @app.view
decorator.
Request
@app.view("/sync/view/decorator")
def sync_decorator_view():
def get():
return "Hello, world!"
def post(request):
body = request.body
return {"status_code": 200, "description": body}
Using an @app.view
decorator.
Request
from .views import sample_view
...
...
app.add_view("/", sample_view)
Subrouters
Batman can create subrouters in Robyn. This is useful when you want to group routes together.
Subrouters can be used for both normal routes and web sockets. They are basically a mini version of the main router and can be used in the same way.
The only thing to remember is that you need to add the subrouter to the main router.
Request
from robyn import Robyn, SubRouter
app = Robyn(__file__)
sub_router = SubRouter("/sub_router")
@sub_router.get("/hello")
def hello():
return "Hello, world"
web_socket = SubRouter("/web_socket")
@web_socket.message()
async def hello():
return "Hello, world"
app.include_router(sub_router)
What's next?
Batman, being the familiar with the dark side wanted to know about Exceptions!
Robyn introduced him to the concept of exceptions and how he can use them to handle errors in his application.