diff --git a/sanic/exceptions.py b/sanic/exceptions.py index 369a87a2..68051ebe 100644 --- a/sanic/exceptions.py +++ b/sanic/exceptions.py @@ -51,6 +51,7 @@ class Handler: def response(self, request, exception): """ Fetches and executes an exception handler and returns a response object + :param request: Request :param exception: Exception to handle :return: Response object diff --git a/sanic/request.py b/sanic/request.py index 62d89781..adbb1e0d 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -132,6 +132,7 @@ File = namedtuple('File', ['type', 'body', 'name']) def parse_multipart_form(body, boundary): """ Parses a request body and returns fields and files + :param body: Bytes request body :param boundary: Bytes multipart boundary :return: fields (RequestParameters), files (RequestParameters) diff --git a/sanic/router.py b/sanic/router.py index 4cc1f073..57d92dd5 100644 --- a/sanic/router.py +++ b/sanic/router.py @@ -26,11 +26,19 @@ class RouteExists(Exception): class Router: """ Router supports basic routing with parameters and method checks + Usage: + + .. code-block:: python + @sanic.route('/my/url/', methods=['GET', 'POST', ...]) def my_route(request, my_parameter): do stuff... + or + + .. code-block:: python + @sanic.route('/my/url/:type', methods['GET', 'POST', ...]) def my_route_with_type(request, my_parameter): do stuff... @@ -55,11 +63,12 @@ class Router: def add(self, uri, methods, handler): """ Adds a handler to the route list + :param uri: Path to match :param methods: Array of accepted method names. - If none are provided, any method is allowed + If none are provided, any method is allowed :param handler: Request handler function. - When executed, it should provide a response object. + When executed, it should provide a response object. :return: Nothing """ if uri in self.routes_all: @@ -113,6 +122,7 @@ class Router: """ Gets a request handler based on the URL of the request, or raises an error + :param request: Request object :return: handler, arguments, keyword arguments """ diff --git a/sanic/sanic.py b/sanic/sanic.py index f48b2bd5..67132f45 100644 --- a/sanic/sanic.py +++ b/sanic/sanic.py @@ -44,6 +44,7 @@ class Sanic: def route(self, uri, methods=None): """ Decorates a function to be registered as a route + :param uri: path of the URL :param methods: list or tuple of methods allowed :return: decorated function @@ -65,6 +66,7 @@ class Sanic: A helper method to register class instance or functions as a handler to the application url routes. + :param handler: function or class instance :param uri: path of the URL :param methods: list or tuple of methods allowed @@ -77,7 +79,8 @@ class Sanic: def exception(self, *exceptions): """ Decorates a function to be registered as a handler for exceptions - :param *exceptions: exceptions + + :param \*exceptions: exceptions :return: decorated function """ @@ -123,6 +126,7 @@ class Sanic: def blueprint(self, blueprint, **options): """ Registers a blueprint on the application. + :param blueprint: Blueprint object :param options: option dictionary with blueprint defaults :return: Nothing @@ -155,9 +159,10 @@ class Sanic: Takes a request from the HTTP Server and returns a response object to be sent back The HTTP Server only expects a response object, so exception handling must be done here + :param request: HTTP Request object :param response_callback: Response function to be called with the - response as the only argument + response as the only argument :return: Nothing """ try: @@ -236,20 +241,21 @@ class Sanic: """ Runs the HTTP Server and listens until keyboard interrupt or term signal. On termination, drains connections before closing. + :param host: Address to host on :param port: Port to host on :param debug: Enables debug output (slows server) :param before_start: Function to be executed before the server starts - accepting connections + accepting connections :param after_start: Function to be executed after the server starts - accepting connections + accepting connections :param before_stop: Function to be executed when a stop signal is - received before it is respected + received before it is respected :param after_stop: Function to be executed when all requests are - complete + complete :param sock: Socket for the server to accept connections from :param workers: Number of processes - received before it is respected + received before it is respected :param loop: asyncio compatible event loop :return: Nothing """ @@ -324,6 +330,7 @@ class Sanic: """ Starts multiple server processes simultaneously. Stops on interrupt and terminate signals, and drains connections when complete. + :param server_settings: kw arguments to be passed to the serve function :param workers: number of workers to launch :param stop_event: if provided, is used as a stop signal diff --git a/sanic/server.py b/sanic/server.py index 11756005..725104f0 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -201,6 +201,7 @@ def update_current_time(loop): """ Caches the current time, since it is needed at the end of every keep-alive request to update the request timeout time + :param loop: :return: """ @@ -229,13 +230,15 @@ def serve(host, port, request_handler, error_handler, before_start=None, request_max_size=None, reuse_port=False, loop=None): """ Starts asynchronous HTTP Server on an individual process. + :param host: Address to host on :param port: Port to host on :param request_handler: Sanic request handler with middleware :param after_start: Function to be executed after the server starts - listening. Takes single argument `loop` + listening. Takes single argument `loop` :param before_stop: Function to be executed when a stop signal is - received before it is respected. Takes single argumenet `loop` + received before it is respected. Takes single + argumenet `loop` :param debug: Enables debug output (slows server) :param request_timeout: time in seconds :param sock: Socket for the server to accept connections from diff --git a/sanic/static.py b/sanic/static.py index 9f5f2d52..1d0bff0f 100644 --- a/sanic/static.py +++ b/sanic/static.py @@ -15,12 +15,14 @@ def register(app, uri, file_or_directory, pattern, use_modified_since): """ Registers a static directory handler with Sanic by adding a route to the router and registering a handler. + :param app: Sanic :param file_or_directory: File or directory path to serve from :param uri: URL to serve from :param pattern: regular expression used to match files in the URL :param use_modified_since: If true, send file modified time, and return - not modified if the browser's matches the server's + not modified if the browser's matches the + server's """ # If we're not trying to match a file directly, diff --git a/sanic/views.py b/sanic/views.py index 9387bcf6..9509b5ee 100644 --- a/sanic/views.py +++ b/sanic/views.py @@ -7,21 +7,25 @@ class HTTPMethodView: to every HTTP method you want to support. For example: - class DummyView(View): + .. code-block:: python + + class DummyView(View): def get(self, request, *args, **kwargs): return text('I am get method') - def put(self, request, *args, **kwargs): return text('I am put method') + etc. If someone tries to use a non-implemented method, there will be a 405 response. If you need any url params just mention them in method definition: - class DummyView(View): + .. code-block:: python + + class DummyView(View): def get(self, request, my_param_here, *args, **kwargs): return text('I am get method with %s' % my_param_here)