Util
Utility functions and decorators for odin-control API adapters.
This module provides common decorators and utility functions used by adapters for request/response type validation and controller management.
Tim Nicholls, STFC Detector System Software Group
request_types(*oargs)
Ensure that a request has a legal content types that an adapter method will accept.
This decorator method compares the HTTP Content-Type header with a list of acceptable types. If there is a match, the adapter method is called accordingly, otherwise an HTTP 415 error response is returned.
Typical usage would be, in an adapter, to decorate a verb method as follows:
@request_types('application/json') def get(self, path, request)
Note that both the request_types and response_types decorators can be applied to a method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
oargs
|
a variable length list of acceptable content types |
()
|
Returns:
| Type | Description |
|---|---|
|
decorator context |
Source code in src/odin_control/adapters/util.py
require_controller(func)
Ensure the adapter has a valid controller before executing HTTP methods.
This decorator checks if the adapter instance has a valid controller object in self.controller. If not, it returns a JSON error response with status 405.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
The HTTP method function to decorate |
required |
Returns:
| Type | Description |
|---|---|
|
Decorated function that validates controller presence |
Source code in src/odin_control/adapters/util.py
response_types(*oargs, **okwargs)
Ensure that a request wants legal response types for an adapter method.
This decorator method compares the HTTP Accept header with a list of acceptable response types. If there is a match, the response type is set accordingly, otherwise an HTTP 406 error code is returned. A default type is also allowable, so if the request fails to specify a type (e.g. '/') then this will be used.
Typical usage for this would be, in an adapter, to decorate a verb method as follows:
@response_type('application/json', 'text/html', default='text/html')
def get(self, path, request):
to specify that the method has acceptable resonse types of JSON, HTML, defaulting to HTML
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
oargs
|
a variable length list of acceptable response types |
()
|
|
okwargs
|
keyword argument(s), allowing default type to be specified. |
{}
|
Returns:
| Type | Description |
|---|---|
|
decorator context |
Source code in src/odin_control/adapters/util.py
wants_metadata(request)
Determine if a client request wants metadata to be included in the response.
This method checks to see if an incoming request has an Accept header with the 'metadata=true' qualifier attached to the MIME-type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
HTTPServerRequest or equivalent from client |
required |
Returns:
| Type | Description |
|---|---|
|
boolean, True if metadata is requested. |
Source code in src/odin_control/adapters/util.py
wrap_result(result, is_async=True)
Conditionally wrap a result in an aysncio Future if being used in async code.
This method allows common functions for e.g. request validation, to be used in both async and sync adapters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
the result to potentially wrap |
required | |
is_async
|
optional flag for if desired outcome is a result wrapped in a future |
True
|
Returns:
| Type | Description |
|---|---|
|
either the result or a Future wrapping the result |