{ "info": { "author": "jgv", "author_email": "jgv@trustpilot.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 2 - Pre-Alpha", "Intended Audience :: Developers", "Natural Language :: English", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5" ], "description": "# lambdarest\n\n[![Build Status](http://travis-ci.org/trustpilot/python-lambdarest.svg?branch=master)](https://travis-ci.org/trustpilot/python-lambdarest) [![Latest Version](https://img.shields.io/pypi/v/lambdarest.svg)](https://pypi.python.org/pypi/lambdarest) [![Python Support](https://img.shields.io/pypi/pyversions/lambdarest.svg)](https://pypi.python.org/pypi/lambdarest)\n\nPython routing mini-framework for [AWS Lambda](https://aws.amazon.com/lambda/) with optional JSON-schema validation.\n\n### Features\n\n* `lambda_handler` function constructor with built-in dispatcher\n* Decorator to register functions to handle HTTP methods\n* Optional JSON-schema input validation using same decorator\n\n## Installation\n\nInstall the package from [PyPI](http://pypi.python.org/pypi/) using [pip](https://pip.pypa.io/):\n\n```bash\npip install lambdarest\n```\n\n## Getting Started\n\nThis module helps you to handle different HTTP methods in your AWS Lambda.\n\n```python\nfrom lambdarest import lambda_handler\n\n@lambda_handler.handle(\"get\")\ndef my_own_get(event):\n return {\"this\": \"will be json dumped\"}\n\n\n##### TEST #####\n\n\ninput_event = {\n \"body\": '{}',\n \"httpMethod\": \"GET\",\n \"path\": \"/\"\n}\nresult = lambda_handler(event=input_event)\nassert result == {\"body\": '{\"this\": \"will be json dumped\"}', \"statusCode\": 200, \"headers\":{}}\n```\n\n## Advanced Usage\n\nOptionally you can validate an incoming JSON body against a JSON schema:\n\n```python\nfrom lambdarest import lambda_handler\n\nmy_schema = {\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"body\":{\n \"type\": \"object\",\n \"properties\": {\n \"foo\": {\n \"type\": \"string\"\n }\n }\n }\n }\n}\n\n@lambda_handler.handle(\"get\", schema=my_schema)\ndef my_own_get(event):\n return {\"this\": \"will be json dumped\"}\n\n\n##### TEST #####\n\n\nvalid_input_event = {\n \"body\": '{\"foo\":\"bar\"}',\n \"httpMethod\": \"GET\",\n \"path\": \"/\"\n}\nresult = lambda_handler(event=valid_input_event)\nassert result == {\"body\": '{\"this\": \"will be json dumped\"}', \"statusCode\": 200, \"headers\":{}}\n\n\ninvalid_input_event = {\n \"body\": '{\"foo\":666}',\n \"httpMethod\": \"GET\",\n \"path\": \"/\"\n}\nresult = lambda_handler(event=invalid_input_event)\nassert result == {\"body\": '\"Validation Error\"', \"statusCode\": 400, \"headers\":{}}\n```\n\n### Query Params\n\nQuery params are also analyzed and validatable with JSON schemas.\nQuery arrays are expected to be comma separated, all numbers are converted to floats.\n\n```python\nfrom lambdarest import lambda_handler\n\nmy_schema = {\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"query\":{\n \"type\": \"object\",\n \"properties\": {\n \"foo\": {\n \"type\": \"array\",\n \"items\": {\n \"type\": \"number\"\n }\n }\n }\n }\n }\n}\n\n@lambda_handler.handle(\"get\", schema=my_schema)\ndef my_own_get(event):\n return event[\"json\"][\"query\"]\n\n\n##### TEST #####\n\n\nvalid_input_event = {\n \"queryStringParameters\": {\n \"foo\": \"1, 2.2, 3\"\n },\n \"httpMethod\": \"GET\",\n \"path\": \"/\"\n}\nresult = lambda_handler(event=valid_input_event)\nassert result == {\"body\": '{\"foo\": [1.0, 2.2, 3.0]}', \"statusCode\": 200, \"headers\":{}}\n```\n\n### Routing\n\nYou can also specify which path to react on for individual handlers using the `path` param:\n\n```python\nfrom lambdarest import lambda_handler\n\n@lambda_handler.handle(\"get\", path=\"/foo/bar/baz\")\ndef my_own_get(event):\n return {\"this\": \"will be json dumped\"}\n\n\n##### TEST #####\n\n\ninput_event = {\n \"body\": '{}',\n \"httpMethod\": \"GET\",\n \"path\": \"/foo/bar/baz\"\n}\nresult = lambda_handler(event=input_event)\nassert result == {\"body\": '{\"this\": \"will be json dumped\"}', \"statusCode\": 200, \"headers\":{}}\n```\n\n## Anormal unittest behaviour with `lambda_handler` singleton\n\nBecause of python unittests leaky test-cases it seems like you shall beware of [this issue](https://github.com/trustpilot/python-lambdarest/issues/16) when using the singleton `lambda_handler` in a multiple test-case scenario.\n\n## Tests\n\nYou can use pytest to run tests against your current Python version. To run tests for all platforms, use tox or the built-in `test-all` Make target:\n\n```\nmake test-all\n```\n\nSee [`setup.py`](setup.py) for test dependencies.\n\n\nRelease History\n---------------\n\n1.0.1 (2017-02-24)\n+++++++++++++++++++\n\n**This is not a backwards compatible change.**\n\nFirst OSS release\n\n**features**\n- dispatching handler for individual HTTP methods\n- (optional) jsonschema validation for endpoints\n- automatic wrapping of responses\n\n2.0.0 (2017-03-04)\n+++++++++++++++++++\n\n**This is not a backwards compatible change.**\n\n**features**\n- now json is divided into [\"json\"][\"body\"] for post body and [\"json\"][\"query\"] for json loaded query params\n- jsonschema validation gets whole [\"json\"] object so remember to change your schemas/code!!!\n\n2.1.0 (2017-03-06)\n+++++++++++++++++++\n\n**bugfixes**\n- empty body and queryStringParameters are tolerated\n\n**features**\n- query parameters arrays are now supported\n- array items are tried casted to numbers, defaulted to strings (see last README example)\n- more tests\n\n2.2.1 (2017-10-03)\n+++++++++++++++++++\n\n**features**\n- builds and deploys automatically using travis to pypi and github releases\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/simongarnier/python-lambdarest", "keywords": "lambda aws rest json schema jsonschema", "license": "", "maintainer": "", "maintainer_email": "", "name": "peachai-lambdarest", "package_url": "https://pypi.org/project/peachai-lambdarest/", "platform": "", "project_url": "https://pypi.org/project/peachai-lambdarest/", "project_urls": { "Homepage": "https://github.com/simongarnier/python-lambdarest" }, "release_url": "https://pypi.org/project/peachai-lambdarest/2.2.6/", "requires_dist": null, "requires_python": "", "summary": "pico framework for aws lambda with optional json schema validation", "version": "2.2.6" }, "last_serial": 3406450, "releases": { "2.2.6": [ { "comment_text": "", "digests": { "md5": "ea0d3da80b23bc847d4ccb1968a5bd59", "sha256": "d75a81eb485bc9c1e2a0d780d6af1efa539a7384dab7265271d848390f336dbd" }, "downloads": -1, "filename": "peachai-lambdarest-2.2.6.tar.gz", "has_sig": false, "md5_digest": "ea0d3da80b23bc847d4ccb1968a5bd59", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7713, "upload_time": "2017-12-11T03:05:12", "url": "https://files.pythonhosted.org/packages/4a/95/8a43ef1ba9f6526ad652cbae15cd499529427549fa3e3ef5b86a9d249aed/peachai-lambdarest-2.2.6.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "ea0d3da80b23bc847d4ccb1968a5bd59", "sha256": "d75a81eb485bc9c1e2a0d780d6af1efa539a7384dab7265271d848390f336dbd" }, "downloads": -1, "filename": "peachai-lambdarest-2.2.6.tar.gz", "has_sig": false, "md5_digest": "ea0d3da80b23bc847d4ccb1968a5bd59", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7713, "upload_time": "2017-12-11T03:05:12", "url": "https://files.pythonhosted.org/packages/4a/95/8a43ef1ba9f6526ad652cbae15cd499529427549fa3e3ef5b86a9d249aed/peachai-lambdarest-2.2.6.tar.gz" } ] }