cutshort


Namecutshort JSON
Version 0.0.4 PyPI version JSON
download
home_pagehttps://github.com/sabbiramin113008/cutshort
SummaryYet another, experimental utility to write wsgi REST API apps using python functions, Mostly.
upload_time2023-01-22 09:14:14
maintainer
docs_urlNone
authorSabbir Amin
requires_python
licenseMIT
keywords python wsgi restapi function-to-rest-endpoint
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # **Cutshort**
Yet another, experimental `nano framework` to write WSGI REST API apps using python functions, mostly. 

## How to Install
`pip install cutshort`

## A Pic Generated by `Dall-E`
I was looking for a dark, pale yet dope background image for `cutshort`. So, `Dall-E` game me this.
Prompt was, 
```commandline
night sky with a terror looking blackhole, digital art, high resolution
```
![background](/img/background.png)

## The Very First Hello World
`Cutshort` is developed keeping in mind that you write a python function, and you would directly publish it to let it be consumed.
 First import `API` and `simple_server` from `cutshort`
```python
from cutshort import API, simple_server
```
Instantiate the `API` object and follow on.
```python
api = API()
```
By default, it is going to console print some logs. ( will remove it pretty soon, promise).
To avoid logging, 
```python
api = API(debug= False)
```
Define any python function,
```python
def get_summation(a: int, b: int):
    return a + b
```
Register this function to `API` object. 
```python
api.add_func(get_summation, path='/', http_method='GET')
```
`add_func` receives a `handler_function`, a `path` variable and `http_method`. 

## Automatic method registering with routing path
`cutshort` can check the `handler_func` name and can assign both `routing_path` and `http_method`. 

Function name should start with a action name like `create`,`update`, `get`, `fetch` etc followed by a `_` underscore and
a `resource` name with some extension. example
```python
user_db = [
    {'ID': 1, 'name': 'John Doe', 'age': 21},
    {'ID': 2, 'name': 'Jane Doe', 'age': 23}]

def get_users():
    resp = {
        'users': user_db
    }
    return resp

api.add_func(get_users)
```
So, `api.add_func(get_users)` would set the routing path to `get_users` and `http_method` to 'GET'

## Handling Function Parameters
`cutshort` currenly only supports inputs from `Request-Body` and `JSON-ENCODED`. Internally, when a handler is set against
a particular routing path, request processor first looks for the params in the request-body in JSON, and then delivers it
to the function. After the processing, it collects the response and sends the response after json encoding.
Example:
```python
def get_user_by_id(user_id: int):
    for user in user_db:
        if user.get('ID') == user_id:
            resp = {
                'user': user
            }
            return resp
    return None
```
Here, the request body should be, 
```json
{
	"user_id": 1
}
```
while sending request from `Insomnia` or `POSTMAN` HTTP Clients.

## Running the Application
As `API` is a WSGI callable so it can easily be run with `Gunicorn`, `Waitress` or any similar `WSGIServer`. 
For development purpose, a simple server `simple_server` is provided with `cutshort`, which is a very light wrap-around `simple_server` from
`werkzeug`. So,  running is simply, 
```python
if __name__ == '__main__':
    simple_server(host='localhost', port=8456, application=api)
```

## Complete Example [Almost]
```python
from cutshort import API, simple_server

api = API()

user_db = [
    {'ID': 1, 'name': 'John Doe', 'age': 21},
    {'ID': 2, 'name': 'Jane Doe', 'age': 23}]


def get_summation(a: int, b: int):
    return a + b


def get_users():
    resp = {
        'users': user_db
    }
    return resp


def get_user_by_id(user_id: int):
    for user in user_db:
        if user.get('ID') == user_id:
            resp = {
                'user': user
            }
            return resp
    return None


def create_user(id: int, name: str, age: int):
    user = {
        'ID': id,
        'name': name,
        'age': age
    }
    user_db.append(user)
    return user_db


def delete_user(id: int):
    for index, user in enumerate(user_db):
        if user.get('ID') == id:
            user_db.remove(user)
            return user_db
    return None


def update_user(id: int, name: str):
    for user in user_db:
        if user.get('ID') == id:
            user['name'] = name
            return user
    return None


def send_message(message: str):
    return 'Your Message is + {}'.format(message)


api.add_func(get_summation, path='/', http_method='GET')
api.add_func(send_message,http_method='POST')
api.add_func(get_users)
api.add_func(get_user_by_id)
api.add_func(create_user)
api.add_func(delete_user)
api.add_func(update_user)

if __name__ == '__main__':
    simple_server(host='localhost', port=8456, application=api)
```

