Skip to main content

A simple router system for HTTP applications

Project description

http-router – A simple router for HTTP applications

The library is not a HTTP framework. It’s an utilite to build the frameworks. The main goal of the library to bind targets to http routes and match them.

Tests Status PYPI Version Python Versions

Requirements

  • python 3.8, 3.9, 3.10, 3.11, 3.12, pypy3

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.route('/items/{item}')
def items():
     pass

 router = Router()
 router.route('/api')(subrouter)


match = router('/api/items/12', method='GET')
assert match, 'HTTP path is ok'
assert match.target is items
 assert match.params == {"item": "12"}

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-4.1.2.tar.gz (9.7 kB view hashes)

Uploaded Source

Built Distributions

http_router-4.1.2-cp312-cp312-win_amd64.whl (299.5 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

http_router-4.1.2-cp312-cp312-musllinux_1_1_x86_64.whl (913.4 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

http_router-4.1.2-cp312-cp312-musllinux_1_1_aarch64.whl (899.6 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ ARM64

http_router-4.1.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (897.2 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

http_router-4.1.2-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (912.1 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

http_router-4.1.2-cp312-cp312-macosx_10_9_x86_64.whl (310.6 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

http_router-4.1.2-cp312-cp312-macosx_10_9_universal2.whl (416.6 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

http_router-4.1.2-cp311-cp311-win_amd64.whl (300.0 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

http_router-4.1.2-cp311-cp311-musllinux_1_1_x86_64.whl (910.6 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

http_router-4.1.2-cp311-cp311-musllinux_1_1_aarch64.whl (901.1 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ ARM64

http_router-4.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (905.3 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

http_router-4.1.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (898.9 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

http_router-4.1.2-cp311-cp311-macosx_10_9_x86_64.whl (310.7 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

http_router-4.1.2-cp311-cp311-macosx_10_9_universal2.whl (416.4 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

http_router-4.1.2-cp310-cp310-win_amd64.whl (299.9 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

http_router-4.1.2-cp310-cp310-musllinux_1_1_x86_64.whl (853.2 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

http_router-4.1.2-cp310-cp310-musllinux_1_1_aarch64.whl (841.6 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ ARM64

http_router-4.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (843.8 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

http_router-4.1.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (835.6 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

http_router-4.1.2-cp310-cp310-macosx_10_9_x86_64.whl (310.4 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

http_router-4.1.2-cp310-cp310-macosx_10_9_universal2.whl (415.5 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

http_router-4.1.2-cp39-cp39-win_amd64.whl (300.4 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

http_router-4.1.2-cp39-cp39-musllinux_1_1_x86_64.whl (850.7 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

http_router-4.1.2-cp39-cp39-musllinux_1_1_aarch64.whl (842.1 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ ARM64

http_router-4.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (843.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

http_router-4.1.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (835.6 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

http_router-4.1.2-cp39-cp39-macosx_10_9_x86_64.whl (310.7 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

http_router-4.1.2-cp39-cp39-macosx_10_9_universal2.whl (416.2 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

http_router-4.1.2-cp38-cp38-win_amd64.whl (300.7 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

http_router-4.1.2-cp38-cp38-musllinux_1_1_x86_64.whl (884.1 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

http_router-4.1.2-cp38-cp38-musllinux_1_1_aarch64.whl (875.1 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ ARM64

http_router-4.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (854.7 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

http_router-4.1.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (846.9 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

http_router-4.1.2-cp38-cp38-macosx_10_9_x86_64.whl (310.7 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

http_router-4.1.2-cp38-cp38-macosx_10_9_universal2.whl (416.0 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

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