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.0.22-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79a67ea1d11151545d1286409862fa19a3efed8ea057c1290e9aaaac08185f26 |
|
MD5 | ba35e58ff1159dc78a5cd33867daa8fb |
|
BLAKE2b-256 | eff6542fef1d96a6fcc1a16e708714235095cf59a88f3f8bb61a87f08db1bcc7 |
Hashes for http_router-2.0.22-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4fc84c668d78ed7f9203dc1b8d4626152fc65b4963ecd9b9a6e8fc7afc784cfb |
|
MD5 | 7b4b6d7f65bb4114c9c84e89d6537127 |
|
BLAKE2b-256 | 8770130b60aecf108c0a4cefb4d545b056e67c3268e5baea114957136b2d8afe |
Hashes for http_router-2.0.22-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae7f7066bd98bfcf56f3ed918e689c0bcc4d852b3dc6f73af4f1731c84852a35 |
|
MD5 | c9d216e423d82bb18a654f33ce7a9668 |
|
BLAKE2b-256 | 4b8ec88b7c7617b673105d3783ec18ef77dd51787d59d32c64bbac05cfd6f500 |
Hashes for http_router-2.0.22-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5466990c30f5b05890444f87f69105c936f32f0efe3cd36e69ba7f9faf06121b |
|
MD5 | 34983ea5866302e158e0d260f5ade246 |
|
BLAKE2b-256 | e8098e1b7a8db3e0d7e1d79eec40630785bea759875b19baebf6c9dbfef053c9 |
Hashes for http_router-2.0.22-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 43595834fecc3c9c56b4511beb38ff085cb7e26f740cd29586c310c5224524a9 |
|
MD5 | 4884999faec825210c54d5b4a31a11a7 |
|
BLAKE2b-256 | ce3b51aa9616d6fe033d237fcc7a0b97e52236975f74fd21670390c69d7e8183 |
Hashes for http_router-2.0.22-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4cf7df12f24b865a446b7fd261cc00a42507e4120b9a4a520cf3181206d7defe |
|
MD5 | ebbdaacf299cb286140d81aa0c961307 |
|
BLAKE2b-256 | 19d86350047ef5428dfb540d6ed8ff8dd97cf33aaf9a8c46b3b72c183159216c |
Hashes for http_router-2.0.22-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 751fc5f8586e58fb50a8f162c653b2f59e4fc95d322a0792558fa5b6de3b2633 |
|
MD5 | ad0705f167df73ff8a14f65de55cc663 |
|
BLAKE2b-256 | befe72b6a49f35735c1a6928a7f58ea1e34d56a9b1775c775b25c006288f166a |
Hashes for http_router-2.0.22-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eeadfabdad549626f6e7d1dffbbc0ea30da84fb24f44e0ce857fca616db73e5c |
|
MD5 | 3cf4db8a2ea02b212260d6bd4944fbc5 |
|
BLAKE2b-256 | cff75256dad6c856a7fb06fb748328c2f952f92c31adbd5cacffd67f0f4a5eea |
Hashes for http_router-2.0.22-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d3ae781d24d24880153a72db500ee0f8e1f38e5fcc3cac641614a582739051b |
|
MD5 | 9ba4938435e2012cdaeb128ca65c81d4 |
|
BLAKE2b-256 | e22ae5c5db79dacde0a040d6df17e76e8794f98bb9dbb0499ea1088c29af8a10 |