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.5.2-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 176166e52ab72bc7439756fc8cdee39becf238531444245ac82014691c5a0274 |
|
MD5 | 3796514e9b3b9b1fad6059927abd7686 |
|
BLAKE2b-256 | bb5699f69a6b0958fdc74e92e3ffb7a6d08c1d7de645e7d1775501e2d6bf2e31 |
Hashes for http_router-2.5.2-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c851c2605d3441292580bd59861da4655e5d342703e38418f11710c30b95546 |
|
MD5 | 973aba544732e6d96d52c5b650583e67 |
|
BLAKE2b-256 | 0d65f85b57dd6a0d3b3256daa6fc171250a2c9d9e4eaa250709185a9166a11dd |
Hashes for http_router-2.5.2-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5900d92eedbf52a60d342224fed9e8173eec1fe4d455fc9be5d60d400b1feaa2 |
|
MD5 | 340cf7ace457e66053edbfafb254515a |
|
BLAKE2b-256 | 1141ce7a1290921e604d486e5e4606010fa6608d46837c7d84c823898b033977 |
Hashes for http_router-2.5.2-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb18871b1ca53b27487b730a787b1bfc20cd53aff0f01876655a99fdcf703751 |
|
MD5 | 559e163350eaabb37a1d2ed85d615d42 |
|
BLAKE2b-256 | 1f833a77daa342ae1f4ffcfeb0c2d103859947ebb63824e56ab5859aeb3f3d42 |
Hashes for http_router-2.5.2-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7133ba31424bfbdc2698cc2eb19a8f4423d6126ce2eacd0be1589be85c4d5d4 |
|
MD5 | 52591a79faea08bd542be308292713ec |
|
BLAKE2b-256 | 726444018eb850f8765302aecf0a879c2a065370a502571f411e21f9f7336beb |
Hashes for http_router-2.5.2-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7dcca326efa97149ca8bf4ebf3f2ca185cd4ef8d7818177d34a224f78ef7f5ca |
|
MD5 | d91149e6fb9ceb5db694e9872d93ddd5 |
|
BLAKE2b-256 | a0112f849a6ac1a2daa5e3642ca6b054869a0d5feb070ea2198aa4f6b2121ccb |
Hashes for http_router-2.5.2-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2062de36da2ea329e867b84e7f136d720a3ec2f1ec147eac45e5f1474ad811bc |
|
MD5 | 5e81fc8cb11f6b053db046a2fafa701d |
|
BLAKE2b-256 | dd1b82358afc490623421775a10e437a58aa51d8e0b5e76dd6d6cbe0ea790c3b |
Hashes for http_router-2.5.2-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c02a7b56b6491faa3c9c30c0c00d45a285c0c9f7019fef5112de21237d058097 |
|
MD5 | fc3f41f58972f9405cdd14b80e33d112 |
|
BLAKE2b-256 | 6b2908254c0f1850d0302736aedaf66b2efb1d8ec757d3ffcdfd49442fb4f16a |
Hashes for http_router-2.5.2-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20c4b49925c8cf249a49f0bc6ab2aa4a110990d87df3a985b3bd239b9108d9e1 |
|
MD5 | f8dfab453ff83d4e2fc94f5f34c54045 |
|
BLAKE2b-256 | cb4701d1eafe4285fc90e9748642cf3ae299d6360124d5853801ca30feb22656 |