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.2.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73fb3303aadbb422bc5cbd7eb264261c04d5ed22fd2a2e8cfd6568ac049a9ca6 |
|
MD5 | 813f029ec40cf2b9077bcace573af34b |
|
BLAKE2b-256 | d4005cae009675b32016e86ce3254cc5f126718f494ba5243bb6382cb9d28905 |
Hashes for http_router-2.2.1-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88596b2966a4b20a65a54f04399bec9011d0485f354bc25780b07a6543ec3b31 |
|
MD5 | dde3428e698e3e453327287b28d8033f |
|
BLAKE2b-256 | 35b1d8cb9921f72bd4023398af08e231ec6342566de1bee625b778f143db5978 |
Hashes for http_router-2.2.1-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b2906499f4e01d798117a7ffa05aeb9f452331888e236269bf24825f80a34bd |
|
MD5 | 562d1354f3ad3e2274c693c8aa738ce4 |
|
BLAKE2b-256 | 7009741c5a36898e18b54b692577f5127bf84fdc392cb1cb3725376d0e468a30 |
Hashes for http_router-2.2.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a5efacec11e8dbc6d89a9c654e97897161d487d87103636b3248472c319b3d4 |
|
MD5 | 4339352e13ef4efaecb27e91bb6629d3 |
|
BLAKE2b-256 | f159a3fbac011989cb9e69c90acfb9e95d4e22b89781439221c2b9f0dbded2ca |
Hashes for http_router-2.2.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd74d0217a850005a9e1f045b734e85a8a0db9756d10208d60b061d99bdae09b |
|
MD5 | 9eb307b2d9d5681bcaa3a90b07bdcd87 |
|
BLAKE2b-256 | 4483dd5f7cb30fa7f6e092e220339297f67e0482afa6461e47d6cca1dbeb945d |
Hashes for http_router-2.2.1-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | beb2b18fd062f83ffb668c8187d07678ce243926f4538fed2d7ce8da4b92978d |
|
MD5 | 68908c03b30beae01506aa6e7fad52f7 |
|
BLAKE2b-256 | 1fc27f0ca77262391e5244449b5f15d4684b646ccbaeb2acdf3f1e48dad04f4c |
Hashes for http_router-2.2.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa20e89b189bdab412b84d0f190fbfe310c98389389fa86b28e48c76b8776363 |
|
MD5 | b9f9222e25ce7906e1c5aedb81ec704e |
|
BLAKE2b-256 | 26ba5d6a91a1c2f9f2457efad463d0d0abfb4d2dd719d32d920acecd80a3859e |
Hashes for http_router-2.2.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00fdfc177e24fa234013636ca913949320ae0bf96d21f29d5056e6f79182e428 |
|
MD5 | 68fbe398d937f2272047d6241160a468 |
|
BLAKE2b-256 | a52b1d7c6b7cecb978af1783028e0a508ef085277e9e62e7a16bb51dbca3be35 |
Hashes for http_router-2.2.1-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1222b2cfcdfd60db3ffe4bbb4f25cede976d3ee71c9e64fb1fff4fd022aabbd1 |
|
MD5 | 0d8a125cbc8b6b414ea89deb1e4337fc |
|
BLAKE2b-256 | beaed40168858d4f317b09ba3ace6bc504065ff4f7a7252de8b704a43f29ea25 |