{ "info": { "author": "Nikolay Lysenko", "author_email": "nikolay-lysenco@yandex.ru", "bugtrack_url": null, "classifiers": [], "description": "[![Build Status](https://travis-ci.org/Nikolay-Lysenko/servifier.svg?branch=master)](https://travis-ci.org/Nikolay-Lysenko/servifier)\n[![codecov](https://codecov.io/gh/Nikolay-Lysenko/servifier/branch/master/graph/badge.svg)](https://codecov.io/gh/Nikolay-Lysenko/servifier)\n[![Maintainability](https://api.codeclimate.com/v1/badges/b9203957727d2ea2d808/maintainability)](https://codeclimate.com/github/Nikolay-Lysenko/servifier/maintainability)\n[![PyPI version](https://badge.fury.io/py/servifier.svg)](https://badge.fury.io/py/servifier)\n\n# Servifier\n\n## Overview\n\nIt is an easy-to-use tool for making web service with API from your own Python functions.\n\nThe list of the features is as follows:\n* fault tolerance,\n* customizable requests validation,\n* concise error messages for end user,\n* authentication.\n\n## Minimal Example\n\nSuppose that you have a file named `simple_service.py` that looks like this:\n\n```python\nfrom servifier import HandleSpec, create_app\n\n\ndef add_numbers(first: int, second: int) -> int:\n \"\"\"Add two numbers.\"\"\"\n return first + second\n\n\ndef subtract_numbers(first: int, second: int) -> int:\n \"\"\"Subtract two numbers.\"\"\"\n return first - second\n\n\nhandle_spec_for_adding = HandleSpec(add_numbers, '/add')\nhandle_spec_for_subtraction = HandleSpec(subtract_numbers, '/subtract')\n\nhandle_specs = [handle_spec_for_adding, handle_spec_for_subtraction]\n\napp = create_app(handle_specs)\napp.run()\n```\n\nRun this script. A demo server starts and after that you can send requests to it:\n\n```bash\n>>> curl -X POST -H \"Content-Type: application/json\" -d '{\"first\": 1, \"second\": 3}' http://127.0.0.1:5000/add\n{\"response\":4,\"status\":200}\n>>> curl -X POST -H \"Content-Type: application/json\" -d '{\"first\": 1, \"second\": 3}' http://127.0.0.1:5000/subtract\n{\"response\":-2,\"status\":200}\n```\n\n## Installation\n\nA stable version of the package can be collected from PyPI:\n\n```pip install servifier```\n\n## Tips on Usage\n\n#### Deployment on a Production Server\n\nIn the above minimal example, the development server provided by `Flask` is used. It is not suitable for production usage.\n\nThere are [plenty of ways](http://flask.pocoo.org/docs/1.0/deploying/) to deploy a Flask application on a production server. For example, you can use [Waitress](http://flask.pocoo.org/docs/1.0/tutorial/deploy/#run-with-a-production-server) or uWSGI.\n\nLet us discuss uWSGI a bit more. You can create `uwsgi.ini` config:\n\n```\n[uwsgi]\n# {Python module}:{Flask app from there}\nmodule = simple_service:app\n# If it is true, there is a master process, not only workers.\nmaster = true\n# Number of workers.\nprocesses = 4\n# Host and port for API, '0.0.0.0' means to use web address.\nhttp = 0.0.0.0:7070\n# Directory with code to be imported.\npythonpath = ./venv/lib/python3.6/site-packages/\n# If it is not set, logs are printed. If it is set, logs are written to this file.\nlogto = /tmp/servifier.log\n```\n\nTo use it, you need to install `uWSGI` Python package:\n```\npip install uwsgi\n```\n\nTo start a production server, delete `app.run()` line from `simple_service.py` (it launches demo server) and run:\n```\nuwsgi --ini uwsgi.ini\n```\n\nIt may be enough to have just uWSGI. However, you can also add Nginx in front of uWSGI as a load balancer and a reverse proxy.\n\n#### Input Data Validation\n\nIt is possible to configure `servifier` so that requests with invalid data are rejected with a proper error code before your function is called.\n\nThe minimal example with a simple service can be modified in the following manner:\n\n```python\nfrom servifier import HandleSpec, create_app\nfrom servifier.validation import IntegerField\n\n\ndef add_numbers(first: int, second: int) -> int:\n \"\"\"Add two numbers.\"\"\"\n return first + second\n\n\ndef subtract_numbers(first: int, second: int) -> int:\n \"\"\"Subtract two numbers.\"\"\"\n return first - second\n \n \nclass IntegerPair:\n \"\"\"A pair of two integers.\"\"\"\n \n first = IntegerField(required=True)\n second = IntegerField(required=True)\n \n def __init__(self, first: int, second: int):\n \"\"\"Initialize an instance with parameters validation.\"\"\"\n self.first = first\n self.second = second\n\n\nhandle_spec_for_adding = HandleSpec(\n add_numbers, '/add', IntegerPair\n)\nhandle_spec_for_subtraction = HandleSpec(\n subtract_numbers, '/subtract', IntegerPair\n)\n\nhandle_specs = [handle_spec_for_adding, handle_spec_for_subtraction]\n\napp = create_app(handle_specs)\napp.run()\n```\n\nBehavior of the service is demonstrated below:\n\n```bash\n>>> curl -X POST -H \"Content-Type: application/json\" -d '{\"first\": \"1\", \"second\": 3}' http://127.0.0.1:5000/add\n{\"error\":\"Invalid Request: check your JSON\",\"status\":422}\n```\n\nComparing to the minimal example, this service returns \"Invalid Request\" status instead of \"Internal Error\" status which is harder to debug for end user.\n\nIf you need more info about how this example works, read about [Python descriptors](https://www.codevoila.com/post/69/python-descriptors-example).\n\n#### Authentication\n\nIt is possible to deny requests that does not include login and token where proper value of token is defined by login and arbitrary salt.\n\nMinimal example with authentication enabled looks like this:\n\n```python\nfrom servifier import HandleSpec, create_app\n\n\ndef add_numbers(first: int, second: int) -> int:\n \"\"\"Add two numbers.\"\"\"\n return first + second\n\n\ndef subtract_numbers(first: int, second: int) -> int:\n \"\"\"Subtract two numbers.\"\"\"\n return first - second\n\n\nhandle_spec_for_adding = HandleSpec(\n add_numbers, '/add', auth_salt='abcd'\n)\nhandle_spec_for_subtraction = HandleSpec(\n subtract_numbers, '/subtract', auth_salt='1234'\n)\n\nhandle_specs = [handle_spec_for_adding, handle_spec_for_subtraction]\n\napp = create_app(handle_specs)\napp.run()\n```\n\nFor a particular login, you can generate its token with `servifier.auth.generate_token` function and tell this value to someone sending requests under this login. JSON attachment from a request must include two additional fields ('login' and 'token') besides fields with arguments for a Python function.\n\n```bash\n>>> curl -X POST -H \"Content-Type: application/json\" -d '{\"login\": \"a\", \"token\": \"6491cacf01b2e1c6d08a5609d2f570ea57d71ae7f06e0391276d70d935d29aa51888d566751aa36dc5e12e18da693ece36427c167e2a7a67e48aca8928ba3979\", \"first\": 1, \"second\": 3}' http://127.0.0.1:5000/subtract\n{\"result\":-2,\"status\":200}\n```", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/Nikolay-Lysenko/servifier", "keywords": "web_service api_maker apify ml_engineering model_to_production", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "servifier", "package_url": "https://pypi.org/project/servifier/", "platform": "", "project_url": "https://pypi.org/project/servifier/", "project_urls": { "Homepage": "https://github.com/Nikolay-Lysenko/servifier" }, "release_url": "https://pypi.org/project/servifier/0.1.1/", "requires_dist": null, "requires_python": ">=3.6", "summary": "An easy-to-use tool for making web service with API from your own Python functions", "version": "0.1.1" }, "last_serial": 5526857, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "4df9179e5fd08b6f1e31f05cb029ccb0", "sha256": "01fc8960336f661ceae795e3063d5ed687bc9d1add64cc67b1dc1af9ac6fcd3a" }, "downloads": -1, "filename": "servifier-0.1.0.tar.gz", "has_sig": false, "md5_digest": "4df9179e5fd08b6f1e31f05cb029ccb0", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6", "size": 7162, "upload_time": "2019-04-29T21:09:13", "url": "https://files.pythonhosted.org/packages/05/67/856ab0012819a7fe73f217f8e2098f8dccf371a29a6b8a7682aff4651522/servifier-0.1.0.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "26a58cd5b846384991ec2ba40704eb6f", "sha256": "cb0309c0c45aeff5334ef9b8622058be28e8eb4c6f9fff5e214cf5948be1f302" }, "downloads": -1, "filename": "servifier-0.1.1.tar.gz", "has_sig": false, "md5_digest": "26a58cd5b846384991ec2ba40704eb6f", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6", "size": 7206, "upload_time": "2019-07-13T13:12:24", "url": "https://files.pythonhosted.org/packages/e1/54/1977e2ea000e20404b1a2316ab0e0e621fb76b177d0025edef243b2d2eee/servifier-0.1.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "26a58cd5b846384991ec2ba40704eb6f", "sha256": "cb0309c0c45aeff5334ef9b8622058be28e8eb4c6f9fff5e214cf5948be1f302" }, "downloads": -1, "filename": "servifier-0.1.1.tar.gz", "has_sig": false, "md5_digest": "26a58cd5b846384991ec2ba40704eb6f", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6", "size": 7206, "upload_time": "2019-07-13T13:12:24", "url": "https://files.pythonhosted.org/packages/e1/54/1977e2ea000e20404b1a2316ab0e0e621fb76b177d0025edef243b2d2eee/servifier-0.1.1.tar.gz" } ] }