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

Uploaded Source

Built Distributions

http_router-2.4.1-cp39-cp39-win_amd64.whl (226.1 kB view details)

Uploaded CPython 3.9 Windows x86-64

http_router-2.4.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.4.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.4.1-cp38-cp38-win_amd64.whl (226.3 kB view details)

Uploaded CPython 3.8 Windows x86-64

http_router-2.4.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.4.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.4.1-cp37-cp37m-win_amd64.whl (223.7 kB view details)

Uploaded CPython 3.7m Windows x86-64

http_router-2.4.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (517.3 kB view details)

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

http_router-2.4.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.4.1.tar.gz.

File metadata

  • Download URL: http-router-2.4.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.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for http-router-2.4.1.tar.gz
Algorithm Hash digest
SHA256 f762b59a9f900de1e36ac7a9fef85351d5647fff741446b9bb0aa10c0292e2c7
MD5 2778566592a03c67a81833218b9e50a3
BLAKE2b-256 3e4eaf95c1a164a1a2a65b1fd33324ce075e8d3137cd4e9d915925e9ac24319f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.4.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 226.1 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.5

File hashes

Hashes for http_router-2.4.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 fc071673313d1243ead6b739cc72a4f35c799decc9ae4094a2a41b1c2ca9a0e7
MD5 56ba972f72da67f2c79231e0fbf72fa5
BLAKE2b-256 2f22987d20e2540673550fac574fd863643392f9f03797e60c38f46c3dbb3bee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for http_router-2.4.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1c7956001e4ec5d6ff3f705e7cad27d6dee7c6e0b1c7a57ae46c11e5b1f59666
MD5 215e677ce8a2fb14f7027531734e2ec3
BLAKE2b-256 16ae23b20ef403b88b13b595f6894a0101d6ca86c618e893eb714e86c71cd4bd

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for http_router-2.4.1-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 93da0dc3db4cde3955521f02bdfc58041162103f32e2cd06164d73bec99f909c
MD5 f5c904dd780de2a352bc25164f24909a
BLAKE2b-256 f2391326923c0ab1bbeaa967b99e2a1187d7cce62a9c6460f67459758d4094ca

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.4.1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 226.3 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.5

File hashes

Hashes for http_router-2.4.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 b581eeab2169c4e2840469d404eac62fe8f22737eea6d16673202ced6f932178
MD5 7ebbb4bf956ad9ad77fb633bc57f6895
BLAKE2b-256 555899c5f04aa6830835018784d1b29649fa699f70625676af87e09d906dc857

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for http_router-2.4.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6446ddb1e590bb670785c411cb62f749a71f913c9cd49a3aef173138103bc240
MD5 b42be888f0e1f116a948818862e8d6ba
BLAKE2b-256 27c4f3d8da9974cba693480156367ff0709f7f8aeb598910f7ea7a678d3f0de2

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for http_router-2.4.1-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 4bb18328711346c4b7c2aa18c3ce21e68b5fd06a79583a3d45c42e1ce0ca06a6
MD5 c3ef14dca7da5df851990b8ed0d3623e
BLAKE2b-256 c67a30c8f44a0b6bdd93595f281a22a8befd4ce2e508a33108cb6f0f4d68d4b8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.4.1-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 223.7 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.5

File hashes

Hashes for http_router-2.4.1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 a7c660718383e5717591dae0860aab2355c97a0735c1cb9713cec82bbcf0df0b
MD5 0fc4654837aa0b8efd551336a92c398e
BLAKE2b-256 1eaf9203d0a0cbb847ae564a17a58f9b1607f3f1cb9706803a0e6880ee3d5422

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for http_router-2.4.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 01c1e6785b952523a9e30bdd2a7e0bdf531722370d050e6989fa5903728f9a62
MD5 99d2a2859bd3f36a320f1d75869f8219
BLAKE2b-256 1bf31f7718c596a577dd070b6c3fd96571711819abf5e43985b89537dbea2ea8

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for http_router-2.4.1-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 d111aadaf39bc2388fc73c17d381a22f30e4e8145bb47a0d8107374ad8bc08d0
MD5 f09d682fc5aea144c875a80004829416
BLAKE2b-256 0721175b13afc6d42689777423f1b5d65f7d953136893507b6c4e94d44c3d086

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