Package: cutshort
CutshortYet another, experimentalnano frameworkto write WSGI REST API apps using python functions, mostly.How to Installpip install cutshortA Pic Generated byDall-EI was looking for a dark, pale yet dope background image forcutshort. So,Dall-Egame me this.
Prompt was,night sky with a terror looking blackhole, digital art, high resolutionThe Very First Hello WorldCutshortis developed keeping in mind that you write a python function, and you would directly publish it to let it be consumed.
First importAPIandsimple_serverfromcutshortfromcutshortimportAPI,simple_serverInstantiate theAPIobject 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,defget_summation(a:int,b:int):returna+bRegister this function toAPIobject.api.add_func(get_summation,path='/',http_method='GET')add_funcreceives ahandler_function, apathvariable andhttp_method.Automatic method registering with routing pathcutshortcan check thehandler_funcname and can assign bothrouting_pathandhttp_method.Function name should start with a action name likecreate,update,get,fetchetc followed by a_underscore and
aresourcename with some extension. exampleuser_db=[{'ID':1,'name':'John Doe','age':21},{'ID':2,'name':'Jane Doe','age':23}]defget_users():resp={'users':user_db}returnrespapi.add_func(get_users)So,api.add_func(get_users)would set the routing path toget_usersandhttp_methodto 'GET'Handling Function Parameterscutshortcurrenly only supports inputs fromRequest-BodyandJSON-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:defget_user_by_id(user_id:int):foruserinuser_db:ifuser.get('ID')==user_id:resp={'user':user}returnrespreturnNoneHere, the request body should be,{"user_id":1}while sending request fromInsomniaorPOSTMANHTTP Clients.Running the ApplicationAsAPIis a WSGI callable so it can easily be run withGunicorn,Waitressor any similarWSGIServer.
For development purpose, a simple serversimple_serveris provided withcutshort, which is a very light wrap-aroundsimple_serverfromwerkzeug. So,  running is simply,if__name__=='__main__':simple_server(host='localhost',port=8456,application=api)Complete Example [Almost]fromcutshortimportAPI,simple_serverapi=API()user_db=[{'ID':1,'name':'John Doe','age':21},{'ID':2,'name':'Jane Doe','age':23}]defget_summation(a:int,b:int):returna+bdefget_users():resp={'users':user_db}returnrespdefget_user_by_id(user_id:int):foruserinuser_db:ifuser.get('ID')==user_id:resp={'user':user}returnrespreturnNonedefcreate_user(id:int,name:str,age:int):user={'ID':id,'name':name,'age':age}user_db.append(user)returnuser_dbdefdelete_user(id:int):forindex,userinenumerate(user_db):ifuser.get('ID')==id:user_db.remove(user)returnuser_dbreturnNonedefupdate_user(id:int,name:str):foruserinuser_db:ifuser.get('ID')==id:user['name']=namereturnuserreturnNonedefsend_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 PlanningAdding Middleware support for interacting withRequestandResponseobjects.Reading function params fromurl-params.Adding proper logging and debugging messages.InspirationsThis 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 likeparse,WebObandWerkzeug.Also,cutshortis inspired byLumi