{ "info": { "author": "Mark Vartanyan", "author_email": "kolypto@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "Natural Language :: English", "Operating System :: OS Independent", "Programming Language :: Python :: 2", "Programming Language :: Python :: 3", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "|Build Status|\n\nFlask JsonTools\n===============\n\nJSON API tools for Flask\n\nTable of Contents\n=================\n\n- View Utilities\n\n - @jsonapi\n\n - JsonResponse\n - make\\_json\\_response()\n\n- FlaskJsonClient\n- Class-Based Views\n\n - MethodView\n - RestfulView\n\nView Utilities\n==============\n\n@jsonapi\n--------\n\nDecorate a view function that talks JSON.\n\nSuch function can return:\n\n- tuples of ``(response, status[, headers])``: to set custom status\n code and optionally - headers\n- Instances of ```JsonResponse`` <#jsonresponse>`__\n- The result of helper function\n ```make_json_response`` <#make_json_response>`__\n\nExample:\n\n.. code:: python\n\n from flask.ext.jsontools import jsonapi\n\n @app.route('/users')\n @jsonapi\n def list_users():\n return [\n {'id': 1, 'login': 'kolypto'},\n #...\n ]\n\n @app.route('/user/', methods=['DELETE'])\n def delete_user(id):\n return {'error': 'Access denied'}, 403\n\nJsonResponse\n~~~~~~~~~~~~\n\nExtends\n```flask.Request`` `__\nand encodes the response with JSON. Views decorated with\n```@jsonapi`` <#jsonapi>`__ return these objects.\n\nArguments:\n\n- ``response``: response data\n- ``status``: status code. Optional, defaults to 200\n- ``headers``: additional headers dict. Optional.\n- ``**kwargs``: additional argumets for\n ```Response`` `__\n\nMethods:\n\n- ``preprocess_response_data(response)``: Override to get custom\n response behavior.\n- ``get_json()``: Get the original response data.\n- ``__getitem__(key)``: Get an item from the response data\n\nThe extra methods allows to reuse views:\n\n.. code:: python\n\n from flask.ext.jsontools import jsonapi\n\n @app.route('/user', methods=['GET'])\n @jsonapi\n def list_users():\n return [ { 1: 'first', 2: 'second' } ]\n\n @app.route('/user/', methods=['GET'])\n @jsonapi\n def get_user(id):\n return list_users().get_json()[id] # Long form\n return list_users()[id] # Shortcut\n\nmake\\_json\\_response()\n~~~~~~~~~~~~~~~~~~~~~~\n\nHelper function that actually preprocesses view return value into\n```JsonResponse`` <#jsonresponse>`__.\n\nAccepts ``rv`` as any of:\n\n- tuple of ``(response, status[, headers])``\n- Object to encode as JSON\n\nFlaskJsonClient\n===============\n\nFlaskJsonClient is a JSON-aware test client: it can post JSON and parse\nJSON responses into ```JsonResponse`` <#jsonresponse>`__.\n\n.. code:: python\n\n from myapplication import Application\n from flask.ext.jsontools import FlaskJsonClient\n\n def JsonTest(unittest.TestCase):\n def setUp(self):\n self.app = Application(__name__)\n self.app.test_client_class = FlaskJsonClient\n\n def testCreateUser(self):\n with self.app.test_client() as c:\n rv = c.post('/user/', json={'name': 'kolypto'})\n # rv is JsonResponse\n rv.status_code\n rv.get_json()['user'] # Long form for the previous\n rv['user'] # Shortcut for the previous\n\nFormatting Utils\n================\n\nDynamicJSONEncoder\n------------------\n\nIn python, de-facto standard for encoding objects of custom classes is\nthe ``__json__()`` method which returns the representation of the\nobject.\n\n``DynamicJSONEncoder`` is the implementation of this protocol: if an\nobject has the ``__json__()`` method, its result if used for the\nrepresentation.\n\nYou'll definitely want to subclass it to support other types, e.g. dates\nand times:\n\n.. code:: python\n\n from flask.ext.jsontools import DynamicJSONEncoder\n\n class ApiJSONEncoder(DynamicJSONEncoder):\n def default(self, o):\n # Custom formats\n if isinstance(o, datetime.datetime):\n return o.isoformat(' ')\n if isinstance(o, datetime.date):\n return o.isoformat()\n if isinstance(o, set):\n return list(o)\n\n # Fallback\n return super(DynamicJSONEncoder, self).default(o)\n\nNow, just install the encoder to your Flask:\n\n.. code:: python\n\n from flask import Flask\n\n app = Flask(__name__)\n app.json_encoder = DynamicJSONEncoder\n\nJsonSerializableBase\n--------------------\n\nSerializing SqlAlchemy models to JSON is a headache: if an attribute is\npresent on an instance, this does not mean it's loaded from the\ndatabase.\n\n``JsonSerializableBase`` is a mixin for SqlAlchemy Declarative Base that\nadds a magic ``__json__()`` method, compatible with\n```DynamicJSONEncoder`` <#dynamicjsonencoder>`__. When serializing, it\nmakes sure that entity serialization will *never* issue additional\nrequests.\n\nExample:\n\n.. code:: python\n\n from sqlalchemy.ext.declarative import declarative_base\n from flask.ext.jsontools import JsonSerializableBase\n\n Base = declarative_base(cls=(JsonSerializableBase,))\n\n class User(Base):\n #...\n\nNow, you can safely respond with SqlAlchemy models in your JSON views,\nand jsontools will handle the rest :)\n\nClass-Based Views\n=================\n\nModule ``flask.ext.jsontools.views`` contains a couple of classes that\nallow to build class-based views which dispatch to different methods.\n\nIn contrast to\n`MethodView `__,\nthis gives much higher flexibility.\n\nMethodView\n----------\n\nUsing ``MethodView`` class for methods, decorate them with\n``@methodview()``, which takes the following arguments:\n\n- ``methods=()``: Iterable of HTTP methods to use with this method.\n- ``ifnset=None``: Conditional matching. List of route parameter names\n that should *not* be set for this method to match.\n- ``ifset=None``: Conditional matching. List of route parameter names\n that should be set for this method to match.\n\nThis allows to map HTTP methods to class methods, and in addition define\nwhen individual methods should match.\n\nQuick example:\n\n.. code:: python\n\n from flask.ext.jsontools import jsonapi, MethodView, methodview\n\n class UserView(MethodView):\n # Canonical way to specify decorators for class-based views\n decorators = (jsonapi, )\n\n @methodview\n def list(self):\n \"\"\" List users \"\"\"\n return db.query(User).all()\n\n @methodview\n def get(self, user_id):\n \"\"\" Load a user by id \"\"\"\n return db.query(User).get(user_id)\n\n userview = CrudView.as_view('user')\n app.add_url_rule('/user/', view_func=userview)\n app.add_url_rule('/user/', view_func=userview)\n\nNow, ``GET`` HTTP method is routed to two different methods depending on\nconditions. Keep defining more methods to get good routing :)\n\nTo simplify the last step of creating the view, there's a helper:\n\n.. code:: python\n\n UserView.route_as_view(app, 'user', ('/user/', '/user/'))\n\nRestfulView\n-----------\n\nSince ``MethodView`` is mostly useful to expose APIs over collections of\nentities, there is a RESTful helper which automatically decorates some\nspecial methods with ``@methodview``.\n\n+---------------+---------------+-------------+\n| View method | HTTP method | URL |\n+===============+===============+=============+\n| list() | GET | ``/`` |\n+---------------+---------------+-------------+\n| create() | POST | ``/`` |\n+---------------+---------------+-------------+\n| get() | GET | ``/`` |\n+---------------+---------------+-------------+\n| replace() | PUT | ``/`` |\n+---------------+---------------+-------------+\n| update() | POST | ``/`` |\n+---------------+---------------+-------------+\n| delete() | DELETE | ``/`` |\n+---------------+---------------+-------------+\n\nBy subclassing ``RestfulView`` and implementing some of these methods,\nyou'll get a complete API endpoint with a single class.\n\nIt's also required to define the list of primary key fields by defining\nthe ``primary_key`` property:\n\n.. code:: python\n\n from flask.ext.jsontools import jsonapi, RestfulView\n\n class User(RestfulView):\n decorators = (jsonapi, )\n primary_key = ('id',)\n\n #region Operation on the collection\n\n def list():\n return db.query(User).all()\n\n def create():\n db.save(user)\n return user\n\n #endregion\n\n #region Operation on entities\n\n def get(id):\n return db.query(User).get(id)\n\n def replace(id):\n db.save(user, id)\n\n def update(id):\n db.save(user)\n\n def delete(id):\n db.delete(user)\n\n #endregion\n\nWhen a class like this is defined, its metaclass goes through the\nmethods and decorates them with ``@methodview``. This way, ``list()``\ngets ``@methodview('GET', ifnset=('id',))``, and ``get()`` gets\n``@methodview('GET', ifset=('id',))``.\n\n.. |Build Status| image:: https://api.travis-ci.org/kolypto/py-flask-jsontools.png?branch=master\n :target: https://travis-ci.org/kolypto/py-flask-jsontools\n\n\n", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/slippers/py-flask-jsontools", "keywords": "flask,json,sqlalchemy", "license": "BSD", "maintainer": "", "maintainer_email": "", "name": "flask-jsontools-slippers", "package_url": "https://pypi.org/project/flask-jsontools-slippers/", "platform": "any", "project_url": "https://pypi.org/project/flask-jsontools-slippers/", "project_urls": { "Homepage": "https://github.com/slippers/py-flask-jsontools" }, "release_url": "https://pypi.org/project/flask-jsontools-slippers/0.1.6/", "requires_dist": [ "click (==6.7)", "flask (==0.12.1)", "itsdangerous (==0.24)", "jinja2 (==2.9.6)", "markupsafe (==1.0)", "werkzeug (==0.12.1)" ], "requires_python": "", "summary": "JSON API tools for Flask", "version": "0.1.6" }, "last_serial": 2899098, "releases": { "0.1.6": [ { "comment_text": "", "digests": { "md5": "624d6da47fb8895f70a40060b782e25e", "sha256": "1a538418ebcaa0b2140a7eadae982158db92f84aec49dc2f48f5fb7533bd69f1" }, "downloads": -1, "filename": "flask_jsontools_slippers-0.1.6-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "624d6da47fb8895f70a40060b782e25e", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 20520, "upload_time": "2017-05-25T21:06:21", "url": "https://files.pythonhosted.org/packages/6c/1d/12f5448d3543ab0dd0e90428aa6f8164ef79996a0e543ef7c4cc00bfd487/flask_jsontools_slippers-0.1.6-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "fff1744de8106955cba1123e4e75897a", "sha256": "63717b875c7159ca4de6aec31bf203ccf8137edaaf0e5851f61093bfb231057b" }, "downloads": -1, "filename": "flask_jsontools_slippers-0.1.6.tar.gz", "has_sig": false, "md5_digest": "fff1744de8106955cba1123e4e75897a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 19041, "upload_time": "2017-05-25T21:06:22", "url": "https://files.pythonhosted.org/packages/3b/a7/154125c246c593bf4f6788a226fc82d1f45573c81a5130d5ae40376b05c8/flask_jsontools_slippers-0.1.6.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "624d6da47fb8895f70a40060b782e25e", "sha256": "1a538418ebcaa0b2140a7eadae982158db92f84aec49dc2f48f5fb7533bd69f1" }, "downloads": -1, "filename": "flask_jsontools_slippers-0.1.6-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "624d6da47fb8895f70a40060b782e25e", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 20520, "upload_time": "2017-05-25T21:06:21", "url": "https://files.pythonhosted.org/packages/6c/1d/12f5448d3543ab0dd0e90428aa6f8164ef79996a0e543ef7c4cc00bfd487/flask_jsontools_slippers-0.1.6-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "fff1744de8106955cba1123e4e75897a", "sha256": "63717b875c7159ca4de6aec31bf203ccf8137edaaf0e5851f61093bfb231057b" }, "downloads": -1, "filename": "flask_jsontools_slippers-0.1.6.tar.gz", "has_sig": false, "md5_digest": "fff1744de8106955cba1123e4e75897a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 19041, "upload_time": "2017-05-25T21:06:22", "url": "https://files.pythonhosted.org/packages/3b/a7/154125c246c593bf4f6788a226fc82d1f45573c81a5130d5ae40376b05c8/flask_jsontools_slippers-0.1.6.tar.gz" } ] }