## Future Planning
1. Adding Middleware support for interacting with `Request` and `Response` objects.
2. Reading function params from `url-params`.
3. Adding proper logging and debugging messages. 


## Inspirations
This is a pet project and it should not be considered to be using in a critical production environment. This project 
heavily relies on python packages like `parse`, `WebOb` and `Werkzeug`. 

Also, `cutshort` is inspired by [Lumi](https://github.com/Tanmoy741127/lumi)



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/sabbiramin113008/cutshort",
    "name": "cutshort",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "python,wsgi,RESTAPI,function-to-rest-endpoint",
    "author": "Sabbir Amin",
    "author_email": "sabbiramin.cse11ruet@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/af/ce/8c6053c38bda49ee0efbc97b7446f580b2e98d747d8eb2958b80bb5ff0a4/cutshort-0.0.4.tar.gz",
    "platform": null,
    "description": "# **Cutshort**\nYet another, experimental `nano framework` to write WSGI REST API apps using python functions, mostly. \n\n## How to Install\n`pip install cutshort`\n\n## A Pic Generated by `Dall-E`\nI was looking for a dark, pale yet dope background image for `cutshort`. So, `Dall-E` game me this.\nPrompt was, \n```commandline\nnight sky with a terror looking blackhole, digital art, high resolution\n```\n![background](/img/background.png)\n\n## The Very First Hello World\n`Cutshort` is developed keeping in mind that you write a python function, and you would directly publish it to let it be consumed.\n First import `API` and `simple_server` from `cutshort`\n```python\nfrom cutshort import API, simple_server\n```\nInstantiate the `API` object and follow on.\n```python\napi = API()\n```\nBy default, it is going to console print some logs. ( will remove it pretty soon, promise).\nTo avoid logging, \n```python\napi = API(debug= False)\n```\nDefine any python function,\n```python\ndef get_summation(a: int, b: int):\n    return a + b\n```\nRegister this function to `API` object. \n```python\napi.add_func(get_summation, path='/', http_method='GET')\n```\n`add_func` receives a `handler_function`, a `path` variable and `http_method`. \n\n## Automatic method registering with routing path\n`cutshort` can check the `handler_func` name and can assign both `routing_path` and `http_method`. \n\nFunction name should start with a action name like `create`,`update`, `get`, `fetch` etc followed by a `_` underscore and\na `resource` name with some extension. example\n```python\nuser_db = [\n    {'ID': 1, 'name': 'John Doe', 'age': 21},\n    {'ID': 2, 'name': 'Jane Doe', 'age': 23}]\n\ndef get_users():\n    resp = {\n        'users': user_db\n    }\n    return resp\n\napi.add_func(get_users)\n```\nSo, `api.add_func(get_users)` would set the routing path to `get_users` and `http_method` to 'GET'\n\n## Handling Function Parameters\n`cutshort` currenly only supports inputs from `Request-Body` and `JSON-ENCODED`. Internally, when a handler is set against\na particular routing path, request processor first looks for the params in the request-body in JSON, and then delivers it\nto the function. After the processing, it collects the response and sends the response after json encoding.\nExample:\n```python\ndef get_user_by_id(user_id: int):\n    for user in user_db:\n        if user.get('ID') == user_id:\n            resp = {\n                'user': user\n            }\n            return resp\n    return None\n```\nHere, the request body should be, \n```json\n{\n\t\"user_id\": 1\n}\n```\nwhile sending request from `Insomnia` or `POSTMAN` HTTP Clients.\n\n## Running the Application\nAs `API` is a WSGI callable so it can easily be run with `Gunicorn`, `Waitress` or any similar `WSGIServer`. \nFor development purpose, a simple server `simple_server` is provided with `cutshort`, which is a very light wrap-around `simple_server` from\n`werkzeug`. So,  running is simply, \n```python\nif __name__ == '__main__':\n    simple_server(host='localhost', port=8456, application=api)\n```\n\n## Complete Example [Almost]\n```python\nfrom cutshort import API, simple_server\n\napi = API()\n\nuser_db = [\n    {'ID': 1, 'name': 'John Doe', 'age': 21},\n    {'ID': 2, 'name': 'Jane Doe', 'age': 23}]\n\n\ndef get_summation(a: int, b: int):\n    return a + b\n\n\ndef get_users():\n    resp = {\n        'users': user_db\n    }\n    return resp\n\n\ndef get_user_by_id(user_id: int):\n    for user in user_db:\n        if user.get('ID') == user_id:\n            resp = {\n                'user': user\n            }\n            return resp\n    return None\n\n\ndef create_user(id: int, name: str, age: int):\n    user = {\n        'ID': id,\n        'name': name,\n        'age': age\n    }\n    user_db.append(user)\n    return user_db\n\n\ndef delete_user(id: int):\n    for index, user in enumerate(user_db):\n        if user.get('ID') == id:\n            user_db.remove(user)\n            return user_db\n    return None\n\n\ndef update_user(id: int, name: str):\n    for user in user_db:\n        if user.get('ID') == id:\n            user['name'] = name\n            return user\n    return None\n\n\ndef send_message(message: str):\n    return 'Your Message is + {}'.format(message)\n\n\napi.add_func(get_summation, path='/', http_method='GET')\napi.add_func(send_message,http_method='POST')\napi.add_func(get_users)\napi.add_func(get_user_by_id)\napi.add_func(create_user)\napi.add_func(delete_user)\napi.add_func(update_user)\n\nif __name__ == '__main__':\n    simple_server(host='localhost', port=8456, application=api)\n```\n\n## Future Planning\n1. Adding Middleware support for interacting with `Request` and `Response` objects.\n2. Reading function params from `url-params`.\n3. Adding proper logging and debugging messages. \n\n\n## Inspirations\nThis is a pet project and it should not be considered to be using in a critical production environment. This project \nheavily relies on python packages like `parse`, `WebOb` and `Werkzeug`. \n\nAlso, `cutshort` is inspired by [Lumi](https://github.com/Tanmoy741127/lumi)\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Yet another, experimental utility to write wsgi REST API apps using python functions, Mostly.",
    "version": "0.0.4",
    "split_keywords": [
        "python",
        "wsgi",
        "restapi",
        "function-to-rest-endpoint"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "940f4a84b47ae34616119e8f8d2ebfc60bdeda17392e559a520c96b3618d1f84",
                "md5": "7ae76bf53eb29137e39d0a952b958d2e",
                "sha256": "bdff27eb8948a5b499366f0f332a64415fcf284329f7ece78fe78c7e20054746"
            },
            "downloads": -1,
            "filename": "cutshort-0.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7ae76bf53eb29137e39d0a952b958d2e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 5747,
            "upload_time": "2023-01-22T09:14:12",
            "upload_time_iso_8601": "2023-01-22T09:14:12.614156Z",
            "url": "https://files.pythonhosted.org/packages/94/0f/4a84b47ae34616119e8f8d2ebfc60bdeda17392e559a520c96b3618d1f84/cutshort-0.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "afce8c6053c38bda49ee0efbc97b7446f580b2e98d747d8eb2958b80bb5ff0a4",
                "md5": "694d3c821b591a9f28c7eb6caa2a9a7d",
                "sha256": "10fc7ef7035ad0583ce0464e8b10db6c784641e8bb09c630522dfd8b306962d3"
            },
            "downloads": -1,
            "filename": "cutshort-0.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "694d3c821b591a9f28c7eb6caa2a9a7d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5260,
            "upload_time": "2023-01-22T09:14:14",
            "upload_time_iso_8601": "2023-01-22T09:14:14.955799Z",
            "url": "https://files.pythonhosted.org/packages/af/ce/8c6053c38bda49ee0efbc97b7446f580b2e98d747d8eb2958b80bb5ff0a4/cutshort-0.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-22 09:14:14",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "sabbiramin113008",
    "github_project": "cutshort",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "cutshort"
}
        
Elapsed time: 0.06467s