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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.9 Windows x86-64

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

Uploaded CPython 3.8 Windows x86-64

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

Uploaded CPython 3.7m Windows x86-64

http_router-2.0.20-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.20.tar.gz.

File metadata

  • Download URL: http-router-2.0.20.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.20.tar.gz
Algorithm Hash digest
SHA256 d213731b4a549e6ccf7122355d47d9a0c9afa3bd65d8d9dc66ae54c018f45a90
MD5 aa310bc5d620f459c61f8feef4d9574a
BLAKE2b-256 2c5641f54d42902ef5c9aa632d929dd1f4504385b4a0d855ed285a5bd5a7dab6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.20-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.20-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 a3d3d5ceba6aad4ee0f025d28bc94b04eb523dcb36d0cf12c1bacc1ea24bb0cb
MD5 9af913c8cd5ef98ce24c0ee62408d205
BLAKE2b-256 8f8aa160d6b8d4f43303828ebe3135efbf9a9341eac471081e4f640c3af686e3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.20-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.20-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 d79274c508d9465fb6f7db08bf1136cc59bf7959e1e8b3b80ca5b5a225b4f996
MD5 340c4942eed80b47b51bcd3002ce8b69
BLAKE2b-256 a1b0fa8cd4d49801a94d7e6c187d30069802c080d6620aecdc81f8c0079d270b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.20-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.20-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 10bd4b3217e4b6a9a12fd7962df4dc3b86f2f12e6884ad6cf8211e2b9a2d4530
MD5 4aae8ac2c9233979b05cfbcf49ea3692
BLAKE2b-256 a2c789434c03c4981056149787d1ef89e5143d826fc286c0479f423c2708b852

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.20-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.20-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 0b10010c32056999b090fe7089b8711ff3e76adfebb28e28ba0473cfa5c41174
MD5 49acf26852cfed3a64a628c81a290c41
BLAKE2b-256 4adf3ba974582a75a1c8d9a58f439e8ef82de2197e3030d0aee1c9a4e04e0d0b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.20-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.20-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 14c104315a76ff09f50ef5eeda4b2f77149b15e36fff78b14489efdcafc3d34a
MD5 ad1f6ec64793956e223e6e44da617383
BLAKE2b-256 343a3227af70ca732be3fbfdf03158c07c1218a783cb093b82cb9ad2d770c215

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.0.20-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.20-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 2c41f9f92e3d1cec925ce795e40a29873cc3a19215d70605f28fdd247b8f6727
MD5 be3c94b9492a895686b085dacb832944
BLAKE2b-256 2b4d9211608f35b992f9fe49e85f8576706470da30a0c1fd2b652cc71859d699

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