Skip to content

Commit

Permalink
completed docs with expected curl headers and output
Browse files Browse the repository at this point in the history
  • Loading branch information
martinblech committed Jul 2, 2012
1 parent 4fb1116 commit 4bf1107
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 34 deletions.
37 changes: 25 additions & 12 deletions docs/examples.rst
Original file line number Diff line number Diff line change
@@ -1,36 +1,49 @@
Usage Examples
==============

A few examples on how to use :mod:`mimerender` with the different supported frameworks.
Content-Type selection
----------------------

A few examples on how to use :mod:`mimerender` with the different supported frameworks. Any of these will behave this way:

.. literalinclude:: ../examples/bottle_example.py
:language: bash
:lines: 3-25

Bottle
------
++++++

.. literalinclude:: ../examples/bottle_example.py
:lines: 8-
:lines: 28-

Flask
-----
+++++

.. literalinclude:: ../examples/flask_example.py
:lines: 8-
:lines: 28-

Webapp2
-------
+++++++

.. literalinclude:: ../examples/webapp2_example.py
:lines: 8-
:lines: 28-

web.py
------
++++++

.. literalinclude:: ../examples/webpy_example.py
:lines: 8-
:lines: 28-

Exception Mapping
-----------------
Content-Type selection plus Exception Mapping
---------------------------------------------

:mod:`mimerender` provides a helper decorator for mapping exceptions to HTTP Status Codes.

.. literalinclude:: ../examples/exception_mapping_example.py
:lines: 11-
:lines: 47-

:mod:`mimerender` will take care of mapping :class:`ValueError` and :class:`NotFound` to the specified HTTP status codes, and it will serialize the exception with an acceptable Content-Type:

.. literalinclude:: ../examples/exception_mapping_example.py
:language: bash
:lines: 3-45
28 changes: 24 additions & 4 deletions examples/bottle_example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
"""mimerender example for Bottle. Run this server and then try:
$ curl -H "Accept: application/html" localhost:8080/x
$ curl -H "Accept: application/xml" localhost:8080/x
$ curl -H "Accept: application/json" localhost:8080/x
$ curl -H "Accept: text/plain" localhost:8080/x
$ curl -iH "Accept: application/html" localhost:8080/x
...
Content-Type: text/html
...
<html><body>Hello, x!</body></html>
$ curl -iH "Accept: application/xml" localhost:8080/x
...
Content-Type: application/xml
...
<message>Hello, x!</message>
$ curl -iH "Accept: application/json" localhost:8080/x
...
Content-Type: application/json
...
{"message": "Hello, x!"}
$ curl -iH "Accept: text/plain" localhost:8080/x
...
Content-Type: text/plain
...
Hello, x!
"""
from bottle import Bottle, run
try:
Expand Down
48 changes: 42 additions & 6 deletions examples/exception_mapping_example.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,48 @@
"""mimerender exception mapping example for webapp2. Run this server and
then try:
$ curl -H "Accept: application/xml" localhost:8080/1
$ curl -H "Accept: application/xml" localhost:8080/2
$ curl -H "Accept: application/xml" localhost:8080/abc
$ curl -H "Accept: application/json" localhost:8080/1
$ curl -H "Accept: application/json" localhost:8080/2
$ curl -H "Accept: application/json" localhost:8080/abc
$ curl -iH "Accept: application/xml" localhost:8080/1
HTTP/1.0 200 OK
...
Content-Type: application/xml
...
<message>found it!</message>
$ curl -iH "Accept: application/xml" localhost:8080/2
HTTP/1.0 404 Not Found
...
Content-Type: application/xml
...
<exception>could not find item with id = 2</exception>
$ curl -iH "Accept: application/xml" localhost:8080/abc
HTTP/1.0 500 Internal Server Error
...
Content-Type: application/xml
...
<exception>invalid literal for int() with base 10: 'abc'</exception>
$ curl -iH "Accept: application/json" localhost:8080/1
HTTP/1.0 200 OK
...
Content-Type: application/json
...
{"message": "found it!"}
$ curl -iH "Accept: application/json" localhost:8080/2
HTTP/1.0 404 Not Found
...
Content-Type: application/json
...
["could not find item with id = 2"]
$ curl -iH "Accept: application/json" localhost:8080/abc
HTTP/1.0 500 Internal Server Error
...
Content-Type: application/json
...
["invalid literal for int() with base 10: 'abc'"]
"""
import webapp2
try:
Expand Down
28 changes: 24 additions & 4 deletions examples/flask_example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
"""mimerender example for Flask. Run this server and then try:
$ curl -H "Accept: application/html" localhost:8080/x
$ curl -H "Accept: application/xml" localhost:8080/x
$ curl -H "Accept: application/json" localhost:8080/x
$ curl -H "Accept: text/plain" localhost:8080/x
$ curl -iH "Accept: application/html" localhost:8080/x
...
Content-Type: text/html
...
<html><body>Hello, x!</body></html>
$ curl -iH "Accept: application/xml" localhost:8080/x
...
Content-Type: application/xml
...
<message>Hello, x!</message>
$ curl -iH "Accept: application/json" localhost:8080/x
...
Content-Type: application/json
...
{"message": "Hello, x!"}
$ curl -iH "Accept: text/plain" localhost:8080/x
...
Content-Type: text/plain
...
Hello, x!
"""
from flask import Flask
try:
Expand Down
28 changes: 24 additions & 4 deletions examples/webapp2_example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
"""mimerender example for webapp2. Run this server and then try:
$ curl -H "Accept: application/html" localhost:8080/x
$ curl -H "Accept: application/xml" localhost:8080/x
$ curl -H "Accept: application/json" localhost:8080/x
$ curl -H "Accept: text/plain" localhost:8080/x
$ curl -iH "Accept: application/html" localhost:8080/x
...
Content-Type: text/html
...
<html><body>Hello, x!</body></html>
$ curl -iH "Accept: application/xml" localhost:8080/x
...
Content-Type: application/xml
...
<message>Hello, x!</message>
$ curl -iH "Accept: application/json" localhost:8080/x
...
Content-Type: application/json
...
{"message": "Hello, x!"}
$ curl -iH "Accept: text/plain" localhost:8080/x
...
Content-Type: text/plain
...
Hello, x!
"""
import webapp2
try:
Expand Down
28 changes: 24 additions & 4 deletions examples/webpy_example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
"""mimerender example for web.py. Run this server and then try:
$ curl -H "Accept: application/html" localhost:8080/x
$ curl -H "Accept: application/xml" localhost:8080/x
$ curl -H "Accept: application/json" localhost:8080/x
$ curl -H "Accept: text/plain" localhost:8080/x
$ curl -iH "Accept: application/html" localhost:8080/x
...
Content-Type: text/html
...
<html><body>Hello, x!</body></html>
$ curl -iH "Accept: application/xml" localhost:8080/x
...
Content-Type: application/xml
...
<message>Hello, x!</message>
$ curl -iH "Accept: application/json" localhost:8080/x
...
Content-Type: application/json
...
{"message": "Hello, x!"}
$ curl -iH "Accept: text/plain" localhost:8080/x
...
Content-Type: text/plain
...
Hello, x!
"""
import web
try:
Expand Down

0 comments on commit 4bf1107

Please sign in to comment.