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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

http_router-1.1.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: http_router-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.7 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.2 CPython/3.8.6

File hashes

Hashes for http_router-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 84f8394cbc53bdd6aa7e40b33d339737e20fc6e7e8a657379c4e37329b864722
MD5 10c6a9c51fc4f430b7e0b9531c16a1db
BLAKE2b-256 a283dcb823e1d55034cc014d6af447e56b64bb5978739c563c66323cdb3c67e2

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