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,
night sky with a terror looking blackhole, digital art, high resolution
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
from cutshort import API, simple_server
Instantiate the API object and follow on.
api = API()
By default, it is going to console print some logs. ( will remove it pretty soon, promise). To avoid logging,
api = API(debug= False)
Define any python function,
def get_summation(a: int, b: int):
return a + b
Register this function to API object.
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
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:
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,
{
"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,
if __name__ == '__main__':
simple_server(host='localhost', port=8456, application=api)
Complete Example [Almost]
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
- Adding Middleware support for interacting with
RequestandResponseobjects. - Reading function params from
url-params. - 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
Metadata
Release files for cutshort 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cutshort-0.0.4.tar.gz | 5.3 kB | Without provenance | Details for cutshort-0.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cutshort-0.0.4-py3-none-any.whl | Python 3 | none | any | Details for cutshort-0.0.4-py3-none-any.whl |
Total release size: 11.0 kB
Release files / cutshort-0.0.4.tar.gz
| Download URL | cutshort-0.0.4.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
10fc7ef7035ad0583ce0464e8b10db6c784641e8bb09c630522dfd8b306962d3
|
|
BLAKE2b-256 checksum How to use checksums |
afce8c6053c38bda49ee0efbc97b7446f580b2e98d747d8eb2958b80bb5ff0a4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.10
|
Release files / cutshort-0.0.4-py3-none-any.whl
| Download URL | cutshort-0.0.4-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bdff27eb8948a5b499366f0f332a64415fcf284329f7ece78fe78c7e20054746
|
|
BLAKE2b-256 checksum How to use checksums |
940f4a84b47ae34616119e8f8d2ebfc60bdeda17392e559a520c96b3618d1f84
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.10
|
Security for cutshort 0.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.