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.0.22.tar.gz (139.5 kB view details)

Uploaded Source

Built Distributions

http_router-2.0.22-cp39-cp39-win_amd64.whl (225.5 kB view details)

Uploaded CPython 3.9 Windows x86-64

http_router-2.0.22-cp39-cp39-manylinux1_x86_64.whl (524.3 kB view details)

Uploaded CPython 3.9

http_router-2.0.22-cp39-cp39-macosx_10_14_x86_64.whl (238.8 kB view details)

Uploaded CPython 3.9 macOS 10.14+ x86-64

http_router-2.0.22-cp38-cp38-win_amd64.whl (225.6 kB view details)

Uploaded CPython 3.8 Windows x86-64

http_router-2.0.22-cp38-cp38-manylinux1_x86_64.whl (553.4 kB view details)

Uploaded CPython 3.8

http_router-2.0.22-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.0.22-cp37-cp37m-win_amd64.whl (223.3 kB view details)

Uploaded CPython 3.7m Windows x86-64

http_router-2.0.22-cp37-cp37m-manylinux1_x86_64.whl (515.8 kB view details)

Uploaded CPython 3.7m

http_router-2.0.22-cp37-cp37m-macosx_10_14_x86_64.whl (234.9 kB view details)

Uploaded CPython 3.7m macOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: http-router-2.0.22.tar.gz
  • Upload date:
  • Size: 139.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for http-router-2.0.22.tar.gz
Algorithm Hash digest
SHA256 3ed93383d7f8c83444607e7d5f3daa1aeb9cb16ab223cc5efd1f65ba2eab65ba
MD5 0f5e9c1e43552690bac63cd00c2c6023
BLAKE2b-256 3cf79c09e7d012504fe059770077d54afe966e04b5b026ab394d6d21331b7eb6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.22-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 225.5 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for http_router-2.0.22-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 79a67ea1d11151545d1286409862fa19a3efed8ea057c1290e9aaaac08185f26
MD5 ba35e58ff1159dc78a5cd33867daa8fb
BLAKE2b-256 eff6542fef1d96a6fcc1a16e708714235095cf59a88f3f8bb61a87f08db1bcc7

See more details on using hashes here.

File details

Details for the file http_router-2.0.22-cp39-cp39-manylinux1_x86_64.whl.

File metadata

  • Download URL: http_router-2.0.22-cp39-cp39-manylinux1_x86_64.whl
  • Upload date:
  • Size: 524.3 kB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for http_router-2.0.22-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4fc84c668d78ed7f9203dc1b8d4626152fc65b4963ecd9b9a6e8fc7afc784cfb
MD5 7b4b6d7f65bb4114c9c84e89d6537127
BLAKE2b-256 8770130b60aecf108c0a4cefb4d545b056e67c3268e5baea114957136b2d8afe

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for http_router-2.0.22-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 ae7f7066bd98bfcf56f3ed918e689c0bcc4d852b3dc6f73af4f1731c84852a35
MD5 c9d216e423d82bb18a654f33ce7a9668
BLAKE2b-256 4b8ec88b7c7617b673105d3783ec18ef77dd51787d59d32c64bbac05cfd6f500

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.22-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 225.6 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for http_router-2.0.22-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 5466990c30f5b05890444f87f69105c936f32f0efe3cd36e69ba7f9faf06121b
MD5 34983ea5866302e158e0d260f5ade246
BLAKE2b-256 e8098e1b7a8db3e0d7e1d79eec40630785bea759875b19baebf6c9dbfef053c9

See more details on using hashes here.

File details

Details for the file http_router-2.0.22-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: http_router-2.0.22-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 553.4 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for http_router-2.0.22-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 43595834fecc3c9c56b4511beb38ff085cb7e26f740cd29586c310c5224524a9
MD5 4884999faec825210c54d5b4a31a11a7
BLAKE2b-256 ce3b51aa9616d6fe033d237fcc7a0b97e52236975f74fd21670390c69d7e8183

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.22-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.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for http_router-2.0.22-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 4cf7df12f24b865a446b7fd261cc00a42507e4120b9a4a520cf3181206d7defe
MD5 ebbdaacf299cb286140d81aa0c961307
BLAKE2b-256 19d86350047ef5428dfb540d6ed8ff8dd97cf33aaf9a8c46b3b72c183159216c

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for http_router-2.0.22-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 751fc5f8586e58fb50a8f162c653b2f59e4fc95d322a0792558fa5b6de3b2633
MD5 ad0705f167df73ff8a14f65de55cc663
BLAKE2b-256 befe72b6a49f35735c1a6928a7f58ea1e34d56a9b1775c775b25c006288f166a

See more details on using hashes here.

File details

Details for the file http_router-2.0.22-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: http_router-2.0.22-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 515.8 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for http_router-2.0.22-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 eeadfabdad549626f6e7d1dffbbc0ea30da84fb24f44e0ce857fca616db73e5c
MD5 3cf4db8a2ea02b212260d6bd4944fbc5
BLAKE2b-256 cff75256dad6c856a7fb06fb748328c2f952f92c31adbd5cacffd67f0f4a5eea

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for http_router-2.0.22-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 6d3ae781d24d24880153a72db500ee0f8e1f38e5fcc3cac641614a582739051b
MD5 9ba4938435e2012cdaeb128ca65c81d4
BLAKE2b-256 e22ae5c5db79dacde0a040d6df17e76e8794f98bb9dbb0499ea1088c29af8a10

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