Skip to main content

A simple router for HTTP applications

Project description

http-router – A simple router for HTTP applications

Tests Status PYPI Version

Requirements

  • python >= 3.7

Installation

http-router should be installed using pip:

pip install http-router

Usage

from http_router import Router


# Initialize the router
router = Router(trim_last_slash=True)


# Plain path
# ----------
@router.route('/simple')
def simple():
    return 'simple'


# Get a binded function and options by the given path
fn, path_params = router('/simple')
print(fn, path_params)
#
# >> <function simple> None
#
assert fn() == 'simple'

# The Router will raise a Router.NotFound for an unknown path
try:
    fn, path_params = router('/unknown')
except router.NotFound as exc:
    print("%r" % exc)
    #
    # >> NotFound('/unknown', 'GET')
    #


# Multiple paths are supported as well
# ------------------------------------
@router.route('/', '/home')
def index():
    return 'index'


print(router('/'))
#
# >> RouteMatch 200 (<function index>, None)
#

print(router('/home'))
#
# >> RouteMatch 200 (<function index>, None)
#


# Bind HTTP Methods
# -----------------
@router.route('/only-post', methods=['POST'])
def only_post():
    return 'only-post'


# The Router will raise a Router.MethodNotAllowed for an unsupported method
try:
    print(router('/only-post', method='GET'))
except router.MethodNotAllowed as exc:
    print("%r" % exc)

    #
    # >> MethodNotAllowed('/only-post', 'GET')
    #

print(router('/only-post', method='POST'))
#
# >> RouteMatch 200 (<function only-post>, None)
#


# Regex Expressions are supported
# -------------------------------
@router.route('/regex(/opt)?')
def optional():
    return 'opt'


print(router('/regex', method='POST'))
#
# >> RouteMatch 200 (<function optional>, {})
#

print(router('/regex/opt', method='POST'))
#
# >> RouteMatch 200 (<function optional>, {})
#


# Dynamic routes are here
# -----------------------
@router.route('/order1/{id}')
def order1(id=None):
    return 'order-%s' % id


print(router('/order1/42'))
#
# >> RouteMatch 200 (<function order1>, {'id': '42'})
#


# Dynamic routes with regex
# -------------------------
@router.route(r'/order2/{ id:\d{3} }')
def order2(id=None):
    return 'order-%s' % id


print(router('/order2/100'))
#
# >> RouteMatch 200 (<function order1>, {'id': '100'})
#

try:
    print(router('/order2/03'))
except router.NotFound:
    pass

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 Distribution

http_router-0.6.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file http_router-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: http_router-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.6

File hashes

Hashes for http_router-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1d91350dbfba86f4e0c4d2f91e8906971a8512cd9ea05e33eccb10cce5762a8f
MD5 9062ac29f401e47042fb8da7a992de00
BLAKE2b-256 81c0a60ed11205d9896225a1e7268a41affa1e13d9c608a29ff5ae6eb1078712

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