A simple router for HTTP applications
Project description
http-router – A simple router for HTTP applications
Requirements
python >= 3.7
Installation
http-router should be installed using pip:
pip install http-router
Usage
Create a router:
from http_router import Router
# Initialize the router
router = Router(trim_last_slash=True)
Define routes:
@router.route('/simple')
def simple():
return 'result from the fn'
Call the router with HTTP path and optionally method to get a match result.
match = router('/simple', method='GET')
assert match, 'HTTP path is ok'
assert match.target is simple
The router supports regex objects too:
import re
@router.route(re.compile(r'/regexp/\w{3}-\d{2}/?'))
def regex():
return 'result from the fn'
But the lib has a simplier interface for the dynamic routes:
@router.route('/users/{username}')
def users():
return 'result from the fn'
By default this will capture characters up to the end of the path or the next /.
Optionally, you can use a converter to specify the type of the argument like {variable_name:converter}.
Converter types:
str |
(default) accepts any text without a slash |
int |
accepts positive integers |
float |
accepts positive floating point values |
path |
like string but also accepts slashes |
uuid |
accepts UUID strings |
Convertors are used by prefixing them with a colon, like so:
@router.route('/orders/{order_id:int}')
def orders():
return 'result from the fn'
Any unknown convertor will be parsed as a regex:
@router.route('/orders/{order_id:\d{3}}')
def orders():
return 'result from the fn'
Multiple paths are supported as well:
@router.route('/', '/home')
def index():
return 'index'
Handling HTTP methods:
@router.route('/only-post', methods=['POST'])
def only_post():
return 'only-post'
Submounting routes:
subrouter = Router()
@subrouter('/items')
def items():
pass
router = Router()
router.route('/api')(subrouter)
match = router('/api/items', method='GET')
assert match, 'HTTP path is ok'
assert match.target is items
Bug tracker
If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/http-router/issues
Contributing
Development of the project happens at: https://github.com/klen/http-router
License
Licensed under a MIT license.
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 Distributions
Hashes for http_router-2.2.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc7bc7717212748cd18f305bdc06ab05564342d42a9d274e46f1e9f2b74bd841 |
|
MD5 | 08558efb7c38966995a90d98d91a6b81 |
|
BLAKE2b-256 | ec2efe4989bb0919b537d3203e6d6719b3ef29fad802f2e77aec4757124b95ca |
Hashes for http_router-2.2.0-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a476e5a2100455089ec8ba3bf72110a0d08c028079cea9dcbf97dab06f915c1 |
|
MD5 | 99bea362a7bb9e675e2270d31173f505 |
|
BLAKE2b-256 | 4b10dd6e05e3c720f25434133bcdcb0033db8acfdfd0a3e16dcd27c50938ce95 |
Hashes for http_router-2.2.0-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8877fc03b1ccb63cc4d8f38facae502de62e3b6162b3763a4a3c7d0168d74c0 |
|
MD5 | d7afafd3db5b7579740c850ce7c955cd |
|
BLAKE2b-256 | d0da4cb913efddd06a0109e386bb9585e2e97f29366880ec0d548c31914554a5 |
Hashes for http_router-2.2.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1be753e661f341d9be245a1ff48419d859e6e071116b00f96030bd038626a0c1 |
|
MD5 | 7aeb583ec5a0981dd8f2fd16a5219f42 |
|
BLAKE2b-256 | 7b92e1a2595a15c83759a56ebd214ac4425068aa18b30559d9b581dcd668eac3 |
Hashes for http_router-2.2.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6ded0f94720eb13cca438d1a37ead8636b912aa83e16cc36e9ffce74f7f4607 |
|
MD5 | 077fc138ede903104fa21fffeb63511c |
|
BLAKE2b-256 | 64fa0f0e61da7de87b03e55a6c81571b5a48b5ca77db1d8f81b6eaa3a8610126 |
Hashes for http_router-2.2.0-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ceda995d946580fab6f1bce77b84a7f23d7dd8c036e2b6ba51ff42b0c3d80110 |
|
MD5 | dda08b7acc13567a11d8118feddf7ccc |
|
BLAKE2b-256 | 216857cb1a9c701e5d3f643b928a225ab2796db4d86d8c3c55e9686277edb64c |
Hashes for http_router-2.2.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e369d22e1a75a1a2ffb6a411288e4a6c2afb4add63bf86140a0689ca2aac0235 |
|
MD5 | 8b13784a1b6209e0d40eb4656ee3ed33 |
|
BLAKE2b-256 | 5723a9a41d8f62c4e143bdd7744322f7c253d2ca38047c33b74011aa475b52ed |
Hashes for http_router-2.2.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49dc8f66304d75997a2e2ed418a6cd1c440321e49ea21c0caeaa6683b34bf49d |
|
MD5 | bea6d988da75dc3a2d14aa975141483e |
|
BLAKE2b-256 | 67cd80a95213f4a55781a12390d7aba1864f19fc7fea358663a5c9255a559611 |
Hashes for http_router-2.2.0-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31838b4209c54f099d5a0a5a8d2ac0585ae9a255ce1abe34d6fcc7d8b278d339 |
|
MD5 | 70f5a872957e3bdd937291aa0852bbdf |
|
BLAKE2b-256 | 59403906cf4750617724601c1470d8865ab0b47894d33726c4d01f7e37ab7ebf |