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.20-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3d3d5ceba6aad4ee0f025d28bc94b04eb523dcb36d0cf12c1bacc1ea24bb0cb |
|
MD5 | 9af913c8cd5ef98ce24c0ee62408d205 |
|
BLAKE2b-256 | 8f8aa160d6b8d4f43303828ebe3135efbf9a9341eac471081e4f640c3af686e3 |
Hashes for http_router-2.0.20-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d79274c508d9465fb6f7db08bf1136cc59bf7959e1e8b3b80ca5b5a225b4f996 |
|
MD5 | 340c4942eed80b47b51bcd3002ce8b69 |
|
BLAKE2b-256 | a1b0fa8cd4d49801a94d7e6c187d30069802c080d6620aecdc81f8c0079d270b |
Hashes for http_router-2.0.20-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 10bd4b3217e4b6a9a12fd7962df4dc3b86f2f12e6884ad6cf8211e2b9a2d4530 |
|
MD5 | 4aae8ac2c9233979b05cfbcf49ea3692 |
|
BLAKE2b-256 | a2c789434c03c4981056149787d1ef89e5143d826fc286c0479f423c2708b852 |
Hashes for http_router-2.0.20-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b10010c32056999b090fe7089b8711ff3e76adfebb28e28ba0473cfa5c41174 |
|
MD5 | 49acf26852cfed3a64a628c81a290c41 |
|
BLAKE2b-256 | 4adf3ba974582a75a1c8d9a58f439e8ef82de2197e3030d0aee1c9a4e04e0d0b |
Hashes for http_router-2.0.20-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14c104315a76ff09f50ef5eeda4b2f77149b15e36fff78b14489efdcafc3d34a |
|
MD5 | ad1f6ec64793956e223e6e44da617383 |
|
BLAKE2b-256 | 343a3227af70ca732be3fbfdf03158c07c1218a783cb093b82cb9ad2d770c215 |
Hashes for http_router-2.0.20-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c41f9f92e3d1cec925ce795e40a29873cc3a19215d70605f28fdd247b8f6727 |
|
MD5 | be3c94b9492a895686b085dacb832944 |
|
BLAKE2b-256 | 2b4d9211608f35b992f9fe49e85f8576706470da30a0c1fd2b652cc71859d699 |