• This repository has been archived on 01/Jul/2023
  • Stars
    star
    639
  • Rank 70,032 (Top 2 %)
  • Language
    Python
  • License
    BSD 3-Clause "New...
  • Created almost 2 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

More Repositories

1

pyaadhaar

This library is built to ease the process of decoding aadhaar QR codes and XML. It supprts old aadhaar QR codes , newly released Secure aadhaar QR codes and also Offline e-KYC XML. This library also can decode QR codes with Opencv. This library bundled with all the features to verify user\'s Email Id and Mobile Number & also to extract the photo of user.
Python
37
star
2

Simple_Dynamic_Website

Simple Dynamic Website Built With HTML, CSS, PHP & MySQL Database
PHP
11
star
3

Bing-Chat-Anywhere

This is an chrome extension that allows you to use Bing Chat from Google Chrome itself
JavaScript
10
star
4

GitHub-Organization-Auto-Invitation-Bot

GitHub Organization Auto Invitation Bot with User Interface. Anyone need to give his email and submit. He will get invitation instantly to his emil address.
CSS
10
star
5

Home_Automation_Energy_Saver_IOT_Nodemcu_Device

Home Automation & Energy Saver Device . This include Nodemcu Full Code , Website Installation Files and Android Application
PHP
6
star
6

kodex

Python
6
star
7

OpenCV-Basics

Covers All The Basics of OpenCV
Python
3
star
8

NLP-Complete-Basics

Jupyter Notebook
3
star
9

Python_Ineuron_Assignments

Jupyter Notebook
3
star
10

blog

JavaScript
3
star
11

Python-ML

Jupyter Notebook
3
star
12

Python_Phone_Directory

Simple Phone Directory with Python
Python
3
star
13

flask_news

HTML
3
star
14

new-editor

HTML
3
star
15

Tic-Tac-Toe-With-Python

Manual and Automatic Tic-Toc-Toe in Python with Source Code
Python
3
star
16

Word_Puzzle_Solve_And_Check_Existance

Enter some alphabets & It will solve that puzzle and tell you the correct answer. In case , the word not exists , it will say that to user.
Python
3
star
17

ReplChat

JavaScript
2
star
18

Spam-Text-Classification-NLP

Jupyter Notebook
2
star
19

tanmoysrt

2
star
20

Twitter-Sentiment-Analysis

Jupyter Notebook
2
star
21

Python-Alexa-Virtual-Assistant

Python
2
star
22

repo-additional-files

2
star
23

Opencv-Projects

Python
2
star
24

Rapid-Video-To-Image_Datset-OpenCV

Python
2
star
25

blogtmp

CSS
2
star
26

Gas-booking-website-Web

PHP
2
star
27

django_blog

JavaScript
2
star
28

ML-Algorithms-From-Scratch

Jupyter Notebook
2
star
29

Image-Face-Recognition-No.-1-End-To-End-Deployment

Image Face Recognition No. 1 End-To-End Deployment
Jupyter Notebook
2
star
30

BCSE3-Assignment

Makefile
2
star
31

realestatedeployment

Jupyter Notebook
2
star
32

minc

Minimal Container for using testing purpose in Docker / K8s based application development | Come with system stats, ping and dns check and remote terminal
HTML
1
star
33

docker-cloud-architecture

Various kind of architecture for large cloud based container deployment with multi deployment and endpoints
1
star
34

Explore-Flutter

Dart
1
star
35

Lets-Learn-Blockchain

Python
1
star
36

Tanmoy741127.github.io

1
star
37

Facebook-Phishing-Page

!! EDUCATION PURPOSES ONLY !!
HTML
1
star
38

Video-Call-Chat-app-from-scratch

Python
1
star
39

Kuikform-Uploader

JavaScript
1
star
40

College-Management-System

JavaScript
1
star
41

test-open-graph-video

HTML
1
star
42

aadhaar-front

Vue
1
star
43

kuikform

An system which can remove the necessity of backend in simple form response collection system
SCSS
1
star
44

newweb_ui

HTML
1
star
45

mern-social-media-client-server-web-and-android

1
star
46

Online-Compiler

JavaScript
1
star
47

aadhaar-image-to-text

HTML
1
star
48

Blog-Frontend

JavaScript
1
star
49

Mediapipe-Explore

Let's learn mediapipe basics and projects
Python
1
star
50

EspWebServer

Python
1
star
51

submitform

HTML
1
star
52

Onos-Cluster-Deployer

Deploy Onos Cluster using docker and docker-compose
Jinja
1
star
53

Intellup

A real-time quiz platform to give competition while all students are at home with long distance with their friends.
JavaScript
1
star
54

React-Router-Basics

JavaScript
1
star
55

kubernetes_with_me

Learn Kubernetes / k8s with me. You can find content and YAML files in this repo
1
star
56

NodeJS-Login-Register-JWT

JavaScript
1
star
57

NodeJS-ExpressJS-First-Project

HTML
1
star
58

optum_hackathon_flutter_front

Dart
1
star
59

mongodb_express_nodejs_blog

EJS
1
star
60

We_r_Titanicx_Aadhaar_Hackathon_Address_Update_Problem

Python
1
star
61

Auto-Update-Telegram-Bot-JU

HTML
1
star
62

docucollect

JavaScript
1
star
63

Local2Net

Expose local ports without port forwarding
Python
1
star
64

nodejs-redis-cache

JavaScript
1
star
65

HackerRank_Python_Questions_Solution

Python
1
star
66

flushmail-web

JavaScript
1
star
67

pepcoding_course_download

Jupyter Notebook
1
star
68

notification_client

JavaScript
1
star
69

blog_backend

Java
1
star
70

geenie

Geenie - Library Management Software
CSS
1
star
71

dsa-leetcode-solutions

Java
1
star
72

test-scripts

Python
1
star
73

replchat_frontend

JavaScript
1
star
74

localbazer

JavaScript
1
star
75

Blog-Project

Python
1
star