Giter Site home page Giter Site logo

luisviniciuslv / pyforgeapi Goto Github PK

View Code? Open in Web Editor NEW
13.0 1.0 1.0 484 KB

PyForgeAPI is a fast, very simple to use and understand open source python library for developing RESTful APIs.

License: GNU General Public License v3.0

Python 100.00%
api api-rest python rest vitrinedev

pyforgeapi's Introduction

PyForgeAPI

What is it and what is it for

PyForgeAPI is a fast, very simple to use and understand open source python library for developing RESTful APIs.

Installation

pip install PyForgeAPI

Examples

Example for GET Route with Query Params and debug mode

from PyForgeAPI import Routes, Response, Request

# Debug mode is False by default
routes = Routes(debug=True)

@routes.get('/')
async def home(req: Request, res: Response):
  # Get query params age
  age = req.query['age']
  # Recovery all persons from database with this age
  res.html("<h1>Listing all persons</h1><ul><li>A Person</li></ul>").status(200).send()

routes.run(application="Person API", host="localhost", port=3000)

Example for GET Route with Params, CORS and multiple methods

from PyForgeAPI import Routes, Response, Request

routes = Routes().cors()

@routes.get('/user/:id')
@routes.post('/user/:id')
async def getUser(req: Request, res: Response):
  # get users from database
  for i in users:
    if i["id"] == req.params["id"]:
      res.json(i).send()
      return
  res.send_status(404)

routes.run(application="Person API", host="localhost", port=3000)

Example for POST Route with Body

from PyForgeAPI import Routes, Response, Request

routes = Routes()

@routes.post('/user')
async def createUser(req: Request, res: Response):
  user = req.body.json
  # Save user in database
  res.text("Created").status(201).send()

routes.run(application="Person API", host="localhost", port=3000)

Example for PUT Route with Body

from PyForgeAPI import Routes, Response, Request

routes = Routes()

@routes.put('/user')
async def createUser(req: Request, res: Response):
  user = req.body.json
  # Update user in database
  res.html('<h1>Created</h1>').status(201).send()

routes.run(application="Person API", host="localhost", port=3000)

Using modules

# chat.py
from PyForgeAPI import Request, Response, Module

chat = Module('chat', '/chat')

@chat.get('/')
async def index(req: Request, res: Response):
  res.send_status(200)

@chat.get('/chat')
async def test(req: Request, res: Response):
  res.send_status(200)
# message.py
from PyForgeAPI import Request, Response, Module

message = Module('message', '/message')

@message.get('/')
async def index(req: Request, res: Response):
  res.send_status(200)

@message.get('/message')
async def test(req: Request, res: Response):
  res.send_status(200)
# main.py
from decorators.routes import Routes

from message import message
from chat import chat

routes = Routes(debug=True).cors()

routes.bind(message)
routes.bind(chat)

routes.run(host='localhost')

See more examples in examples folder

Change Log

Version 1.4.0

ToDo

  • Automatic docs Page

Added

  • Automatic reload in development mode (EXPERIMENTAL)
  • Request method PATCH
  • More security in CORS
  • JSON Database module

Changed

  • Better params and query params recovery in routes
  • Improved route logging

Contributors

How to Contributing

Open pull request ๐Ÿ˜Ž

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.