Merge pull request #175 from Derrreks/master

Improving comments
This commit is contained in:
Eli Uriegas 2016-12-02 20:07:28 -06:00 committed by GitHub
commit d8a974bb4f
4 changed files with 19 additions and 10 deletions

View File

@ -67,7 +67,7 @@ class Request(dict):
try: try:
self.parsed_json = json_loads(self.body) self.parsed_json = json_loads(self.body)
except Exception: except Exception:
log.exception("failed when parsing body as json") log.exception("Failed when parsing body as json")
return self.parsed_json return self.parsed_json
@ -89,7 +89,7 @@ class Request(dict):
self.parsed_form, self.parsed_files = ( self.parsed_form, self.parsed_files = (
parse_multipart_form(self.body, boundary)) parse_multipart_form(self.body, boundary))
except Exception: except Exception:
log.exception("failed when parsing form") log.exception("Failed when parsing form")
return self.parsed_form return self.parsed_form

View File

@ -30,11 +30,17 @@ class Router:
@sanic.route('/my/url/<my_parameter>', methods=['GET', 'POST', ...]) @sanic.route('/my/url/<my_parameter>', methods=['GET', 'POST', ...])
def my_route(request, my_parameter): def my_route(request, my_parameter):
do stuff... do stuff...
or
@sanic.route('/my/url/<my_paramter>:type', methods['GET', 'POST', ...])
def my_route_with_type(request, my_parameter):
do stuff...
Parameters will be passed as keyword arguments to the request handling Parameters will be passed as keyword arguments to the request handling
function provided Parameters can also have a type by appending :type to function. Provided parameters can also have a type by appending :type to
the <parameter>. If no type is provided, a string is expected. A regular the <parameter>. Given parameter must be able to be type-casted to this.
expression can also be passed in as the type If no type is provided, a string is expected. A regular expression can
also be passed in as the type. The argument given to the function will
always be a string, independent of the type.
""" """
routes_static = None routes_static = None
routes_dynamic = None routes_dynamic = None

View File

@ -47,11 +47,11 @@ def sanic_endpoint_test(app, method='get', uri='/', gather_request=True,
return request, response return request, response
except: except:
raise ValueError( raise ValueError(
"request and response object expected, got ({})".format( "Request and response object expected, got ({})".format(
results)) results))
else: else:
try: try:
return results[0] return results[0]
except: except:
raise ValueError( raise ValueError(
"request object expected, got ({})".format(results)) "Request object expected, got ({})".format(results))

View File

@ -3,8 +3,9 @@ from .exceptions import InvalidUsage
class HTTPMethodView: class HTTPMethodView:
""" Simple class based implementation of view for the sanic. """ Simple class based implementation of view for the sanic.
You should implement methods(get, post, put, patch, delete) for the class You should implement methods (get, post, put, patch, delete) for the class
to every HTTP method you want to support. to every HTTP method you want to support.
For example: For example:
class DummyView(View): class DummyView(View):
@ -14,9 +15,11 @@ class HTTPMethodView:
def put(self, request, *args, **kwargs): def put(self, request, *args, **kwargs):
return text('I am put method') return text('I am put method')
etc. etc.
If someone try use not implemented method, there will be 405 response
If you need any url params just mention them in method definition like: 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): class DummyView(View):
def get(self, request, my_param_here, *args, **kwargs): def get(self, request, my_param_here, *args, **kwargs):