Skip to content

Commit

Permalink
Update docs and examples to use route.add_get and family
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Jul 24, 2016
1 parent ac0c0ed commit 1c50f9b
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 42 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ This is simple usage example:
app = web.Application()
app.router.add_route('GET', '/echo', wshandler)
app.router.add_route('GET', '/{name}', handle)
app.router.add_get('/echo', wshandler)
app.router.add_get('/{name}', handle)
web.run_app(app)
Expand Down
3 changes: 1 addition & 2 deletions demos/chat/aiohttpdemo_chat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,5 @@ async def index(request):
return resp



def setup(app):
app.router.add_route('GET', '/', index)
app.router.add_get('/', index)
10 changes: 5 additions & 5 deletions demos/polls/aiohttpdemo_polls/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@


def setup_routes(app, project_root):
app.router.add_route('GET', '/', index)
app.router.add_route('GET', '/poll/{question_id}', poll, name='poll')
app.router.add_route('GET', '/poll/{question_id}/results',
results, name='results')
app.router.add_route('POST', '/poll/{question_id}/vote', vote, name='vote')
app.router.add_get('/', index)
app.router.add_get('/poll/{question_id}', poll, name='poll')
app.router.add_get('/poll/{question_id}/results',
results, name='results')
app.router.add_post('/poll/{question_id}/vote', vote, name='vote')
app.router.add_static('/static/',
path=str(project_root / 'static'),
name='static')
2 changes: 1 addition & 1 deletion docs/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ following example::
app = Application(loop=loop)
db = await create_connection(user='user', password='123')
app['db'] = db
app.router.add_route('GET', '/', go)
app.router.add_get('/', go)
return app


Expand Down
2 changes: 1 addition & 1 deletion docs/gunicorn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ name this file *my_app_module.py*::


my_web_app = web.Application()
my_web_app.router.add_route('GET', '/', index)
my_web_app.router.add_get('/', index)


Start Gunicorn
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Server example::
return web.Response(body=text.encode('utf-8'))

app = web.Application()
app.router.add_route('GET', '/{name}', handle)
app.router.add_get('/{name}', handle)

web.run_app(app)

Expand Down
2 changes: 1 addition & 1 deletion docs/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ A simple would be::

def create_app(loop):
app = web.Application(loop=loop)
app.router.add_route('GET', '/', hello)
app.router.add_get('/', hello)
return app

async def test_hello(test_client):
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ to ``polls/aiohttpdemo_polls/routes.py``::


def setup_routes(app, project_root):
app.router.add_route('GET', '/', index)
app.router.add_get('/', index)

Now if we open browser we can see::

Expand Down
32 changes: 18 additions & 14 deletions docs/web.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ request handler with the application's :class:`router <UrlDispatcher>` on a
particular *HTTP method* and *path*::

app = web.Application()
app.router.add_route('GET', '/', hello)
app.router.add_get('/', hello)

After that, run the application by :func:`run_app` call::

Expand Down Expand Up @@ -56,7 +56,7 @@ accepts a list of any non-parsed command-line arguments and returns an

def init_function(argv):
app = web.Application()
app.router.add_route("GET", "/", index_handler)
app.router.add_get("/", index_handler)
return app


Expand All @@ -79,12 +79,13 @@ A handler **may** also be a :ref:`coroutine<coroutine>`, in which case
return web.Response()

Handlers are setup to handle requests by registering them with the
:attr:`Application.router` on a particular route (*HTTP method* and *path*
pair)::
:attr:`Application.router` on a particular route (*HTTP method* and
*path* pair) using methods like :class:`UrlDispatcher.add_get` and
:class:`UrlDispatcher.add_post`::

app.router.add_route('GET', '/', handler)
app.router.add_route('POST', '/post', post_handler)
app.router.add_route('PUT', '/put', put_handler)
app.router.add_get('/', handler)
app.router.add_post('/post', post_handler)
app.router.add_put('/put', put_handler)

:meth:`~UrlDispatcher.add_route` also supports the wildcard *HTTP method*,
allowing a handler to serve incoming requests on a *path* having **any**
Expand All @@ -109,7 +110,10 @@ Resource in turn has at least one *route*.

Route corresponds to handling *HTTP method* by calling *web handler*.

:meth:`UrlDispatcher.add_route` is just a shortcut for pair of
:meth:`UrlDispatcher.add_get` / :meth:`UrlDispatcher.add_post` and
family are plain shortcuts for :meth:`UrlDispatcher.add_route`.

:meth:`UrlDispatcher.add_route` in turn is just a shortcut for pair of
:meth:`UrlDispatcher.add_resource` and :meth:`Resource.add_route`::

resource = app.router.add_resource(path, name=name)
Expand Down Expand Up @@ -192,7 +196,7 @@ functions or coroutines::
async def hello(request):
return web.Response(body=b"Hello, world")

app.router.add_route('GET', '/', hello)
app.router.add_get('/', hello)

But sometimes it's convenient to group logically similar handlers into a Python
*class*.
Expand All @@ -214,8 +218,8 @@ application developers can organize handlers in classes if they so wish::
return web.Response(text=txt)

handler = Handler()
app.router.add_route('GET', '/intro', handler.handle_intro)
app.router.add_route('GET', '/greet/{name}', handler.handle_greeting)
app.router.add_get('/intro', handler.handle_intro)
app.router.add_get('/greet/{name}', handler.handle_greeting)


