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.19-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dbcc3495350bc01d3fc7d789a88a29ad9542b679b38db219ae3ab3325eb8cb18 |
|
MD5 | 87c8e5bb45788d586aec13656062ef68 |
|
BLAKE2b-256 | 218bf94b1ef3480a4a35b24c500b8d72e05784aee6ade3dc0f303b9989ca9ae4 |
Hashes for http_router-2.0.19-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 846604b11cec3977a50a6a256cf38066fd1b8c5724d40e7c62b07a5eefeaac4e |
|
MD5 | 290e44e4e585236a632cfbe9afe8c5f8 |
|
BLAKE2b-256 | 74270645988c12ed77e8c60580df54afd8a0ba447c0fd6c62dec125155598d2b |
Hashes for http_router-2.0.19-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 863d770322a0ad209a16a8626f78840b4c00e0219d6359209197dc28cde717d7 |
|
MD5 | be38b5ce4517b5acc0bb39539dd51277 |
|
BLAKE2b-256 | ca55a54f3c47776fbafb6c3fef0a2600d7860dac13359f54ae0f317929175a66 |
Hashes for http_router-2.0.19-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6375165baa24d8d58546601c94cd304084c06d1da0a93d18e5fbd839c7f1bd3a |
|
MD5 | c7e2cc9fbc3b48e26a58930b889237e5 |
|
BLAKE2b-256 | 2765fab0c0f4ee1c8bbf67963615e5d9e077f59caac38ed41fd58bb421475f8a |
Hashes for http_router-2.0.19-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 274c097aab1a52d8ca14d093efb22d4cd8940fe318a36a7332bbf7977be5a4e5 |
|
MD5 | 522c7f4bf9a5b79589a7798e30907f27 |
|
BLAKE2b-256 | a5d226c4d2959ef3d49d77dd11e9969fe8fd3f2d428d3f2aaf41fa4a88d0cb58 |
Hashes for http_router-2.0.19-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b2283105df5f2e4b62cd003d26e8941ef71388de82802ffe952b2f742a56ea6 |
|
MD5 | 2a6791416282ef071dda27fd97dff76b |
|
BLAKE2b-256 | d77ba4f62c957a5f96a19e4ccdb7860b8bb786ebd8c1096c1febdfd476420f50 |