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

Uploaded Source

Built Distributions

http_router-2.2.1-cp39-cp39-win_amd64.whl (225.9 kB view details)

Uploaded CPython 3.9 Windows x86-64

http_router-2.2.1-cp39-cp39-manylinux1_x86_64.whl (526.2 kB view details)

Uploaded CPython 3.9

http_router-2.2.1-cp39-cp39-macosx_10_14_x86_64.whl (239.2 kB view details)

Uploaded CPython 3.9 macOS 10.14+ x86-64

http_router-2.2.1-cp38-cp38-win_amd64.whl (226.0 kB view details)

Uploaded CPython 3.8 Windows x86-64

http_router-2.2.1-cp38-cp38-manylinux1_x86_64.whl (555.1 kB view details)

Uploaded CPython 3.8

http_router-2.2.1-cp38-cp38-macosx_10_14_x86_64.whl (236.6 kB view details)

Uploaded CPython 3.8 macOS 10.14+ x86-64

http_router-2.2.1-cp37-cp37m-win_amd64.whl (223.8 kB view details)

Uploaded CPython 3.7m Windows x86-64

http_router-2.2.1-cp37-cp37m-manylinux1_x86_64.whl (517.8 kB view details)

Uploaded CPython 3.7m

http_router-2.2.1-cp37-cp37m-macosx_10_14_x86_64.whl (235.4 kB view details)

Uploaded CPython 3.7m macOS 10.14+ x86-64

File details

Details for the file http-router-2.2.1.tar.gz.

File metadata

  • Download URL: http-router-2.2.1.tar.gz
  • Upload date:
  • Size: 139.7 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.1.tar.gz
Algorithm Hash digest
SHA256 ecd7a017058b2d7d57d78dd2522daa85a6752bb4c88fdcb86a2ab6da6c5ef9e0
MD5 449f0afa6819058dbb8e53cd323535e6
BLAKE2b-256 7bd60fff9e8325d7d05c93e68f0bd7a66b3ffa721a8ea168089b4445e92b0905

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 225.9 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.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 73fb3303aadbb422bc5cbd7eb264261c04d5ed22fd2a2e8cfd6568ac049a9ca6
MD5 813f029ec40cf2b9077bcace573af34b
BLAKE2b-256 d4005cae009675b32016e86ce3254cc5f126718f494ba5243bb6382cb9d28905

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.1-cp39-cp39-manylinux1_x86_64.whl
  • Upload date:
  • Size: 526.2 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.1-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 88596b2966a4b20a65a54f04399bec9011d0485f354bc25780b07a6543ec3b31
MD5 dde3428e698e3e453327287b28d8033f
BLAKE2b-256 35b1d8cb9921f72bd4023398af08e231ec6342566de1bee625b778f143db5978

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.1-cp39-cp39-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 239.2 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.1-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 0b2906499f4e01d798117a7ffa05aeb9f452331888e236269bf24825f80a34bd
MD5 562d1354f3ad3e2274c693c8aa738ce4
BLAKE2b-256 7009741c5a36898e18b54b692577f5127bf84fdc392cb1cb3725376d0e468a30

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 226.0 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.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 4a5efacec11e8dbc6d89a9c654e97897161d487d87103636b3248472c319b3d4
MD5 4339352e13ef4efaecb27e91bb6629d3
BLAKE2b-256 f159a3fbac011989cb9e69c90acfb9e95d4e22b89781439221c2b9f0dbded2ca

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.1-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 555.1 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.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 bd74d0217a850005a9e1f045b734e85a8a0db9756d10208d60b061d99bdae09b
MD5 9eb307b2d9d5681bcaa3a90b07bdcd87
BLAKE2b-256 4483dd5f7cb30fa7f6e092e220339297f67e0482afa6461e47d6cca1dbeb945d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.1-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 236.6 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.1-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 beb2b18fd062f83ffb668c8187d07678ce243926f4538fed2d7ce8da4b92978d
MD5 68908c03b30beae01506aa6e7fad52f7
BLAKE2b-256 1fc27f0ca77262391e5244449b5f15d4684b646ccbaeb2acdf3f1e48dad04f4c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.1-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 223.8 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.1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 fa20e89b189bdab412b84d0f190fbfe310c98389389fa86b28e48c76b8776363
MD5 b9f9222e25ce7906e1c5aedb81ec704e
BLAKE2b-256 26ba5d6a91a1c2f9f2457efad463d0d0abfb4d2dd719d32d920acecd80a3859e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 517.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.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 00fdfc177e24fa234013636ca913949320ae0bf96d21f29d5056e6f79182e428
MD5 68fbe398d937f2272047d6241160a468
BLAKE2b-256 a52b1d7c6b7cecb978af1783028e0a508ef085277e9e62e7a16bb51dbca3be35

See more details on using hashes here.

File details

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

File metadata

  • Download URL: http_router-2.2.1-cp37-cp37m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 235.4 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.1-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 1222b2cfcdfd60db3ffe4bbb4f25cede976d3ee71c9e64fb1fff4fd022aabbd1
MD5 0d8a125cbc8b6b414ea89deb1e4337fc
BLAKE2b-256 beaed40168858d4f317b09ba3ace6bc504065ff4f7a7252de8b704a43f29ea25

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