Cornice Web Services for Pyramid
Cornice is a Python library that simplifies the definition of RESTful web services within the Pyramid web framework. It allows developers to declare services, resources, and their methods using decorators and schema validation, often integrating with tools like Colander and Marshmallow. The current version is 6.1.0, and it maintains an active release cadence, frequently updating for Python and Pyramid compatibility and addressing dependencies.
Common errors
-
ImportError: cannot import name 'Service' from 'cornice'
cause Incorrect import path; 'Service' is directly available in the top-level 'cornice' package, not a submodule.fixChange the import to `from cornice import Service`. -
AttributeError: 'Service' object has no attribute 'add_get'
cause Attempting to use older, deprecated `add_` methods (e.g., `add_get`, `add_post`) instead of the decorator syntax for defining service methods.fixUse the decorator syntax for defining service methods, e.g., `@service.get()` or `@service.post()`. -
TypeError: 'Request' object is not subscriptable
cause Attempting to access request attributes like `request['json']` or `request['validated']` using dictionary-style bracket notation.fixAccess request data using dot notation: `request.json` or `request.validated`. -
colander.Invalid: ... missing or invalid fields ...
cause Validation failed because the request body or parameters do not conform to the defined Colander schema. This often indicates missing required fields or incorrect data types.fixReview your Colander schema definition and ensure the incoming request data (e.g., JSON body, query parameters) matches the expected structure and types. Check logs for specific field errors.
Warnings
- breaking Cornice 6.0.0 dropped support for Marshmallow versions older than 3. Attempting to use an older Marshmallow version will lead to compatibility issues or errors.
- breaking Cornice 5.0.0 dropped support for Python 2.x. Applications running on Python 2 will not be compatible with Cornice versions 5.0.0 and above.
- gotcha In Cornice 5.0.2, the internal default JSON renderer name changed from `cornice` to `cornicejson`. While this change was not officially documented as a breaking change, applications explicitly relying on or configuring a renderer named 'cornice' might experience issues.
- gotcha Prior to Cornice 5.0.3, Colander validators (e.g., `body_validator`) might incorrectly miss `body`, `headers`, `path`, or `querystring` data from the request, leading to incomplete validation.
Install
-
pip install cornice
Imports
- Service
from cornice.service import Service
from cornice import Service
- resource
from cornice.resource import resource
- validators
from cornice import validators
Quickstart
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from cornice import Service
# 1. Define a Cornice service
hello_service = Service(name='hello', path='/hello', description="Hello World Service")
@hello_service.get()
def get_hello(request):
"""Returns a 'Hello World!' message."""
# Access request data via request.GET, request.json, request.validated, etc.
return {'message': 'Hello World! from Cornice!'}
# 2. Integrate into a Pyramid application
if __name__ == '__main__':
with Configurator() as config:
# Include the cornice package to register its components
config.include('cornice')
# Add the defined Cornice service to the Pyramid configuration
config.add_cornice_service(hello_service)
# Create a WSGI application
app = config.make_wsgi_app()
# 3. Run the Pyramid application
server = make_server('0.0.0.0', 6543, app)
print('Serving on http://0.0.0.0:6543/hello')
print('Press Ctrl+C to quit')
try:
server.serve_forever()
except KeyboardInterrupt:
pass
finally:
server.server_close()