From b7df86e7ddd2cd88fe69d4263fb12664d959e7fc Mon Sep 17 00:00:00 2001 From: Lagicrus Date: Thu, 4 Jul 2019 13:14:10 +0100 Subject: [PATCH] Updated routing docs (#1620) * Updated routing docs Updated routing docs to show all supported types as defined within https://github.com/huge-success/sanic/blob/3685b4de85ff9c6803b789d245366684765e2551/sanic/router.py#L18 Added example code for all examples besides regex Added examples of queries that work with that type and ones that would not * Tweak to call out string not str Related to https://github.com/huge-success/sanic/pull/1620#discussion_r300120962 * Changed to using code comments to achieve a mono space like display To address https://github.com/huge-success/sanic/pull/1620#discussion_r300120726 * Adjusted to list Following https://github.com/huge-success/sanic/pull/1620#discussion_r300120726 --- docs/sanic/routing.md | 58 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/docs/sanic/routing.md b/docs/sanic/routing.md index c016bd70..24b01544 100644 --- a/docs/sanic/routing.md +++ b/docs/sanic/routing.md @@ -41,27 +41,75 @@ inside the quotes. If the parameter does not match the specified type, Sanic will throw a `NotFound` exception, resulting in a `404: Page not found` error on the URL. +### Supported types + +* `string` + * "Bob" + * "Python 3" +* `int` + * 10 + * 20 + * 30 + * -10 + * (No floats work here) +* `number` + * 1 + * 1.5 + * 10 + * -10 +* `alpha` + * "Bob" + * "Python" + * (If it contains a symbol or a non alphanumeric character it will fail) +* `path` + * "hello" + * "hello.text" + * "hello world" +* `uuid` + * 123a123a-a12a-1a1a-a1a1-1a12a1a12345 (UUIDv4 Support) +* `regex expression` + +If no type is set then a string is expected. The argument given to the function will always be a string, independent of the type. + ```python from sanic.response import text -@app.route('/number/') +@app.route('/string/') +async def string_handler(request, string_arg): + return text('String - {}'.format(string_arg)) + +@app.route('/int/') async def integer_handler(request, integer_arg): - return text('Integer - {}'.format(integer_arg)) + return text('Integer - {}'.format(integer_arg)) @app.route('/number/') async def number_handler(request, number_arg): - return text('Number - {}'.format(number_arg)) + return text('Number - {}'.format(number_arg)) + +@app.route('/alpha/') +async def number_handler(request, alpha_arg): + return text('Alpha - {}'.format(alpha_arg)) + +@app.route('/path/') +async def number_handler(request, path_arg): + return text('Path - {}'.format(path_arg)) + +@app.route('/uuid/') +async def number_handler(request, uuid_arg): + return text('Uuid - {}'.format(uuid_arg)) @app.route('/person/') async def person_handler(request, name): - return text('Person - {}'.format(name)) + return text('Person - {}'.format(name)) @app.route('/folder/') async def folder_handler(request, folder_id): - return text('Folder - {}'.format(folder_id)) + return text('Folder - {}'.format(folder_id)) ``` +**Warning** `str` is not a valid type tag. If you want `str` recognition then you must use `string` + ## HTTP request types By default, a route defined on a URL will be available for only GET requests to that URL.