Skip to main content

dispatch request on wsgi application.

Project description

WebDispatch

https://travis-ci.org/aodag/WebDispatch.svg?branch=master https://coveralls.io/repos/aodag/WebDispatch/badge.png?branch=master

WebDispatch is dispatcher collection for wsgi application. That has no dependency to exsiting frameworks, but works fine with WebOb.

Dispatch and Generate URL

dispatch with url patterns.

example helo application:

>>> from webob.dec import wsgify
>>> @wsgify
... def greeting(request):
...     return "Hello, %s" % request.urlvars['name']

create and configure URL Dispatcher:

>>> from webdispatch import URLDispatcher
>>> dispatcher = URLDispatcher()
>>> dispatcher.add_url('top', '/hello/{name}', greeting)

invoke dispatcher as WSGI Application:

>>> from webob import Request
>>> req = Request.blank('/hello/webdispatch')
>>> res = req.get_response(dispatcher)
>>> res.body
b'Hello, webdispatch'

Wildcard

You can use wildcard after last slash. Pattern with wildcard consumes paths before the wildcard, (and make that new script_name,) and remained paths becomes new path_info.

>>> @wsgify
... def with_pathinfo(request):
...     return "Hello, %s" % request.path_info
>>> dispatcher.add_url('top', '/with_pathinfo/*', with_pathinfo)
>>> req = Request.blank('/with_pathinfo/this/is/pathinfo')
>>> res = req.get_response(dispatcher)
>>> res.body
b'Hello, this/is/pathinfo'

Type Converter

You can specify converter with varname below “:”.

>>> @wsgify
... def add(request):
...     result = request.urlvars['v1'] + request.urlvars['v2']
...     return "result, %d" % result
>>> dispatcher.add_url('add', '/add/{v1:int}/{v2:int}', add)
>>> req = Request.blank('/add/1/2')
>>> res = req.get_response(dispatcher)
>>> res.body
b'result, 3'

default converters are defined as bellow:

DEFAULT_CONVERTERS = {
    'int': int,
    'date': lambda s: datetime.strptime(s, '%Y-%m-%d'),
    'date_ym': lambda s: datetime.strptime(s, '%Y-%m'),
}

Action Dispatch

ActionDispatcher invokes object method with action name from urlvars.

action handler class:

>>> class MyHandler(object):
...     @wsgify
...     def greeting(self, request):
...         return "Hello"

create and configure ActionDispatcher:

>>> from webdispatch import ActionDispatcher
>>> actiondispatcher = ActionDispatcher()
>>> actiondispatcher.register_actionhandler(MyHandler)

add action url with urlvars named action:

>>> dispatcher.add_url('action_dispatch', '/actions/{action}', actiondispatcher)

invoke wsgi appclication.:

>>> req = Request.blank('/actions/greeting')
>>> res = req.get_response(dispatcher)
>>> res.body
b'Hello'

Method Dispatch

dispatch by HTTP METHOD restfully.

sample wsgi app:

>>> @wsgify
... def get_hello(request):
...    return "Get Hello"
>>> @wsgify
... def post_hello(request):
...    return "Post Hello"

create and configure:

>>> from webdispatch import MethodDispatcher
>>> restapp = MethodDispatcher()
>>> restapp.register_app('get', get_hello)
>>> restapp.register_app('post', post_hello)

Each applications are registered with HTTP Method name.

invoke WSGI application:

>>> req = Request.blank('/')
>>> res = req.get_response(restapp)
>>> res.body
b'Get Hello'

extra_environ

DispatchBase accepts extra_environ argument. Dispatcher adds that argument to wsgi environ by request.

Changes

1.2

  • added extra_environ argument to constructer

  • drop python2.6 tests

  • drop dependency for python2.6

  • added type converter for url vars

1.1

  • added extra_environ method to DispatchBase class

  • added support for Python 3.4

1.0.1

  • include char of “-” to urlmatch words #9

1.0

  • no changes

1.0b4

  • fix response body to bytes

1.0b3

  • fix some bugs

1.0b2

  • fix setup bug

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

WebDispatch-1.2.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

WebDispatch-1.2-py2.py3-none-any.whl (17.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file WebDispatch-1.2.tar.gz.

File metadata

  • Download URL: WebDispatch-1.2.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for WebDispatch-1.2.tar.gz
Algorithm Hash digest
SHA256 0558eb3c67408ff7b82ddd19ff692dea70bad479a6b6e363c4e5f8921ba01a3b
MD5 e26dffa335333293974b6e5720a115c5
BLAKE2b-256 f8f4b4532d8eeb920b465851f890b9dd6da539be0fcf55ee12de9096db912485

See more details on using hashes here.

File details

Details for the file WebDispatch-1.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for WebDispatch-1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 859b2e11536eb0d217a767bfa80f75728ca9409109e9a083deaf784f02e07d91
MD5 a2a06d97e7844162bbe9577e504c1b03
BLAKE2b-256 ca45c9d97a3a827ab8d526c28e5ed787daf179f92b30b912767b0e0fcc24e8c0

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