.. _aiohttp-web-class-based-views:
Expand Down Expand Up @@ -311,7 +315,7 @@ The following example shows custom routing based on the *HTTP Accept* header::
# do xml handling

chooser = AcceptChooser()
app.router.add_route('GET', '/', chooser.do_route)
app.router.add_get('/', chooser.do_route)

chooser.reg_acceptor('application/json', handle_json)
chooser.reg_acceptor('application/xml', handle_xml)
Expand Down Expand Up @@ -390,7 +394,7 @@ third-party library, :mod:`aiohttp_session`, that adds *session* support::
async def init(loop):
app = web.Application(middlewares=[session_middleware(
EncryptedCookieStorage(b'Sixteen byte key'))])
app.router.add_route('GET', '/', handler)
app.router.add_get('/', handler)
srv = await loop.create_server(
app.make_handler(), '0.0.0.0', 8080)
return srv
Expand Down Expand Up @@ -453,7 +457,7 @@ header::
return web.Response(body=b"Hello, world")

app = web.Application()
app.router.add_route('GET', '/', hello, expect_handler=check_auth)
app.router.add_get('/', hello, expect_handler=check_auth)


.. _aiohttp-web-file-upload:
Expand Down
10 changes: 10 additions & 0 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1227,26 +1227,36 @@ Router is any object that implements :class:`AbstractRouter` interface.
Shortcut for adding a GET handler. Calls the :meth:`add_route` with \
``method`` equals to ``'GET'``.

.. versionadded:: 0.23

.. method:: add_post(path, *args, **kwargs)

Shortcut for adding a POST handler. Calls the :meth:`add_route` with \
``method`` equals to ``'POST'``.

.. versionadded:: 0.23

.. method:: add_put(path, *args, **kwargs)

Shortcut for adding a PUT handler. Calls the :meth:`add_route` with \
``method`` equals to ``'PUT'``.

.. versionadded:: 0.23

.. method:: add_patch(path, *args, **kwargs)

Shortcut for adding a PATCH handler. Calls the :meth:`add_route` with \
``method`` equals to ``'PATCH'``.

.. versionadded:: 0.23

.. method:: add_delete(path, *args, **kwargs)

Shortcut for adding a DELETE handler. Calls the :meth:`add_route` with \
``method`` equals to ``'DELETE'``.

.. versionadded:: 0.23

.. method:: add_static(prefix, path, *, name=None, expect_handler=None, \
chunk_size=256*1024, response_factory=StreamResponse)

Expand Down
2 changes: 1 addition & 1 deletion examples/cli_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ def init(argv):

app = Application()
app["args"] = args
app.router.add_route('GET', '/', display_message)
app.router.add_get('/', display_message)

return app
6 changes: 3 additions & 3 deletions examples/web_classview1.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ async def index(request):

async def init(loop):
app = Application(loop=loop)
app.router.add_route('GET', '/', index)
app.router.add_route('GET', '/get', MyView)
app.router.add_route('POST', '/post', MyView)
app.router.add_get('/', index)
app.router.add_get('/get', MyView)
app.router.add_post('/post', MyView)

handler = app.make_handler()
srv = await loop.create_server(handler, '127.0.0.1', 8080)
Expand Down
6 changes: 3 additions & 3 deletions examples/web_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def logout(request):
@asyncio.coroutine
def init(loop):
app = web.Application(loop=loop)
app.router.add_route('GET', '/', root)
app.router.add_route('GET', '/login', login)
app.router.add_route('GET', '/logout', logout)
app.router.add_get('/', root)
app.router.add_get('/login', login)
app.router.add_get('/logout', logout)

handler = app.make_handler()
srv = yield from loop.create_server(handler, '127.0.0.1', 8080)
Expand Down
2 changes: 1 addition & 1 deletion examples/web_rewrite_headers_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def middleware(request):
@asyncio.coroutine
def init(loop):
app = Application(loop=loop, middlewares=[middleware_factory])
app.router.add_route('GET', '/', handler)
app.router.add_get('/', handler)

requests_handler = app.make_handler()
srv = yield from loop.create_server(requests_handler, '127.0.0.1', 8080)
Expand Down
10 changes: 5 additions & 5 deletions examples/web_srv.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ def hello(request):
@asyncio.coroutine
def init(loop):
app = Application(loop=loop)
app.router.add_route('GET', '/', intro)
app.router.add_route('GET', '/simple', simple)
app.router.add_route('GET', '/change_body', change_body)
app.router.add_route('GET', '/hello/{name}', hello)
app.router.add_route('GET', '/hello', hello)
app.router.add_get('/', intro)
app.router.add_get('/simple', simple)
app.router.add_get('/change_body', change_body)
app.router.add_get('/hello/{name}', hello)
app.router.add_get('/hello', hello)

handler = app.make_handler()
srv = yield from loop.create_server(handler, '127.0.0.1', 8080)
Expand Down
2 changes: 1 addition & 1 deletion examples/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def wshandler(request):
def init(loop):
app = Application(loop=loop)
app['sockets'] = []
app.router.add_route('GET', '/', wshandler)
app.router.add_get('/', wshandler)

handler = app.make_handler()
srv = yield from loop.create_server(handler, '127.0.0.1', 8080)
Expand Down

0 comments on commit 1c50f9b

Please sign in to comment.