From 5b1a3b314f2c9d46763b9c26efcc6eb3d38de99d Mon Sep 17 00:00:00 2001 From: Alexis Metaireau Date: Wed, 7 Dec 2011 10:06:11 +0100 Subject: [PATCH] update cornice blogpost --- content/mozilla/introducing-cornice.rst | 49 ++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/content/mozilla/introducing-cornice.rst b/content/mozilla/introducing-cornice.rst index f46dc51..8e7048b 100644 --- a/content/mozilla/introducing-cornice.rst +++ b/content/mozilla/introducing-cornice.rst @@ -3,14 +3,53 @@ Introducing cornice :date: 06/12/2011 -Wow, this is my second working day at mozilla. I've been working, yesterday and -today, on a pyramid REST-ish toolkit, `cornice `_. +Wow, already my third working day at mozilla. Since Monday, I've been working with +Tarek Ziadé, on a pyramid REST-ish toolkit named `cornice `_. -Cornice allows you to take out from you all the repetitive stuff you do when -writing a web service. I'm mainly thinking about different kinds of validation. +Its goal is to take all the hard bits appart from you when implementing a web +service, so you can focus on what's important. Cornice provides you facilities +for validation of any kind. + +The goal is to simplify your work, but we don't want to reinvent the wheel, so +it is easily pluggable with validations frameworks, such as Collander. + +Handling errors and validation +============================== + +We have changed the way errors are handled. Here is how it works: + +.. code-block:: python + + service = Service(name="service", path="/service") + + + def is_awesome(request): + if not 'awesome' in request.GET: + request.errors.add('body', 'awesome', 'You lack awesomeness!') + + + @service.get(validator=(is_awesome)) + def get1(request): + return {"test": "succeeded"} + + +All the errors collected during the validation process, or after, are collected +before returning the request. If any, a error 400 is fired up, with the list of +problems encoutred encoded as a nice json list (we plan to support multiple +formats in the future) + +As you might have seen, `request.errors.add` takes three parameters: **location**, +**name** and **description**. + +**location** is where the error arised. It can either be "body", "query", "headers" +or "path". **name** is the name of the variable causing problem, if any, and +**description** contains a more detailled message. + +Here is an example of a malformed request:: + + $ # run a demo app To describe a web service in *cornice*, you have to write something like this .. code-block:: python -