Skip to main content

Yet another, experimental utility to write wsgi REST API apps using python functions, Mostly.

Project description

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

background

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

  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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cutshort-0.0.4.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

cutshort-0.0.4-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file cutshort-0.0.4.tar.gz.

File metadata

  • Download URL: cutshort-0.0.4.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for cutshort-0.0.4.tar.gz
Algorithm Hash digest
SHA256 10fc7ef7035ad0583ce0464e8b10db6c784641e8bb09c630522dfd8b306962d3
MD5 694d3c821b591a9f28c7eb6caa2a9a7d
BLAKE2b-256 afce8c6053c38bda49ee0efbc97b7446f580b2e98d747d8eb2958b80bb5ff0a4

See more details on using hashes here.

File details

Details for the file cutshort-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: cutshort-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for cutshort-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 bdff27eb8948a5b499366f0f332a64415fcf284329f7ece78fe78c7e20054746
MD5 7ae76bf53eb29137e39d0a952b958d2e
BLAKE2b-256 940f4a84b47ae34616119e8f8d2ebfc60bdeda17392e559a520c96b3618d1f84

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page