Skip to main content

A simple router for HTTP applications

Project description

http-router – A simple router for HTTP applications

Tests Status PYPI Version Python Versions

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

http-router-2.5.1.tar.gz (139.6 kB view details)

Uploaded Source

Built Distributions

http_router-2.5.1-cp39-cp39-win_amd64.whl (226.3 kB view details)

Uploaded CPython 3.9 Windows x86-64

http_router-2.5.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (527.7 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.5+ x86-64

http_router-2.5.1-cp39-cp39-macosx_10_14_x86_64.whl (238.7 kB view details)

Uploaded CPython 3.9 macOS 10.14+ x86-64

http_router-2.5.1-cp38-cp38-win_amd64.whl (226.4 kB view details)

Uploaded CPython 3.8 Windows x86-64

http_router-2.5.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (554.9 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.5+ x86-64

http_router-2.5.1-cp38-cp38-macosx_10_14_x86_64.whl (236.2 kB view details)

Uploaded CPython 3.8 macOS 10.14+ x86-64

http_router-2.5.1-cp37-cp37m-win_amd64.whl (223.8 kB view details)

Uploaded CPython 3.7m Windows x86-64

http_router-2.5.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (517.4 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.5+ x86-64

http_router-2.5.1-cp37-cp37m-macosx_10_14_x86_64.whl (234.8 kB view details)

Uploaded CPython 3.7m macOS 10.14+ x86-64

File details

Details for the file http-router-2.5.1.tar.gz.

File metadata

  • Download URL: http-router-2.5.1.tar.gz
  • Upload date:
  • Size: 139.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.6

File hashes

Hashes for http-router-2.5.1.tar.gz
Algorithm Hash digest
SHA256 f61c88616a5e45e8788a0a2ae22c44d8a1988210221ef2f55b12a199faec29bf
MD5 2cf573af1dbcf6e8ff04ce4ef3c6da2b
BLAKE2b-256 1bd6d18969d0aae83fd3c21d40cc2636ff62be256950fa5ebb75695eadff3354

See more details on using hashes here.

File details

Details for the file http_router-2.5.1-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: http_router-2.5.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 226.3 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.6

File hashes

Hashes for http_router-2.5.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 5d9bae3d0088b6645b0ac08ea4a50e305da0f0506625d9391467ff317e8460a6
MD5 dd73a991fbaad856dac121c64bbf9be8
BLAKE2b-256 6d7f1a7aeb1edec13599bf2329a1f65be6870250bedf6454e07003551c6fca26

See more details on using hashes here.

File details

Details for the file http_router-2.5.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for http_router-2.5.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 96db6f5330b19f50c6f835137898d0086204b0a280481cb12ad967482329d63a
MD5 c06ef98ce495ea9fb4e3fc6b40b20fa6
BLAKE2b-256 fc01337a5565e3087c4a225b2b6b2c8a98f53d8b4ccf0f7a449b0ad920a6e252

See more details on using hashes here.

File details

Details for the file http_router-2.5.1-cp39-cp39-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: http_router-2.5.1-cp39-cp39-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 238.7 kB
  • Tags: CPython 3.9, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.6

File hashes

Hashes for http_router-2.5.1-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 8392bc740f0c349c870963b92903fc0fa3fe2bc1030c23e560deb4700f5d43cb
MD5 1cd6928db5c57266e65a10992681ab80
BLAKE2b-256 0078f6ec823597e3111638920aa415b1dbec7071c61f8304c5269af7a9162de2

See more details on using hashes here.

File details

Details for the file http_router-2.5.1-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: http_router-2.5.1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 226.4 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.6

File hashes

Hashes for http_router-2.5.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 48688f61f7fd62ecd4dcc578445d78a421ed9122f0f60efab7ca2893d6646ed2
MD5 6c69e6c92c3428e54874db123f4a9972
BLAKE2b-256 5860341987c0ec6811f87af4aba8e1dc845917e8e013db6d9cabd47255ce0c2a

See more details on using hashes here.

File details

Details for the file http_router-2.5.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for http_router-2.5.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3b335656c7100624fc15e84be8016eee70242aa195f8b69a8deafadfc33d5f70
MD5 10073d25a5ceab76f24197f1017a42bb
BLAKE2b-256 64dc2bc3f60bf3f0a1c10ba17b28400d10784930aaca3dc08fe33bf00fe5c6cb

See more details on using hashes here.

File details

Details for the file http_router-2.5.1-cp38-cp38-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: http_router-2.5.1-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 236.2 kB
  • Tags: CPython 3.8, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.6

File hashes

Hashes for http_router-2.5.1-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 1205c1bc69e9733d503d69e1da05941180c1dc9cbf63d65363cd9e9f7c77964a
MD5 5d8b7c5851385b371332025635b85ded
BLAKE2b-256 a078b375c476a681278205ed3e3fef73a3d43446b00b01fbd232e3f73b06febd

See more details on using hashes here.

File details

Details for the file http_router-2.5.1-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: http_router-2.5.1-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 223.8 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.6

File hashes

Hashes for http_router-2.5.1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 cdf0fd40761fa8cfdb788939ad89ce60d23acc4b6feca5b1ae0a9e2ed5e15f2a
MD5 9e2aacfd19f081d77a6b50d215574fe9
BLAKE2b-256 1e77e91a2a47c3991f17b4d73494231a1994fe1745348a9ed6f1444d2f5beb44

See more details on using hashes here.

File details

Details for the file http_router-2.5.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for http_router-2.5.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a2cf683e062794a9dbe1512011b9519f4c36bb61bec1da9ede2f1a3b26a1da06
MD5 a17e1743d1cdd9d7a5b45cca741e0acf
BLAKE2b-256 fa74aa94ef4d74d1c08d3f9b77bba5754a1c4dfb1d517a1f375d8d8c7627ba83

See more details on using hashes here.

File details

Details for the file http_router-2.5.1-cp37-cp37m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: http_router-2.5.1-cp37-cp37m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 234.8 kB
  • Tags: CPython 3.7m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.6

File hashes

Hashes for http_router-2.5.1-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 901fc095e927fcbe1e0ca374a892afde96fed02c0f95ae870fbd35d1101c7258
MD5 d70aaa671b609fc468c851ad7b3be60c
BLAKE2b-256 dc16164d49f8ddad05fa51e3710459cb8eee2797abab1d49d591c76879285436

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page