If multiple endpoints within an api.API class require the same parameter (such as a path parameter defined in the class route), you can declare it as a class property. This makes the parameter available to all methods in the class without repeating the declaration in every function signature.
To define a general parameter, assign a value from utilmeta.core.request to a class property. Common request parameter types include:
request.PathParam: For path parameters.request.QueryParam: For query string parameters.request.HeaderParam: For request header parameters.request.CookieParam: For cookie parameters.
Example: Using @api.route to define a path template and capturing a slug for all methods in the class.
from utilmeta.core import api, request
@api.route('{slug}/comments')
class CommentAPI(api.API):
slug: str = request.PathParam
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# self.slug is now available for use in __init__ and all methods
self.article = Article.objects.filter(slug=self.slug).first()
def get(self):
return CommentSchema.serialize(Comment.objects.filter(article=self.article))