Moved example extension and switched to includes

This commit is contained in:
jtc42 2020-01-22 15:17:31 +00:00
parent d2780f5541
commit 1115d00a18
12 changed files with 117 additions and 397 deletions

View file

@ -9,82 +9,7 @@ As with most HTTP APIs, we make use of basic HTTP request methods. GET requests
Continuing our example on the previous page, and discussed below, adding API views may look like:
.. code-block:: python
from labthings.server.extensions import BaseExtension
from labthings.server.find import find_component
from labthings.server.view import View
from labthings.server.decorators import use_body
from labthings.server import fields
## Extension methods
def identify(microscope):
"""
Demonstrate access to Microscope.camera, and Microscope.stage
"""
response = (
f"My name is {microscope.name}. "
f"My parent camera is {microscope.camera}, "
f"and my parent stage is {microscope.stage}."
)
return response
def rename(microscope, new_name):
"""
Rename the microscope
"""
microscope.name = new_name
microscope.save_settings()
## Extension views
class ExampleIdentifyView(View):
def get(self):
# Find our microscope component
microscope = find_component("org.openflexure.microscope")
# Return our identify function's output
return identify(microscope)
class ExampleRenameView(View):
# Expect a request parameter called "name", which is a string. Pass to argument "args".
@use_body(fields.String(required=True, example="My Example Microscope"))
def post(self, body):
# Look for our new name in the request body
new_name = body
# Find our microscope component
microscope = find_component("org.openflexure.microscope")
# Pass microscope and new name to our rename function
rename(microscope, new_name)
# Return our identify function's output
return identify(microscope)
## Create extension
# Create your extension object
my_extension = BaseExtension("com.myname.myextension", version="0.0.0")
# Add methods to your extension
my_extension.add_method(identify, "identify")
my_extension.add_method(rename, "rename")
# Add API views to your extension
my_extension.add_view(ExampleIdentifyView, "/identify")
my_extension.add_view(ExampleRenameView, "/rename")
.. literalinclude:: ./example_extension/02_adding_views.py
Note that we are now passing our microscope object as an argument to our API methods. Finding the microscope component is performed by the API view at request-time, and passed onto the functions.
@ -99,7 +24,7 @@ For POST and PUT requests, data usually needs to be provided to the view in orde
Adding additional fields, and the meaning of the field types, will be discussed further in the next section.
When a POST request is made to our API view, the request body is converted to processed by `@use_body``, and passed as a positional argument to our ``post`` function.
When a POST request is made to our API view, the request body is converted to processed by ``@use_body``, and passed as a positional argument to our ``post`` function.
Swagger documentation
+++++++++++++++++++++