falconjsonio


Namefalconjsonio JSON
Version 1.0.4 PyPI version JSON
download
home_pagehttps://bitbucket.org/garymonson/falcon-json-io
SummaryJSON-Schema input and output for Falcon
upload_time2023-06-16 02:32:45
maintainer
docs_urlNone
authorGary Monson
requires_python
licenseMIT
keywords json schema falcon
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Falcon JSON IO
==============

Validate HTTP request and response body by defining acceptable values
with JSON schema. For use with the `Falcon web
framework <http://falconframework.org/>`__.

Test status
-----------

|Codeship Status for garymonson/falcon-json-io|

Usage overview
--------------

Define your request body schema, and your endpoint is only called if the
request matches your specification. Otherwise, an error is returned to
the caller.

Define your response body schema, and your response is validated before
returning to the sender. A response that does not match the
specification will return a 500 error instead.

Retrieve your request JSON using req.context[‘doc’].

Set your JSON response in req.context[‘result’].

Using the middleware
--------------------

Enabled the middleware:

::

   app = falcon.API(
       middleware=[
           falconjsonio.middleware.RequireJSON(),
           falconjsonio.middleware.JSONTranslator(),
       ],
   )

Define your requirements:

::

   from falconjsonio.schema import request_schema, response_schema

   people_post_request_schema = {
       'type':       'object',
       'properties': {
           'title':  {'type': 'string'},
           'name':   {'type': 'string'},
           'dob':    {'type': 'date-time'},
           'email':  {'type': 'email'},
       },
       'required': ['name', 'dob'],
   }
   people_post_response_schema = {
       'oneOf': [
           {
               'type':       'object',
               'properties': {
                   'href': {'type': uri'},
               },
               'required': ['uri'],
           },
           {
               'type':       'object',
               'properties': {
                   'error': {'type': 'string'},
               },
               'required': ['error'],
           },
       ],
   }

   # ...

   class People(object):
       @response_schema(people_get_response_schema)
       def on_get(self, req, resp):
           # Put your JSON response here:
           req.context['result'] = {'some': 'json'}

       @request_schema(people_post_request_schema)
       @response_schema(people_post_response_schema)
       def on_post(self, req, resp):
           # JSON request supplied here:
           form = req.context['doc']
           # Put your JSON response here:
           req.context['result'] = {'some': 'json'}

Hook the endpoint in, of course:

::

   app.add_route('/people', People())

If your methods are inherited from a parent class, you can apply the
decorator to the resource class instead, and pass the method name to the
decorator:

::

   class People(object):
       def on_get(self, req, resp):
           # Put your JSON response here:
           req.context['result'] = {'some': 'json'}

       def on_post(self, req, resp):
           # JSON request supplied here:
           form = req.context['doc']
           # Put your JSON response here:
           req.context['result'] = {'some': 'json'}

   @response_schema(schema=people_get_response_schema, method_name='on_get')
   @request_schema(schema=people_post_request_schema, method_name='on_post')
   @response_schema(schema=people_post_response_schema, method_name='on_post')
   class ChildPeople(People):
       pass

This is especially useful when you have a parent class with all the
smarts, and your child classes merely declare a few settings for the
parent class (e.g.
`falcon-autocrud <https://pypi.python.org/pypi/falcon-autocrud>`__)

Quick start for contributing
----------------------------

::

   virtualenv -p `which python3` virtualenv
   source virtualenv/bin/activate
   pip install -r requirements.txt
   pip install -r dev_requirements.txt
   nosetests

.. |Codeship Status for garymonson/falcon-json-io| image:: https://codeship.com/projects/c370db70-b520-0133-3191-1af10c27659b/status?branch=master
   :target: https://codeship.com/projects/134051
            

Raw data

            {
    "_id": null,
    "home_page": "https://bitbucket.org/garymonson/falcon-json-io",
    "name": "falconjsonio",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "json schema falcon",
    "author": "Gary Monson",
    "author_email": "gary.monson@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/e3/4f/1ed41869343b2903935e2c9d12ddd3c93194ec0d163be15aba4697d8cf0b/falconjsonio-1.0.4.tar.gz",
    "platform": null,
    "description": "Falcon JSON IO\n==============\n\nValidate HTTP request and response body by defining acceptable values\nwith JSON schema. For use with the `Falcon web\nframework <http://falconframework.org/>`__.\n\nTest status\n-----------\n\n|Codeship Status for garymonson/falcon-json-io|\n\nUsage overview\n--------------\n\nDefine your request body schema, and your endpoint is only called if the\nrequest matches your specification. Otherwise, an error is returned to\nthe caller.\n\nDefine your response body schema, and your response is validated before\nreturning to the sender. A response that does not match the\nspecification will return a 500 error instead.\n\nRetrieve your request JSON using req.context[\u2018doc\u2019].\n\nSet your JSON response in req.context[\u2018result\u2019].\n\nUsing the middleware\n--------------------\n\nEnabled the middleware:\n\n::\n\n   app = falcon.API(\n       middleware=[\n           falconjsonio.middleware.RequireJSON(),\n           falconjsonio.middleware.JSONTranslator(),\n       ],\n   )\n\nDefine your requirements:\n\n::\n\n   from falconjsonio.schema import request_schema, response_schema\n\n   people_post_request_schema = {\n       'type':       'object',\n       'properties': {\n           'title':  {'type': 'string'},\n           'name':   {'type': 'string'},\n           'dob':    {'type': 'date-time'},\n           'email':  {'type': 'email'},\n       },\n       'required': ['name', 'dob'],\n   }\n   people_post_response_schema = {\n       'oneOf': [\n           {\n               'type':       'object',\n               'properties': {\n                   'href': {'type': uri'},\n               },\n               'required': ['uri'],\n           },\n           {\n               'type':       'object',\n               'properties': {\n                   'error': {'type': 'string'},\n               },\n               'required': ['error'],\n           },\n       ],\n   }\n\n   # ...\n\n   class People(object):\n       @response_schema(people_get_response_schema)\n       def on_get(self, req, resp):\n           # Put your JSON response here:\n           req.context['result'] = {'some': 'json'}\n\n       @request_schema(people_post_request_schema)\n       @response_schema(people_post_response_schema)\n       def on_post(self, req, resp):\n           # JSON request supplied here:\n           form = req.context['doc']\n           # Put your JSON response here:\n           req.context['result'] = {'some': 'json'}\n\nHook the endpoint in, of course:\n\n::\n\n   app.add_route('/people', People())\n\nIf your methods are inherited from a parent class, you can apply the\ndecorator to the resource class instead, and pass the method name to the\ndecorator:\n\n::\n\n   class People(object):\n       def on_get(self, req, resp):\n           # Put your JSON response here:\n           req.context['result'] = {'some': 'json'}\n\n       def on_post(self, req, resp):\n           # JSON request supplied here:\n           form = req.context['doc']\n           # Put your JSON response here:\n           req.context['result'] = {'some': 'json'}\n\n   @response_schema(schema=people_get_response_schema, method_name='on_get')\n   @request_schema(schema=people_post_request_schema, method_name='on_post')\n   @response_schema(schema=people_post_response_schema, method_name='on_post')\n   class ChildPeople(People):\n       pass\n\nThis is especially useful when you have a parent class with all the\nsmarts, and your child classes merely declare a few settings for the\nparent class (e.g.\n`falcon-autocrud <https://pypi.python.org/pypi/falcon-autocrud>`__)\n\nQuick start for contributing\n----------------------------\n\n::\n\n   virtualenv -p `which python3` virtualenv\n   source virtualenv/bin/activate\n   pip install -r requirements.txt\n   pip install -r dev_requirements.txt\n   nosetests\n\n.. |Codeship Status for garymonson/falcon-json-io| image:: https://codeship.com/projects/c370db70-b520-0133-3191-1af10c27659b/status?branch=master\n   :target: https://codeship.com/projects/134051",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "JSON-Schema input and output for Falcon",
    "version": "1.0.4",
    "project_urls": {
        "Homepage": "https://bitbucket.org/garymonson/falcon-json-io"
    },
    "split_keywords": [
        "json",
        "schema",
        "falcon"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e34f1ed41869343b2903935e2c9d12ddd3c93194ec0d163be15aba4697d8cf0b",
                "md5": "62c765a766b9a985fac6dd4c25237cfc",
                "sha256": "5a607eaa783c8ab1aedeeb28d3b6070a9c056a84e23a918608a0995a423f015b"
            },
            "downloads": -1,
            "filename": "falconjsonio-1.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "62c765a766b9a985fac6dd4c25237cfc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 9078,
            "upload_time": "2023-06-16T02:32:45",
            "upload_time_iso_8601": "2023-06-16T02:32:45.433368Z",
            "url": "https://files.pythonhosted.org/packages/e3/4f/1ed41869343b2903935e2c9d12ddd3c93194ec0d163be15aba4697d8cf0b/falconjsonio-1.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-16 02:32:45",
    "github": false,
    "gitlab": false,
    "bitbucket": true,
    "codeberg": false,
    "bitbucket_user": "garymonson",
    "bitbucket_project": "falcon-json-io",
    "lcname": "falconjsonio"
}
        
Elapsed time: 0.07530s