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.3.2-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eab2bb5e18736c08c65608ef0be4c25e08affedc73d37aebed4bb9baa0a2fc46 |
|
MD5 | 37356d68b37fb56c8902c64185c55451 |
|
BLAKE2b-256 | 24d946d4f3debc09d2a2c9f4cc2766d999851e065bc8629b48968880e3432591 |
Hashes for http_router-2.3.2-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6cf1842ea253109d8d0b1feda33459bf9142a1732dc0472970cced66f5b3009 |
|
MD5 | db00b84f3a6e16f7565e52ce2f79b3dc |
|
BLAKE2b-256 | 3a4f478e2c31728a592e092a3c54b2463ead3fd4182e66767c29932d8e55e205 |
Hashes for http_router-2.3.2-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95ce98d4671188f043587b2f289730443be1d043ed326e1a97b9c7cffaf78012 |
|
MD5 | 6f7538dc6a8bde64246bb25d66782d08 |
|
BLAKE2b-256 | 01dac012c289a058a434b2dddc4f245905cf6d6a8153131fbd28f746a5da298a |
Hashes for http_router-2.3.2-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 255397b8848b45e4e9151d469e851eb072f080703d4bc0e46d459c585f01a239 |
|
MD5 | 12b05bec9a2cf424ccf990f2bdb7ba9f |
|
BLAKE2b-256 | ecee19ef9fd29f0f86fbb805f73d370ae7e96ea90a541320e994dc82b0e7a1ba |
Hashes for http_router-2.3.2-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2be357aaf064881e4439eaa0fecfecc536e92df9a33afb71f8387c4f0ee15b23 |
|
MD5 | 90c9d4021dcd855e0764a19f50f7918d |
|
BLAKE2b-256 | b733e1acde881735384c147bb413ca156b14925c22c8a967366d63fe5b0408a0 |
Hashes for http_router-2.3.2-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4187821ce0db4ae11ce78172d3c2cc11f58ebd67c96e96afc4e1bda45054f0a5 |
|
MD5 | 9b6d9b20da376862c41d54b751eefaa2 |
|
BLAKE2b-256 | 44f31912eb18a90f37c6e5e4b17f0d4f0bb0b735cc663007085d715b55707125 |
Hashes for http_router-2.3.2-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f33100edd3a05bd7efd96c3e503d9239f34e8c4d467af87872d41ff0028dfbcd |
|
MD5 | 540aad42fd3f31e7dd8f90f4c86b9085 |
|
BLAKE2b-256 | a3d97b7012e2b0872d9f2fa127394ef56a715fafd519e382752683f3050ee1ac |
Hashes for http_router-2.3.2-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f680c0ab568313deb6df25fddd872e8a95a6e10660147db4dda11fc121453294 |
|
MD5 | 07fa0b6b913fcf932a6d87e240320f33 |
|
BLAKE2b-256 | ab1033f748ce50a65125153499f7acc7383a45b7991a22af1a2d11d42ad2d3e6 |
Hashes for http_router-2.3.2-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb0e5c89e84453ac7d975e8fc3c51d00d8471838b8cbb550cc6ec3a14a99cb78 |
|
MD5 | 219d8cc8d59326d0e3133b96dd097b9e |
|
BLAKE2b-256 | ae6d773ce7311452eb52d8535095e840e46ab9fb19a00491163aeb3d2c49cc2d |