-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creating Nested Schema and Schema Not associated with Model #18
Comments
@arunksoman I'll try to answer your questions in the quickest way, but I surely have to start explaining the design pattern behind the REST extension. This extension is designed to be used with the whole Emmett stack, including its ORM. The rationale was to design an immediate way to expose standard REST endpoints above a single entity, represented by a Given the upper foreword, I gonna split your questions in 2 groups, as one has a very simple answer, the other 2 implies quite a lot of missing context.
I think the most robust answer I gan give you is: probably you shouldn't use this extension for that. Plain Emmett would be definitely supportive in working with json data regardless of the source, as you have the
Because Emmett-REST is designed for RESTFul APIs, which means not just fetching json data, but works with entities: creating, updating and deleting them. And the easiest way to design a RESTFul API is having verbs for a single entity, and thus a single Model. Making changes to other entities can still be done, but it is really dependant on your application logic, and providing a common way of doing so would be impossible. Should that be done on the Model level? On the controller level? Somewhere else? In case you want to present aggregated data coming from multiple entities, then, again, is probably easier to just write a specific
I would say it is really dependant on the type of relation you have between your entities, and what you need to do. For instance, is it a Also, since serializers and parsers are classes that can be initialized just passing the model they work on, you can define any number of serializers/parser and re-use them inside your code: from emmett_rest import Serializer, serialize
class AssigneeSerializer(Serializer):
attributes = ['id', 'email']
class TaskSerializer(Serializer):
_assignee_serializer = AssigneeSerializer(User)
def assignee(self, row):
return serialize([row.assignee], self._assignee_serializer)[0] For tasks = app.rest_module(__name__, 'api_task', Task, url_prefix='tasks')
class TaskRelatedPipe(Pipe):
async def pipe(self, next_pipe, **kwargs):
request.task = Task.get(kwargs.pop('task_id'))
if not request.task:
response.status = 404
return tasks.error_404()
return await next_pipe(**kwargs)
tasks_related = app.module(__name__, 'api_task_related', url_prefix='tasks/<int:task_id>')
tasks_related.pipeline = [TaskRelatedPipe()]
tasks_assignees = task_related.rest_module(__name__, 'api_task_asignee', User, url_prefix='assignees')
@tasks_assignees.get_dbset
def fetch_assignees():
return request.tasks.assignees you then probably want to remove the update method and override the create and delete ones to actually work on relations and not users, but you probably got the idea. Managing relations inside the In the end, without additional context, and a clear idea on what you need to do, is quite hard to give you the "perfect answer", but I hope to have exposed all the options on the table here :) |
Thank you so much for your time. I think I have to read few more times in order understand answer. Can I ask another question? Can we still generate open API docs if we are using |
Unfortunately not. OpenAPI integration is part of the REST extensions, as it uses models to generate schemas. |
How can we create nested json schema with emmett_rest? For example, data from more than one model or data from more than one related Model? Also, why it is mandatory to have return data from an endpoint only associated with a Model?
Here, if the json data is not associated with any Model, how can I use this extension?
The text was updated successfully, but these errors were encountered: