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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

Uploaded CPython 3.9 Windows x86-64

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

Uploaded CPython 3.8 Windows x86-64

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

Uploaded CPython 3.7m Windows x86-64

http_router-2.0.17-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.17-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: http_router-2.0.17-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.17-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 f6d1f0cfe7ee00106f5562626e89fb0449b481f5e4e3004aa5bb65579a6ed0d4
MD5 0ecb8f4f81fa36a92a5fe828213c2b57
BLAKE2b-256 0bbd9b85b9557048966051fa82b5e70a4b281842c08ec518e665758eef907b00

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.17-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.17-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 7590f3944fad9ec0c4da7fc0b3c1a20659200f3b0c45b0d24e0196f86c1dbca9
MD5 557d1689dd7f22141bab298ba31d57d1
BLAKE2b-256 bd60e45d0d0b253a6b6392de10b4e3e5b063ff169b3e2ed42ebc461c06ed3aac

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.17-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.17-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 782132f63420de85b73fb1a0f3ed16cd6898ba11f0e719df419293110b98d769
MD5 5d278abe8f092e673721abb34536354b
BLAKE2b-256 e91bacdb453f39e5228b51bbb5f5aa045bd3b0975e2ad819dec987d7b164883c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.17-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.17-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 0184d7cfcc0294fa68ac2d9ea2a76ae3c9b4df24e92054449bf828049b2a277e
MD5 8ce5aca000c1d801f557ac6323a3ba15
BLAKE2b-256 fc7981f49cf3b4d70ad7fa9ddd926039d489d92e4792edc89b913b4758332578

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.17-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.17-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 8e9e6c1edc00b2cd9eea7fa2cef7ec1b2fc6d0217dcbfe451807f4bcf085f9cf
MD5 40ba5c49ffe50b9346d044372578cb3f
BLAKE2b-256 fc81f55f06ed3b25b9cdf5b86bd76aaa1fb745d5b03c145210ddff90d31a557f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.17-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.17-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 f562c4536bddacd2ba808663ee3ffedbacacb21d83f3c5eaa3f24b528d58ce8d
MD5 e8bd326399eeb502765e9659b77b1188
BLAKE2b-256 4716008cc3e6971aa6208f7f5c014d01769d3648a230658bc32e8775db9d6577

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