Extremely Stupid Simple, Blazing Fast, Get Out of your way immediately Microframework for building Python Web Applications.
Reason this release was yanked:
Cookie not present error, fixed in 0.2.9
Project description
Routerling
A new born baby router on it's way to being a web development platform. Routerling is a router multiplexer built with 1 goal in mind.
- Make it stupid simple to build high performance web applications
How?
Following the unix philosophy of be the master of one thing and one thing only - Routerling has only ONE API you need to learn - Router
. The API is also deliberately made
consistent across the 3 different supported languages [Golang, Python, Typescript/JavaScript].
See Similarities
Python
from routerling import Context, HttpRequest, ResponseWriter, Router
from logging import log
def get_customer_orders(r: HttpRequest, w: ReponseWriter, c: Context):
w.headers = "go-go-gadget", ""
w.body = '{customer: {customer_id}, orders: []}'.format(r.params.get('id'))
def change_headers(r: HttpRequest, w: ResponseWriter, c: Context):
w.headers = "go-go-gadget", "i was included after..."
def create_customer(r: HttpRequest, w: ResponseWriter, c: Context):
print(r.body)
w.status = 201
w.body = '{id: 13}'
# register functions to routes
router.BEFORE('/*', lambda req, res, state: print('will run before all routes are handled'))
router.AFTER('/v1/customers/*', change_headers)
router.GET('/v1/customers/:id/orders', get_customer_orders)
router.GET('/v1/customers/:id', lambda req, res, state: log(2, state.abcxyz_variable))
router.POST('/v1/customers', create_customer)
Serve your application
uvicorn app:router
Request Object
-
request.body
Body/payload of request if it exists. You can use any
XML, JSON, CSV etc.
library you prefer to parser.body
as you like.# r.body: str body: str = r.body
-
request.headers
header = r.headers.get('header-name')
All headers sent with the request.
# r.headers: dict header = r.headers.get('content-type')
-
request.params
param = r.params.get('param-name')
Dictionary containing parts of the url that matched your route parameters i.e.
/customers/:id/orders
will return{'id': 45}
for url/customers/45/orders
.identifier = r.params.get('id')
Response Object
-
response.abort
Signals to routerling that you want to abort a request and ignore all
GET
,POST
,PUT etc.
handlers including allAFTER
orBEFORE
hooks from executing. The payload given to abort is used as the response body. Only callingw.abort() will not end function execution.
You have to explicitly return from the request handler after using w.abort().w.abort('This is the body/payload i want to abort with') return # or return w.abort('This is the body/payload i want to abort with') #abort registers then returns None
-
response.body
Used to set payload/body to be sent back to the client. Returning data from a handler function does not do anything and is not used by routerling.
To send something back to the client use w.body
.w.body = b'my body'
-
response.headers
Used to set headers to be sent back to the client.
w.headers = "Content-Type", "application/json" w.headers = ["Authorization", "Bearer myToken"]
Context Object
-
context.keep
Used to store values across different request handlers for any given request lifecylce i.e. from the time a client browser hits your server (request start) to the time a response is sent back to client (request end).
Sample Use Case
Imagine you want to use the amazing jsonschema library to validate json request payloads received on every POST, PATCH, or PUT request from your users. One way to achieve this is to decorate your request handlers and abort the request if the json payload does not match your schema validation constraints.
The code snippet below shows a sample implementation for such a use case for reference.
from http import HttpStatus as status from json import dumps, loads # consider using ujson in production apps from jsonschema import validate, draft7_format_checker from jsonschema.exceptions import ValidationError def expects(schema): def proxy(func): @wraps(func) async def delegate(r: HttpRequest, w: ResponseWriter, c: Context): body = loads(r.body) try: validate(instance=body, schema=schema, format_checker=draft7_format_checker) except ValidationError as exc: w.status = status.NOT_ACCEPTABLE w.headers = 'content-type', 'application/json' return w.abort(dumps({'message': exc.message})) # Here we call Context.keep('key', value) to store values across handlers for the request lifecycle # ------------------------------------------------------------------------------------------------- for k,v in body.items(): c.keep(k, v) return await func(r, w, c) return delegate return proxy @expects({ 'type': 'object', 'properties': { 'username': {'type': 'string'} 'password': {'type': 'string', 'minLength': 8} } }) async def handler(r: HttpRequest, w: ResponseWriter, c: Context): print(c.username, " you can do this because the @expects :func: calls c.keep('username', value)") print(c.password, " ^^^ same for this") w.body = dumps({'username': username, 'password': password})
-
context._application
Retrieves the root Routerling app instance (root router instance), after all you are an adult when it comes to coding ;-)
Newly introduced in routerling version 0.2.3
Socket Connections
Implementation & Documentation coming soon
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file routerling-0.2.8.tar.gz
.
File metadata
- Download URL: routerling-0.2.8.tar.gz
- Upload date:
- Size: 14.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.9.6 Linux/5.4.0-80-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d14d020828187aa707de30d93fb53881d9c21ef5c897c059fe6a95b24d81e02 |
|
MD5 | 5de5d841da853a7cb98d414d8bbc7db0 |
|
BLAKE2b-256 | 46c453e3707f597bc37149fdbfdb6db6cc2407e5be9dc855f514bad3f5807073 |
File details
Details for the file routerling-0.2.8-py3-none-any.whl
.
File metadata
- Download URL: routerling-0.2.8-py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.9.6 Linux/5.4.0-80-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c188780543008041f21dcba2b36d6d7893b921c77030666ea6345f903f04e5c8 |
|
MD5 | c31e4f017e606f0310b43298ff802053 |
|
BLAKE2b-256 | 494c7eaf9c6179ca50a3e823cb60c8b17bfeb396ab203c9f2ffce33f62de140d |