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.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d9bae3d0088b6645b0ac08ea4a50e305da0f0506625d9391467ff317e8460a6 |
|
MD5 | dd73a991fbaad856dac121c64bbf9be8 |
|
BLAKE2b-256 | 6d7f1a7aeb1edec13599bf2329a1f65be6870250bedf6454e07003551c6fca26 |
Hashes for http_router-2.5.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96db6f5330b19f50c6f835137898d0086204b0a280481cb12ad967482329d63a |
|
MD5 | c06ef98ce495ea9fb4e3fc6b40b20fa6 |
|
BLAKE2b-256 | fc01337a5565e3087c4a225b2b6b2c8a98f53d8b4ccf0f7a449b0ad920a6e252 |
Hashes for http_router-2.5.1-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8392bc740f0c349c870963b92903fc0fa3fe2bc1030c23e560deb4700f5d43cb |
|
MD5 | 1cd6928db5c57266e65a10992681ab80 |
|
BLAKE2b-256 | 0078f6ec823597e3111638920aa415b1dbec7071c61f8304c5269af7a9162de2 |
Hashes for http_router-2.5.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48688f61f7fd62ecd4dcc578445d78a421ed9122f0f60efab7ca2893d6646ed2 |
|
MD5 | 6c69e6c92c3428e54874db123f4a9972 |
|
BLAKE2b-256 | 5860341987c0ec6811f87af4aba8e1dc845917e8e013db6d9cabd47255ce0c2a |
Hashes for http_router-2.5.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b335656c7100624fc15e84be8016eee70242aa195f8b69a8deafadfc33d5f70 |
|
MD5 | 10073d25a5ceab76f24197f1017a42bb |
|
BLAKE2b-256 | 64dc2bc3f60bf3f0a1c10ba17b28400d10784930aaca3dc08fe33bf00fe5c6cb |
Hashes for http_router-2.5.1-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1205c1bc69e9733d503d69e1da05941180c1dc9cbf63d65363cd9e9f7c77964a |
|
MD5 | 5d8b7c5851385b371332025635b85ded |
|
BLAKE2b-256 | a078b375c476a681278205ed3e3fef73a3d43446b00b01fbd232e3f73b06febd |
Hashes for http_router-2.5.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cdf0fd40761fa8cfdb788939ad89ce60d23acc4b6feca5b1ae0a9e2ed5e15f2a |
|
MD5 | 9e2aacfd19f081d77a6b50d215574fe9 |
|
BLAKE2b-256 | 1e77e91a2a47c3991f17b4d73494231a1994fe1745348a9ed6f1444d2f5beb44 |
Hashes for http_router-2.5.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2cf683e062794a9dbe1512011b9519f4c36bb61bec1da9ede2f1a3b26a1da06 |
|
MD5 | a17e1743d1cdd9d7a5b45cca741e0acf |
|
BLAKE2b-256 | fa74aa94ef4d74d1c08d3f9b77bba5754a1c4dfb1d517a1f375d8d8c7627ba83 |
Hashes for http_router-2.5.1-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 901fc095e927fcbe1e0ca374a892afde96fed02c0f95ae870fbd35d1101c7258 |
|
MD5 | d70aaa671b609fc468c851ad7b3be60c |
|
BLAKE2b-256 | dc16164d49f8ddad05fa51e3710459cb8eee2797abab1d49d591c76879285436 |