-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
173 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# API Reference | ||
|
||
## FlaskMuckApiView | ||
::: flask_muck.FlaskMuckApiView |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Configuration | ||
|
||
Configuration is handled entirely through setting class variables on the FlaskMuckApiViews. As noted in the | ||
[quickstart](quickstart.md) you will likely have some class variable settings that will be shared by most or all of your | ||
view classes. It's advised to set up base classes to handle sharing configuration between views. | ||
|
||
## FlaskMuckApiView Class Variables | ||
|
||
| Class Variable | Description | Required | | ||
|:------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------:| | ||
| session `scoped_session` | SqlAlchemy database session used to query and modify the resource. | :octicons-check-circle-16: | | ||
| api_name `str` | Name of the API. Used as the url path appended to your Flask Blueprint. | :octicons-check-circle-16: | | ||
| Model `SqlaModelType` | SqlAlchemy Model used to make queries. | :octicons-check-circle-16: | | ||
| ResponseSchema `type[Schema]` | Marshmallow schema used to serialize the resource returned by any of the views. | :octicons-check-circle-16: | | ||
| decorators `list[Callable]` | List of decorators to apply to all views in the API. This is inherited functionality built into Flask's [class based views](https://flask.palletsprojects.com/en/2.3.x/views/#view-decorators). | | | ||
| parent `Optional[type[FlaskMuckApiView]]` | If set, this API becomes a nested resource API. For more information on nested APIs see the [documentation](nesting_apis.md). | | | ||
| CreateSchema `Optional[type[Schema]]` | Marshmallow schema used to validate the POST request JSON payload sent to create a resource. | | | ||
| UpdateSchema `Optional[type[Schema]]` | Marshmallow schema used to validate the PUT request JSON payload sent to update a resource. | | | ||
| PatchSchema `Optional[type[Schema]]` | Marshmallow schema used to validate the PATCH request JSON payload sent to patch a resource. | | | ||
| DeleteSchema `Optional[type[Schema]]` | Marshmallow schema used to validate the DELETE request JSON payload sent to create a resource. Optional. | | | ||
| DetailSchema `Optional[type[Schema]]` | Optional Marshmallow schema used to serialize the resource returned by the GET /<api_name\>/<ID\>/ endpoint. If this schema is not set the ResponseSchema is used. | | | ||
| pre_create_callbacks `list[type[FlaskMuckCallback]]` | List of callback classes to be called before a resource is created. Ideal for validation. | | | ||
| pre_update_callbacks `list[type[FlaskMuckCallback]]` | List of callback classes to be called before a resource is updated. Ideal for validation. | | | ||
| pre_patch_callbacks `list[type[FlaskMuckCallback]]` | List of callback classes to be called before a resource is patched. Ideal for validation. | | | ||
| pre_delete_callbacks `list[type[FlaskMuckCallback]]` | List of callback classes to be called before a resource is deleted. Ideal for validation. | | | ||
| post_create_callbacks `list[type[FlaskMuckCallback]]` | List of callback classes to be called after a resource is created. Useful for activities such as notifications. Called post commit. | | | ||
| post_update_callbacks `list[type[FlaskMuckCallback]]` | List of callback classes to be called after a resource is updated. Useful for activities such as notifications. Called post commit. | | | ||
| post_patch_callbacks `list[type[FlaskMuckCallback]]` | List of callback classes to be called after a resource is patched. Useful for activities such as notifications. Called post commit. | | | ||
| post_delete_callbacks `list[type[FlaskMuckCallback]]` | List of callback classes to be called after a resource is deleted. Useful for activities such as notifications. Called post commit. | | | ||
| searchable_columns `list[InstrumentedAttribute]` | List of Model columns that will be queried using an "ILIKE" statement when the `search=` query param is used on the GET /resource/ endpoint. | | | ||
| default_pagination_limit `int` | Default pagination limit when retrieving paginated results on the GET /<api_name\>/ endpoint. Default is 20. | | | ||
| one_to_one_api `bool` | If True this API is treated as a one-to-one relationship and the GET /<api_name\>/ endpoint will return a single resource. Generally used in combination with the `parent` setting. | | | ||
| allowed_methods `set[str]` | Set of allowed HTTP methods for this API. Default is `{"GET", "POST", "PUT", "PATCH", "DELETE"}`. This setting is used to control which actions are available for this resource. Not including a method affects which routes will be registered to a Flask Blueprint. | | | ||
| operator_separator `str` | Separator used when assigning operators to search or filter query parameters in the GET /<api_name\>/ endpoint. Default is `"__"`. | | | ||
|
||
## Base Class Example | ||
|
||
Let's say you have an API with the following requirements for all views: | ||
|
||
- Has authentication decorator. | ||
- Has permission checking decorator. | ||
- Does not allow patches. | ||
- Uses "|" as the operator separator for filters. | ||
- Uses the standard database session. | ||
|
||
The best way to handle this is to create a base API view that all other API views inherit from. | ||
|
||
```python title="Base Class" | ||
class BaseApiView(FlaskMuckApiView): | ||
session = db.session | ||
decorators = [login_required, permission_required] | ||
allowed_methods = {"GET", "POST", "PUT", "DELETE"} | ||
operator_separator = "|" | ||
``` | ||
|
||
```python title="Concrete Class" | ||
class TurtleApiView(BaseApiView):#(1)! | ||
api_name = 'turtles' | ||
Model = Turtle | ||
...#(2)! | ||
``` | ||
|
||
1. Concrete view inherits from the BaseApiView and ingerits all of its configuration. | ||
2. Remainder of class variables are configured as normal. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from .views import FlaskMuckApiView | ||
from .callback import MuckCallback | ||
from .callback import FlaskMuckCallback | ||
|
||
VERSION = "0.0.3b2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters