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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.9 Windows x86-64

http_router-2.0.19-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.19-cp38-cp38-win_amd64.whl (225.6 kB view details)

Uploaded CPython 3.8 Windows x86-64

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

Uploaded CPython 3.7m Windows x86-64

http_router-2.0.19-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.19.tar.gz.

File metadata

  • Download URL: http-router-2.0.19.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.19.tar.gz
Algorithm Hash digest
SHA256 55349cc0063e2876e3450e0c59e8baa03998732d121beeae039e6f2c059b3fba
MD5 5a4a7c165f30d7a53bee619c302ed871
BLAKE2b-256 057d2cf6990810cb208e9952c5945a7b417af3603eacdb3dd2821eb16b7beaca

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.19-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.19-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 dbcc3495350bc01d3fc7d789a88a29ad9542b679b38db219ae3ab3325eb8cb18
MD5 87c8e5bb45788d586aec13656062ef68
BLAKE2b-256 218bf94b1ef3480a4a35b24c500b8d72e05784aee6ade3dc0f303b9989ca9ae4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.19-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.19-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 846604b11cec3977a50a6a256cf38066fd1b8c5724d40e7c62b07a5eefeaac4e
MD5 290e44e4e585236a632cfbe9afe8c5f8
BLAKE2b-256 74270645988c12ed77e8c60580df54afd8a0ba447c0fd6c62dec125155598d2b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.19-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.19-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 863d770322a0ad209a16a8626f78840b4c00e0219d6359209197dc28cde717d7
MD5 be38b5ce4517b5acc0bb39539dd51277
BLAKE2b-256 ca55a54f3c47776fbafb6c3fef0a2600d7860dac13359f54ae0f317929175a66

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.19-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.19-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 6375165baa24d8d58546601c94cd304084c06d1da0a93d18e5fbd839c7f1bd3a
MD5 c7e2cc9fbc3b48e26a58930b889237e5
BLAKE2b-256 2765fab0c0f4ee1c8bbf67963615e5d9e077f59caac38ed41fd58bb421475f8a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.19-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.19-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 274c097aab1a52d8ca14d093efb22d4cd8940fe318a36a7332bbf7977be5a4e5
MD5 522c7f4bf9a5b79589a7798e30907f27
BLAKE2b-256 a5d226c4d2959ef3d49d77dd11e9969fe8fd3f2d428d3f2aaf41fa4a88d0cb58

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.19-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.19-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 5b2283105df5f2e4b62cd003d26e8941ef71388de82802ffe952b2f742a56ea6
MD5 2a6791416282ef071dda27fd97dff76b
BLAKE2b-256 d77ba4f62c957a5f96a19e4ccdb7860b8bb786ebd8c1096c1febdfd476420f50

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