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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.9 Windows x86-64

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

Uploaded CPython 3.9

http_router-2.1.0-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.1.0-cp38-cp38-win_amd64.whl (225.5 kB view details)

Uploaded CPython 3.8 Windows x86-64

http_router-2.1.0-cp38-cp38-manylinux1_x86_64.whl (553.3 kB view details)

Uploaded CPython 3.8

http_router-2.1.0-cp38-cp38-macosx_10_14_x86_64.whl (236.1 kB view details)

Uploaded CPython 3.8 macOS 10.14+ x86-64

http_router-2.1.0-cp37-cp37m-win_amd64.whl (223.3 kB view details)

Uploaded CPython 3.7m Windows x86-64

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

Uploaded CPython 3.7m

http_router-2.1.0-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.1.0.tar.gz.

File metadata

  • Download URL: http-router-2.1.0.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.1.0.tar.gz
Algorithm Hash digest
SHA256 e93eb9719803ec98186ed3b47668d9a176fc9690f211782615847e15cdefc3b7
MD5 1f5687bd1ca3ec7a7c210fab3d486645
BLAKE2b-256 38d35b03080c4702bf8872dfb616c3a791e29cbeb91a9dd98f95ad810f1ac0d4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.1.0-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.1.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 5dd06a4fc646371a0f854f814c09ec5853d37f8c56be15275aecab98cf8711a5
MD5 1197b2bcf92cf8ce857d93473d41497c
BLAKE2b-256 a315b8714a6072713a2eb9c82ba9f0ffe0170b45cba09b3b035a4711a4fcf037

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.1.0-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.1.0-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ef1c992c8a8d3cfee111554827efd6558f1e8018d10290bb3ab3dfc4e1e06664
MD5 0f1242c5fde702707a33d109dfd9c774
BLAKE2b-256 d4ee33e0530869a735080a4db7e72df807daeace673580b57ba866b48e78d7dc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.1.0-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.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.1.0-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 21b27507c0592fe8bb7d45aeda5f1596ba433dd6c94b87fd21f6f9b36365b388
MD5 134ac83469d6f806489a4c967f73d135
BLAKE2b-256 5cb5ca31672e29045d0653d6f69a711ab02747206c9d17dce77f20f0cc2525a9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.1.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 225.5 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.1.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 b3a9c350e92375494ce7ad02c1297359f8aafdf1beef92c88c600e4763db6a82
MD5 e1036b42490c22e6d520c86f47de1386
BLAKE2b-256 0f7dfc7dd9eb15fd8ca52ae0974545a46cc10fdfd2fb8ad5c0e7d8983fb8a726

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.1.0-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 553.3 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.1.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 87c5b1d45ffeb941bb9703c6273fa60cb035675022051b97e2ed44372487e42d
MD5 3ff0c98981b27339702aad87cc758509
BLAKE2b-256 e849fba6d577bfed86fb05e8625b7cc3b5fafa6a95320ed0d6568c4c6f8e5384

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.1.0-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 236.1 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.1.0-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 f10210262e699754b0b1041215fd834cb5bbc9031efd946ddb4089098f2f5691
MD5 0761d5339a293eca0851fd680a6c63a1
BLAKE2b-256 319c40c8bfd0c7c106b7b097b916cc252035bde53fa45bf1a38e184f2565c5ca

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.1.0-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.1.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 9da646dd667837dd7d01b863a7ed0d6fa74b2eba0fac3a93304b661d66731a31
MD5 e25f8b6d440646246b1ce5b88e4bbc0a
BLAKE2b-256 844ceee34ccfb2cab10cd8b40e5a0929e6fb043bbd38ab96b5ebfd8d85021f5e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.1.0-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.1.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2a047478ff7564e6e314ac261ab4e42134ffcb1ff8592df8786fe59facb8c29f
MD5 b93e8c44082a0466163aa9a9b1c2df12
BLAKE2b-256 d93e0ea4d02f3a73d500fe7890d6af1bd3328b3d1123b81e1e1721a6805930b6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.1.0-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.1.0-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 c8ee26d175ae243ce634f413ab6393a2b86820ed4c41c5ef95489d41fe73a1f8
MD5 61e13ce23017f436c4de2c1306d1b25d
BLAKE2b-256 878407f55b44832254e4e6f88c4c3190fe671117ca34d9f2a9117fdb713088db

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