Skip to main content

Define Web Services in Pyramid.

Project description

Overview

Cornice provides a Service class you can use to define web services in Pyramid.

Each instance of a Service class corresponds to a server path and you may implement various methods HTTP on the path with simple decorators.

Cornice will automatically return a 405 error with the right Allow header if a method that was not implemented is requested.

Cornice also provides a Sphinx directive that can be used to document your web services. The extension iterates over defined services and will automatically generate your web service documentation.

QuickStart

To use Cornice, start by including it in your project with the include method in Pyramid:

def main(global_config, **settings):
    ...
    config.include("cornice")
    ...
    return config.make_wsgi_app()

Learn more about include at http://docs.pylonsproject.org/projects/pyramid/1.0/narr/advconfig.html#including-configuration-from-external-sources

Then you can start to define web services in your views.

For example, let’s define a service where you can GET and POST a value at /values/{value}, where value is an ascii value representing the name of the value:

import json
from cornice import Service

values = Service(name='foo', path='/values/{value}',
                 description="Cornice Demo")

_VALUES = {}


@values.get()
def get_value(request):
    """Returns the value.
    """
    key = request.matchdict['value']
    return _VALUES.get(key)


@values.post()
def set_value(request):
    """Set the value.

    Returns *True* or *False*.
    """
    key = request.matchdict['value']
    try:
        _VALUES.set(key, json.loads(request.body))
    except ValueError:
        return False
    return True

By default, Cornice uses a Json rendered.

0.3 - 2011-11-23

  • remove singleton “_defined” state from Service class; this allows service definitions to be loaded into more than one Configurator.

0.2 - 2011-11-05

  • Fixed the MANIFEST

0.1 - 2011-11-03

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cornice-0.3.tar.gz (7.6 kB view details)

Uploaded Source

File details

Details for the file cornice-0.3.tar.gz.

File metadata

  • Download URL: cornice-0.3.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for cornice-0.3.tar.gz
Algorithm Hash digest
SHA256 df76ffe1c4b8b3a6d74a2a5bfdd17bca60953fcfe89e31133cdbb90579558542
MD5 063baf02f3e35009bbbb0a012be64144
BLAKE2b-256 9066aaded9bd8c87098970e10e235aea86d63fc20bbb7b25e5f521153977a19f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page