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.4.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc071673313d1243ead6b739cc72a4f35c799decc9ae4094a2a41b1c2ca9a0e7 |
|
MD5 | 56ba972f72da67f2c79231e0fbf72fa5 |
|
BLAKE2b-256 | 2f22987d20e2540673550fac574fd863643392f9f03797e60c38f46c3dbb3bee |
Hashes for http_router-2.4.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c7956001e4ec5d6ff3f705e7cad27d6dee7c6e0b1c7a57ae46c11e5b1f59666 |
|
MD5 | 215e677ce8a2fb14f7027531734e2ec3 |
|
BLAKE2b-256 | 16ae23b20ef403b88b13b595f6894a0101d6ca86c618e893eb714e86c71cd4bd |
Hashes for http_router-2.4.1-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93da0dc3db4cde3955521f02bdfc58041162103f32e2cd06164d73bec99f909c |
|
MD5 | f5c904dd780de2a352bc25164f24909a |
|
BLAKE2b-256 | f2391326923c0ab1bbeaa967b99e2a1187d7cce62a9c6460f67459758d4094ca |
Hashes for http_router-2.4.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b581eeab2169c4e2840469d404eac62fe8f22737eea6d16673202ced6f932178 |
|
MD5 | 7ebbb4bf956ad9ad77fb633bc57f6895 |
|
BLAKE2b-256 | 555899c5f04aa6830835018784d1b29649fa699f70625676af87e09d906dc857 |
Hashes for http_router-2.4.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6446ddb1e590bb670785c411cb62f749a71f913c9cd49a3aef173138103bc240 |
|
MD5 | b42be888f0e1f116a948818862e8d6ba |
|
BLAKE2b-256 | 27c4f3d8da9974cba693480156367ff0709f7f8aeb598910f7ea7a678d3f0de2 |
Hashes for http_router-2.4.1-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4bb18328711346c4b7c2aa18c3ce21e68b5fd06a79583a3d45c42e1ce0ca06a6 |
|
MD5 | c3ef14dca7da5df851990b8ed0d3623e |
|
BLAKE2b-256 | c67a30c8f44a0b6bdd93595f281a22a8befd4ce2e508a33108cb6f0f4d68d4b8 |
Hashes for http_router-2.4.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7c660718383e5717591dae0860aab2355c97a0735c1cb9713cec82bbcf0df0b |
|
MD5 | 0fc4654837aa0b8efd551336a92c398e |
|
BLAKE2b-256 | 1eaf9203d0a0cbb847ae564a17a58f9b1607f3f1cb9706803a0e6880ee3d5422 |
Hashes for http_router-2.4.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01c1e6785b952523a9e30bdd2a7e0bdf531722370d050e6989fa5903728f9a62 |
|
MD5 | 99d2a2859bd3f36a320f1d75869f8219 |
|
BLAKE2b-256 | 1bf31f7718c596a577dd070b6c3fd96571711819abf5e43985b89537dbea2ea8 |
Hashes for http_router-2.4.1-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d111aadaf39bc2388fc73c17d381a22f30e4e8145bb47a0d8107374ad8bc08d0 |
|
MD5 | f09d682fc5aea144c875a80004829416 |
|
BLAKE2b-256 | 0721175b13afc6d42689777423f1b5d65f7d953136893507b6c4e94d44c3d086 |