server/api: output JSON for HTTP 405
This commit is contained in:
parent
59473799a4
commit
15f734d21b
|
@ -1,5 +1,6 @@
|
||||||
''' Exports create_app. '''
|
''' Exports create_app. '''
|
||||||
|
|
||||||
|
import json
|
||||||
import falcon
|
import falcon
|
||||||
from szurubooru import api, errors, middleware
|
from szurubooru import api, errors, middleware
|
||||||
|
|
||||||
|
@ -23,8 +24,23 @@ def _on_not_found_error(ex, _request, _response, _params):
|
||||||
def _on_processing_error(ex, _request, _response, _params):
|
def _on_processing_error(ex, _request, _response, _params):
|
||||||
raise falcon.HTTPBadRequest(title='Processing error', description=str(ex))
|
raise falcon.HTTPBadRequest(title='Processing error', description=str(ex))
|
||||||
|
|
||||||
|
def create_method_not_allowed(allowed_methods):
|
||||||
|
allowed = ', '.join(allowed_methods)
|
||||||
|
|
||||||
|
def method_not_allowed(request, response, **kwargs):
|
||||||
|
response.status = falcon.status_codes.HTTP_405
|
||||||
|
response.set_header('Allow', allowed)
|
||||||
|
request.context.output = {
|
||||||
|
'title': 'Method not allowed',
|
||||||
|
'description': 'Allowed methods: %r' % allowed_methods,
|
||||||
|
}
|
||||||
|
|
||||||
|
return method_not_allowed
|
||||||
|
|
||||||
def create_app():
|
def create_app():
|
||||||
''' Create a WSGI compatible App object. '''
|
''' Create a WSGI compatible App object. '''
|
||||||
|
falcon.responders.create_method_not_allowed = create_method_not_allowed
|
||||||
|
|
||||||
app = falcon.API(
|
app = falcon.API(
|
||||||
request_type=api.Request,
|
request_type=api.Request,
|
||||||
middleware=[
|
middleware=[
|
||||||
|
|
|
@ -21,6 +21,7 @@ class ContextAdapter(object):
|
||||||
def process_request(self, request, _response):
|
def process_request(self, request, _response):
|
||||||
request.context.files = {}
|
request.context.files = {}
|
||||||
request.context.input = {}
|
request.context.input = {}
|
||||||
|
request.context.output = None
|
||||||
# pylint: disable=protected-access
|
# pylint: disable=protected-access
|
||||||
for key, value in request._params.items():
|
for key, value in request._params.items():
|
||||||
request.context.input[key] = value
|
request.context.input[key] = value
|
||||||
|
@ -53,14 +54,12 @@ class ContextAdapter(object):
|
||||||
for key, value in json.loads(body).items():
|
for key, value in json.loads(body).items():
|
||||||
request.context.input[key] = value
|
request.context.input[key] = value
|
||||||
except (ValueError, UnicodeDecodeError):
|
except (ValueError, UnicodeDecodeError):
|
||||||
raise falcon.HTTPError(
|
raise falcon.HTTPBadRequest(
|
||||||
falcon.HTTP_401,
|
|
||||||
'Malformed JSON',
|
'Malformed JSON',
|
||||||
'Could not decode the request body. The '
|
'Could not decode the request body. The '
|
||||||
'JSON was incorrect or not encoded as UTF-8.')
|
'JSON was incorrect or not encoded as UTF-8.')
|
||||||
|
|
||||||
def process_response(self, request, response, _resource):
|
def process_response(self, request, response, _resource):
|
||||||
if not request.context.output:
|
if request.context.output:
|
||||||
return
|
|
||||||
response.body = json.dumps(
|
response.body = json.dumps(
|
||||||
request.context.output, default=json_serializer, indent=2)
|
request.context.output, default=json_serializer, indent=2)
|
||||||
|
|
Loading…
Reference in New Issue