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.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e7c199cbd524ac437fa7d5c370f34a586863eac99a7ba6a1bd635269de0da831 |
|
MD5 | ab585ef835721a9201ced9fd0f81c53d |
|
BLAKE2b-256 | 053e16b6b519210bc568787184416c89d3d043511afbc2e4ed922411c73333b3 |
Hashes for http_router-2.3.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c017af4953c9bd779c54a103514ba5194ad70e309ac589311039741f3de7b7ae |
|
MD5 | dbd8e602481c942433f58538202134f3 |
|
BLAKE2b-256 | 2aafb026d4fa91c965ecc453c0edc2e42d99b63337e97ce1471f6bcbe9488c52 |
Hashes for http_router-2.3.0-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 412323f899bbe109dbf986bd01de387aa08f1a730d9528791224c9601c6a9c58 |
|
MD5 | 402791855633521c577be9f4b3efa33f |
|
BLAKE2b-256 | 76a1fa1b844c3ed6b511cb6f1a69ee393abd12068133e94bd229858e8d0d52de |
Hashes for http_router-2.3.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f412b2c9c611a560e1794e61183bfa598486d59bda498e7ce92a7af35f7e55f |
|
MD5 | f5037572771818e1628fc73556ddfd1a |
|
BLAKE2b-256 | 6bf8a7e415d8633fb80bf0fbd3b431fd343beec476696ec928ec09f935d4c99f |
Hashes for http_router-2.3.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c02ddd8b0c11812e87588562707e8c31e637a8c25aa5b90b2cb5964e87db768 |
|
MD5 | 0372bf668f1b044ebee9680f9ace6c5e |
|
BLAKE2b-256 | e3ccf953e2b2d31afe0e6c64ae0824fec339da859be1a32aa7f5d6ac49fdd0c0 |
Hashes for http_router-2.3.0-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 304c8cc2617e316ebd3b4abc916ffb184448d15fe6c5417440c7363adb9b6397 |
|
MD5 | b96f4bf46588c323ea7ce85acfa10e7d |
|
BLAKE2b-256 | f23b1941bbac1e73a48cb6f6eb8fc5f1540205153de6207c2bb32848059a79a2 |
Hashes for http_router-2.3.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 961d129ce4bc2c741f3f582d09e6bae4aedecd0cb157c00f1a338e68f6cf6480 |
|
MD5 | dd7763c21a345b622a187553a50c3f3a |
|
BLAKE2b-256 | 933cbdfec447aedff3b4a324fbf5c3a73a45a1e39148fafb7d4a901ae065085c |
Hashes for http_router-2.3.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 397228d831d18fdd8d3a30501a24e569217ba7f3e5ec774e6fbd121334354005 |
|
MD5 | 8711c3727b9981245bce257668abf085 |
|
BLAKE2b-256 | daad5317f5160e25417c7910da019211d3e5af4246d077637eb9ca293ff0a218 |
Hashes for http_router-2.3.0-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf220a26030850b5c9cc099807ddb6c726bd8f65ffecd6d5e91f46aa390f1244 |
|
MD5 | 95aa043f72aac303ddfb674cf4f9a092 |
|
BLAKE2b-256 | 4cd551c45ef4473e00b1a5f86f68f9d72ccfed6c72f598c5fe1d424ac2b96d70 |