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.1.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5dd06a4fc646371a0f854f814c09ec5853d37f8c56be15275aecab98cf8711a5 |
|
MD5 | 1197b2bcf92cf8ce857d93473d41497c |
|
BLAKE2b-256 | a315b8714a6072713a2eb9c82ba9f0ffe0170b45cba09b3b035a4711a4fcf037 |
Hashes for http_router-2.1.0-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef1c992c8a8d3cfee111554827efd6558f1e8018d10290bb3ab3dfc4e1e06664 |
|
MD5 | 0f1242c5fde702707a33d109dfd9c774 |
|
BLAKE2b-256 | d4ee33e0530869a735080a4db7e72df807daeace673580b57ba866b48e78d7dc |
Hashes for http_router-2.1.0-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 21b27507c0592fe8bb7d45aeda5f1596ba433dd6c94b87fd21f6f9b36365b388 |
|
MD5 | 134ac83469d6f806489a4c967f73d135 |
|
BLAKE2b-256 | 5cb5ca31672e29045d0653d6f69a711ab02747206c9d17dce77f20f0cc2525a9 |
Hashes for http_router-2.1.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3a9c350e92375494ce7ad02c1297359f8aafdf1beef92c88c600e4763db6a82 |
|
MD5 | e1036b42490c22e6d520c86f47de1386 |
|
BLAKE2b-256 | 0f7dfc7dd9eb15fd8ca52ae0974545a46cc10fdfd2fb8ad5c0e7d8983fb8a726 |
Hashes for http_router-2.1.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87c5b1d45ffeb941bb9703c6273fa60cb035675022051b97e2ed44372487e42d |
|
MD5 | 3ff0c98981b27339702aad87cc758509 |
|
BLAKE2b-256 | e849fba6d577bfed86fb05e8625b7cc3b5fafa6a95320ed0d6568c4c6f8e5384 |
Hashes for http_router-2.1.0-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f10210262e699754b0b1041215fd834cb5bbc9031efd946ddb4089098f2f5691 |
|
MD5 | 0761d5339a293eca0851fd680a6c63a1 |
|
BLAKE2b-256 | 319c40c8bfd0c7c106b7b097b916cc252035bde53fa45bf1a38e184f2565c5ca |
Hashes for http_router-2.1.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9da646dd667837dd7d01b863a7ed0d6fa74b2eba0fac3a93304b661d66731a31 |
|
MD5 | e25f8b6d440646246b1ce5b88e4bbc0a |
|
BLAKE2b-256 | 844ceee34ccfb2cab10cd8b40e5a0929e6fb043bbd38ab96b5ebfd8d85021f5e |
Hashes for http_router-2.1.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a047478ff7564e6e314ac261ab4e42134ffcb1ff8592df8786fe59facb8c29f |
|
MD5 | b93e8c44082a0466163aa9a9b1c2df12 |
|
BLAKE2b-256 | d93e0ea4d02f3a73d500fe7890d6af1bd3328b3d1123b81e1e1721a6805930b6 |
Hashes for http_router-2.1.0-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8ee26d175ae243ce634f413ab6393a2b86820ed4c41c5ef95489d41fe73a1f8 |
|
MD5 | 61e13ce23017f436c4de2c1306d1b25d |
|
BLAKE2b-256 | 878407f55b44832254e4e6f88c4c3190fe671117ca34d9f2a9117fdb713088db |