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

Uploaded Source

Built Distributions

http_router-2.2.0-cp39-cp39-win_amd64.whl (225.4 kB view details)

Uploaded CPython 3.9 Windows x86-64

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

Uploaded CPython 3.9

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

Uploaded CPython 3.8 Windows x86-64

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

Uploaded CPython 3.8

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

Uploaded CPython 3.7m Windows x86-64

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

Uploaded CPython 3.7m

http_router-2.2.0-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.2.0.tar.gz.

File metadata

  • Download URL: http-router-2.2.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.2.0.tar.gz
Algorithm Hash digest
SHA256 efa4a70a18be91becededae1b888b265affaf18c9b08677366abcd53194c0a26
MD5 9e68fdbe5a501b7576a715006e6e0d3b
BLAKE2b-256 53987497f7563a4dd9d31710f791c8fa88b287fd90eea3aa9f912e8108adbcca

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 225.4 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.2.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 bc7bc7717212748cd18f305bdc06ab05564342d42a9d274e46f1e9f2b74bd841
MD5 08558efb7c38966995a90d98d91a6b81
BLAKE2b-256 ec2efe4989bb0919b537d3203e6d6719b3ef29fad802f2e77aec4757124b95ca

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.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.2.0-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7a476e5a2100455089ec8ba3bf72110a0d08c028079cea9dcbf97dab06f915c1
MD5 99bea362a7bb9e675e2270d31173f505
BLAKE2b-256 4b10dd6e05e3c720f25434133bcdcb0033db8acfdfd0a3e16dcd27c50938ce95

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.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.2.0-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 c8877fc03b1ccb63cc4d8f38facae502de62e3b6162b3763a4a3c7d0168d74c0
MD5 d7afafd3db5b7579740c850ce7c955cd
BLAKE2b-256 d0da4cb913efddd06a0109e386bb9585e2e97f29366880ec0d548c31914554a5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.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.2.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 1be753e661f341d9be245a1ff48419d859e6e071116b00f96030bd038626a0c1
MD5 7aeb583ec5a0981dd8f2fd16a5219f42
BLAKE2b-256 7b92e1a2595a15c83759a56ebd214ac4425068aa18b30559d9b581dcd668eac3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.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.2.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d6ded0f94720eb13cca438d1a37ead8636b912aa83e16cc36e9ffce74f7f4607
MD5 077fc138ede903104fa21fffeb63511c
BLAKE2b-256 64fa0f0e61da7de87b03e55a6c81571b5a48b5ca77db1d8f81b6eaa3a8610126

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.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.2.0-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 ceda995d946580fab6f1bce77b84a7f23d7dd8c036e2b6ba51ff42b0c3d80110
MD5 dda08b7acc13567a11d8118feddf7ccc
BLAKE2b-256 216857cb1a9c701e5d3f643b928a225ab2796db4d86d8c3c55e9686277edb64c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.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.2.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 e369d22e1a75a1a2ffb6a411288e4a6c2afb4add63bf86140a0689ca2aac0235
MD5 8b13784a1b6209e0d40eb4656ee3ed33
BLAKE2b-256 5723a9a41d8f62c4e143bdd7744322f7c253d2ca38047c33b74011aa475b52ed

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.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.2.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 49dc8f66304d75997a2e2ed418a6cd1c440321e49ea21c0caeaa6683b34bf49d
MD5 bea6d988da75dc3a2d14aa975141483e
BLAKE2b-256 67cd80a95213f4a55781a12390d7aba1864f19fc7fea358663a5c9255a559611

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.0-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.4

File hashes

Hashes for http_router-2.2.0-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 31838b4209c54f099d5a0a5a8d2ac0585ae9a255ce1abe34d6fcc7d8b278d339
MD5 70f5a872957e3bdd937291aa0852bbdf
BLAKE2b-256 59403906cf4750617724601c1470d8865ab0b47894d33726c4d01f7e37ab7ebf

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