Giter Club home page Giter Club logo

lumi's Introduction

Lumi ๐Ÿ’ง

Lumi is a nano framework to convert your python functions into a REST API without any extra headache.

  • This library is created by taking the concept of RPC and blended with REST API specs.
  • We need to just register the function and it will be available as a REST API.
  • Web-server written with Gunicorn
  • Local development server provided for rapid development and prototyping.

Installation

pip install lumi

Function <--> API mapping

function - API mapping

How to use ๐Ÿค”

Let's create a simple function to add two numbers.

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

Now, we want to expose this function as a REST API. We can do this by registering the function with Lumi.

# app.py

from lumi import Lumi

app = Lumi()

app.register(add) # Registering the function
app.register(subtract)

app.runServer(host="127.0.0.1", port=8080)

Noice ๐ŸŽ‰๐ŸŽ‰ API has been generated

Run the sever by

python app.py

You are going to see this in your terminal

[2022-11-24 17:32:08 +0530] [10490] [INFO] Starting gunicorn 20.1.0
[2022-11-24 17:32:08 +0530] [10490] [INFO] Listening at: http://127.0.0.1:8080 (10490)
[2022-11-24 17:32:08 +0530] [10490] [INFO] Using worker: sync
[2022-11-24 17:32:08 +0530] [10492] [INFO] Booting worker with pid: 10492
...
...
[2022-11-24 17:32:08 +0530] [10500] [INFO] Booting worker with pid: 10500

Congratulations ๐Ÿ‘. Our Server is online.

The above code will generate a REST API with the following details.

  • Endpoint : 127.0.0.1:8080
  • Route : /add
  • Method : POST
  • Sample Request Body : {"a": 1, "b": 2}

Let's run the API and test it.

curl -X POST -H "Content-Type: application/json" -d '{"a": 1, "b": 2}' http://127.0.0.1:8080/add

Output

{
    "exit_code": 0, 
    "status_code": 200, 
    "result": 3, 
    "error": ""
}

Custom Routing

Now you may think, the function name will be always same as the route. But, you can change the route by passing the route parameter.

app.register(add, route="/addition")

Custom Request Method

By default, the request method is POST. But, you can change it by passing the method parameter. Currently, it supports GET, POST, PUT and PATCH methods.

from lumi import Lumi, RequestMethod

app = Lumi()

def add(a, b):
    return a+b

# Default : Register function for POST method
app.register(add)
# Register function for GET method
app.register(add, request_method=RequestMethod.GET)
# Register function for POST method
app.register(add, request_method=RequestMethod.POST)
# Register function for PUT method
app.register(add, request_method=RequestMethod.PUT)
# Register function for PATCH method
app.register(add, request_method=RequestMethod.PATCH)

app.runServer()

๐ŸŸก Pay attention before using GET request : If you are using GET method

  • You need to pass the parameters in the query string, as GET dont support request body.
  • All those arguments, that will be passed to function will be in String format. So take care to convert them to the desired type in your function.

Send File

Send file to user by returning the file object.

from lumi import Lumi, RequestMethod
app = Lumi()

def download_file():
    return open("file.txt", "rb") # Return file object

app.register(download_file) 

Debug Mode

By default, the debug mode is True. But, you can change it by passing the debug parameter.

# app.py

from lumi import Lumi

app = Lumi(debug=False)
...

Status Codes

Status Code Description
200 Request successfully executed and No Error happened during function execution
500 Request was received but there was an error during function execution
400 Bad Request (Possible Reason - The required parameters for the function has not provided)
405 Method Not Allowed (Lumi only supports POST request)
404 The route has no function associated with that

Exit Codes

Exit Code Description
0 No Error
1 Error

Note : If the function has some error , you can expect the exit code to be 1 and the error message in the response.

Task Lists

  • Base System
  • Add support for default parameters that is provided in the function
  • Debug mode and logging support
  • Make available GET request for the function
  • Provide option to override POST with PUT if the user wants
  • Add support to send file directly to user
  • Add support to serve files through a public folder [Customizable]
  • Add suport for middleware integration
  • Support nested routing of urls
  • For local development, create an file observer that can automatically reload the server when the file is changed.
  • Add support for object serialization and deserialization based on argument types of function

Contributing

Contributions are always welcome!

Our community

Tanmoy741127
Tanmoy Sarkar
AmirMGhanem
Amir M. Ghanem
matheusfelipeog
Matheus Felipe
0xflotus
0xflotus

Support

Buy Me A Coffee

lumi's People

Contributors

0xflotus avatar amirmghanem avatar github-actions[bot] avatar matheusfelipeog avatar tanmoysrt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lumi's Issues

Support for File

Currently, Lum supports communication over JSON only.
But in real life, we need to upload files and also download files from Server. So we will bring that support

Tasks -

  • **Send File To Client: ** Any function can return an instance of a file from any function. In the backend, we need to check the return type and if that is file, need to set the appropriate headers and send the file
  • Receive File From Client: : To start work on this feature set, we need to first complete this issue #8 . After then we can work on this. When the file will receive we will cast it to file and pass it to function

Json Encoding Problem

Everytime i`m running API CURL get error: {"exit_code": 1, "status_code": 400, "result": "", "error": "Failed to decode JSON"}

GET Or PUT Methods

first of all i like the idea of the project.
how do you deal with method types?

Create LOG

is there any simple way to create simple loggong mechanism per function?

lumi doesn't work on Windows

Gunicorn doesn't support Windows which restricts also lumi framework.
Maybe there should be considered any other WSGI server implementation, e.g. waitress which works well on other OSes?

Add supports for `application/x-www-form-urlencoded` and `multipart/form-data` types

Currently, Lumi accepts only application/json. It's best because the data type doesn't convert to string when reached to the server.
But in some special cases, we may need application/x-www-form-urlencoded and multipart/form-data ,

So we are going to enable these options and the user will have the option to define the type of arguments so that Lumi can auto-cast the arguments before sending them to the function.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.