Skip to content

Commit

Permalink
Document workspaced mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
cript0nauta committed Aug 14, 2018
1 parent 3db6cb8 commit 3234853
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 10 deletions.
25 changes: 25 additions & 0 deletions doc/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ API Reference
Generic views
-------------

Use this ones to expose API endpoints that don't depend of a workspace, such as
users, vuln templates, or the workspaces API itself

.. autoclass:: server.api.base.GenericView
:members: model_class,schema_class,route_prefix,base_args,representations,
lookup_field,lookup_field_type,get_joinedloads, get_undefer,
Expand All @@ -20,10 +23,32 @@ Generic views
.. autoclass:: server.api.base.SortableMixin
.. autoclass:: server.api.base.ReadOnlyView
.. autoclass:: server.api.base.CreateMixin
:members: _perform_create
.. autoclass:: server.api.base.UpdateMixin
:members: _update_object, _perform_update

.. autoclass:: server.api.base.DeleteMixin
.. autoclass:: server.api.base.ReadWriteView


Generic workspaced views
------------------------

Use this type of views to make workspace-sensitive API endpoints. All views
created by this class need the user to specify a valid workspace name in the URL.

This is the most common type of views in Faraday.

.. autoclass:: server.api.base.GenericWorkspacedView
.. autoclass:: server.api.base.ListWorkspacedMixin
.. autoclass:: server.api.base.RetrieveWorkspacedMixin
.. autoclass:: server.api.base.ReadWriteWorkspacedView
.. autoclass:: server.api.base.CreateWorkspacedMixin
:members: _perform_create

.. autoclass:: server.api.base.UpdateWorkspacedMixin
:members: _update_object, _perform_update

.. autoclass:: server.api.base.DeleteWorkspacedMixin
.. autoclass:: server.api.base.CountWorkspacedMixin
.. autoclass:: server.api.base.ReadWriteWorkspacedView
68 changes: 58 additions & 10 deletions server/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,14 @@ def handle_invalid_usage(error):

class GenericWorkspacedView(GenericView):
"""Abstract class for a view that depends on the workspace, that is
passed in the URL"""
passed in the URL
.. note::
This view inherits from GenericView, so make sure you understand
that first by checking the docs above, or just by looking at the
source code of server/api/base.py.
"""

# Default attributes
route_prefix = '/v2/ws/<workspace_name>/'
Expand Down Expand Up @@ -420,6 +427,8 @@ class SortableMixin(object):
See the example of :ref:`pagination-and-sorting-recipe` to learn
how is it used.
Works for both workspaced and non-workspaced views.
"""
sort_field_paremeter_name = "sort"
sort_direction_paremeter_name = "sort_dir"
Expand Down Expand Up @@ -537,7 +546,7 @@ def _filter_query(self, query):


class ListWorkspacedMixin(ListMixin):
"""Add GET /<workspace_name>/ route"""
"""Add GET /<workspace_name>/<route_base>/ route"""
# There are no differences with the non-workspaced implementations. The code
# inside the view generic methods is enough
pass
Expand All @@ -552,7 +561,7 @@ def get(self, object_id, **kwargs):


class RetrieveWorkspacedMixin(RetrieveMixin):
"""Add GET /<workspace_name>/<id>/ route"""
"""Add GET /<workspace_name>/<route_base>/<id>/ route"""
# There are no differences with the non-workspaced implementations. The code
# inside the view generic methods is enough
pass
Expand All @@ -574,7 +583,10 @@ class ReadOnlyWorkspacedView(SortableMixin,
ListWorkspacedMixin,
RetrieveWorkspacedMixin,
GenericWorkspacedView):
"""A workspaced generic view with list and retrieve endpoints"""
"""A workspaced generic view with list and retrieve endpoints
It is just a GenericWorkspacedView inheriting also from
ListWorkspacedMixin, RetrieveWorkspacedMixin and SortableMixin"""
pass


Expand All @@ -593,6 +605,11 @@ def post(self, **kwargs):
return self._dump(created, kwargs), 201

def _perform_create(self, data, **kwargs):
"""Check for conflicts and create a new object
Is is passed the data parsed by the marshmallow schema (it
transform from raw post data to a JSON)
"""
obj = self.model_class(**data)
# assert not db.session.new
try:
Expand Down Expand Up @@ -660,7 +677,12 @@ def _set_command_id(self, obj, created):


class CreateWorkspacedMixin(CreateMixin, CommandMixin):
"""Add POST /<workspace_name>/ route"""
"""Add POST /<workspace_name>/<route_base>/ route
If a GET parameter command_id is passed, it will create a new
CommandObject associated to that command to register the change in
the database.
"""

def _perform_create(self, data, workspace_name):
assert not db.session.new
Expand Down Expand Up @@ -693,7 +715,7 @@ def _perform_create(self, data, workspace_name):


class UpdateMixin(object):
"""Add PUT /<workspace_name>/<id>/ route"""
"""Add PUT /<id>/ route"""

def put(self, object_id, **kwargs):
obj = self._get_object(object_id, **kwargs)
Expand Down Expand Up @@ -747,7 +769,12 @@ def _perform_update(self, object_id, obj, data, workspace_name=None):


class UpdateWorkspacedMixin(UpdateMixin, CommandMixin):
"""Add PUT /<id>/ route"""
"""Add PUT /<workspace_name>/<route_base>/<id>/ route
If a GET parameter command_id is passed, it will create a new
CommandObject associated to that command to register the change in
the database.
"""

def _perform_update(self, object_id, obj, data, workspace_name):
# # Make sure that if I created new objects, I had properly commited them
Expand All @@ -774,11 +801,21 @@ def _perform_delete(self, obj):


class DeleteWorkspacedMixin(DeleteMixin):
"""Add DELETE /<workspace_name>/<id>/ route"""
"""Add DELETE /<workspace_name>/<route_base>/<id>/ route"""
pass


class CountWorkspacedMixin(object):
"""Add GET /<workspace_name>/<route_base>/count/ route
Group objects by the field set in the group_by GET parameter. If it
isn't specified, the view will return a 404 error. For each group,
show the count of elements and its value.
This view is often used by some parts of the web UI. It was designed
to keep backwards compatibility with the count endpoint of Faraday
v2.
"""

#: List of SQLAlchemy query filters to apply when counting
count_extra_filters = []
Expand Down Expand Up @@ -825,7 +862,12 @@ class ReadWriteView(CreateMixin,
UpdateMixin,
DeleteMixin,
ReadOnlyView):
"""A generic view with list, retrieve and create endpoints"""
"""A generic view with list, retrieve and create endpoints
It is just a GenericView inheriting also from ListMixin,
RetrieveMixin, SortableMixin, CreateMixin, UpdateMixin and
DeleteMixin.
"""
pass


Expand All @@ -835,7 +877,13 @@ class ReadWriteWorkspacedView(CreateWorkspacedMixin,
CountWorkspacedMixin,
ReadOnlyWorkspacedView):
"""A generic workspaced view with list, retrieve and create
endpoints"""
endpoints
It is just a GenericWorkspacedView inheriting also from
ListWorkspacedMixin, RetrieveWorkspacedMixin, SortableMixin,
CreateWorkspacedMixin, DeleteWorkspacedMixin and
CountWorkspacedMixin.
"""
pass


Expand Down

0 comments on commit 3234853

Please sign in to comment.