I’ve added a few QoL features, those being:
Reply Models
Before, you could only verify whats coming IN, but not whats coming OUT of the server. Now, you can do that easily:
from napi.http import HTTP, Reply, Require, ReplyModel, Error
@HTTP.GET()
@HTTP.Responses(
ReplyModel(200, str),
ReplyModel(400, Error),
strict= True
)
@HTTP.Require()
async def hello(ctx):
if ctx.body.get("hello"): raise Error(400, "No hello")
return "Hello World!"
In this case, the request will go through without an issue, but if, for example, you returned anything other than a string:
return 0
The request will fail automatically. This will also be useful for when I add OpenAPI generation, as the generator will have access to the responses.
This behavior can be disabled, by setting strict to False.
Nested object validation
@HTTP.Require(body = {"hello": {"world": str}) <- This code used to be invalid before.
I since added support for nested objects (no depth limit).
Automatic Toggle Switch Registration
Plugins used to register their own keys in the config, and then setup isEnabled method that would determine if the service should be started or not.
To reduce the headache and boilerplate of this approach, Nautica now automatically creates a key with your service’s name under services in config.n3. If you want your own logic in isEnabled, you can do so by overwriting the method. By doing so you’ll disable the automatic registration and the framework gives you the freedom to do it your way. If you want custom logic AND automatic registration, it can simply be combined by using super().isEnabled().
Require.ListOf(…)
I’d say this is a highly useful addition. It does exactly what it sounds like: It checks if all the items in a list match a certain schema.
Require.ListOf(str)
List of just strings
Require.ListOf({"hello": str})
List of dictionaries
Requrie.ListOf(Require.AnyTypeOf(str, int))
List of strings and integers
Comments 0
No comments yet. Be the first!
Sign in to join the conversation.