2016-03-28 12:14:50 +00:00
|
|
|
''' Exports create_app. '''
|
|
|
|
|
2016-03-19 20:37:04 +00:00
|
|
|
import falcon
|
2016-04-18 20:41:39 +00:00
|
|
|
from szurubooru import api, errors, middleware
|
2016-03-28 20:53:56 +00:00
|
|
|
|
2016-04-03 17:00:47 +00:00
|
|
|
def _on_auth_error(ex, _request, _response, _params):
|
2016-04-03 14:04:10 +00:00
|
|
|
raise falcon.HTTPForbidden(
|
|
|
|
title='Authentication error', description=str(ex))
|
2016-03-28 12:14:50 +00:00
|
|
|
|
2016-04-03 17:00:47 +00:00
|
|
|
def _on_validation_error(ex, _request, _response, _params):
|
2016-04-03 14:04:10 +00:00
|
|
|
raise falcon.HTTPBadRequest(title='Validation error', description=str(ex))
|
2016-04-02 08:01:27 +00:00
|
|
|
|
2016-04-03 17:00:47 +00:00
|
|
|
def _on_search_error(ex, _request, _response, _params):
|
2016-04-03 14:04:10 +00:00
|
|
|
raise falcon.HTTPBadRequest(title='Search error', description=str(ex))
|
2016-04-02 12:40:10 +00:00
|
|
|
|
2016-04-03 17:00:47 +00:00
|
|
|
def _on_integrity_error(ex, _request, _response, _params):
|
2016-04-03 14:04:10 +00:00
|
|
|
raise falcon.HTTPConflict(
|
|
|
|
title='Integrity violation', description=ex.args[0])
|
|
|
|
|
2016-04-03 17:00:47 +00:00
|
|
|
def _on_not_found_error(ex, _request, _response, _params):
|
2016-04-03 14:04:10 +00:00
|
|
|
raise falcon.HTTPNotFound(title='Not found', description=str(ex))
|
2016-03-19 20:37:04 +00:00
|
|
|
|
2016-04-09 19:41:10 +00:00
|
|
|
def _on_processing_error(ex, _request, _response, _params):
|
2016-04-10 15:04:25 +00:00
|
|
|
raise falcon.HTTPBadRequest(title='Processing error', description=str(ex))
|
2016-04-09 19:41:10 +00:00
|
|
|
|
2016-04-19 10:40:08 +00:00
|
|
|
def create_method_not_allowed(allowed_methods):
|
|
|
|
allowed = ', '.join(allowed_methods)
|
2016-04-19 15:39:16 +00:00
|
|
|
def method_not_allowed(request, response, **_kwargs):
|
2016-04-19 10:40:08 +00:00
|
|
|
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
|
|
|
|
|
2016-03-19 20:37:04 +00:00
|
|
|
def create_app():
|
2016-04-03 20:03:58 +00:00
|
|
|
''' Create a WSGI compatible App object. '''
|
2016-04-19 10:40:08 +00:00
|
|
|
falcon.responders.create_method_not_allowed = create_method_not_allowed
|
|
|
|
|
2016-03-28 20:53:56 +00:00
|
|
|
app = falcon.API(
|
2016-04-15 15:54:21 +00:00
|
|
|
request_type=api.Request,
|
2016-03-28 20:53:56 +00:00
|
|
|
middleware=[
|
2016-04-03 20:03:58 +00:00
|
|
|
middleware.RequireJson(),
|
2016-04-15 15:54:21 +00:00
|
|
|
middleware.ContextAdapter(),
|
2016-04-18 20:41:39 +00:00
|
|
|
middleware.DbSession(),
|
2016-04-03 20:03:58 +00:00
|
|
|
middleware.Authenticator(),
|
2016-03-28 20:53:56 +00:00
|
|
|
])
|
2016-03-19 20:37:04 +00:00
|
|
|
|
2016-04-03 20:03:58 +00:00
|
|
|
user_list_api = api.UserListApi()
|
|
|
|
user_detail_api = api.UserDetailApi()
|
2016-04-19 09:56:09 +00:00
|
|
|
tag_category_list_api = api.TagCategoryListApi()
|
|
|
|
tag_category_detail_api = api.TagCategoryDetailApi()
|
2016-04-15 21:02:30 +00:00
|
|
|
tag_list_api = api.TagListApi()
|
|
|
|
tag_detail_api = api.TagDetailApi()
|
2016-04-20 17:02:39 +00:00
|
|
|
tag_merging_api = api.TagMergingApi()
|
2016-04-06 15:56:34 +00:00
|
|
|
password_reset_api = api.PasswordResetApi()
|
2016-04-03 20:03:58 +00:00
|
|
|
|
|
|
|
app.add_error_handler(errors.AuthError, _on_auth_error)
|
|
|
|
app.add_error_handler(errors.IntegrityError, _on_integrity_error)
|
|
|
|
app.add_error_handler(errors.ValidationError, _on_validation_error)
|
|
|
|
app.add_error_handler(errors.SearchError, _on_search_error)
|
|
|
|
app.add_error_handler(errors.NotFoundError, _on_not_found_error)
|
2016-04-09 19:41:10 +00:00
|
|
|
app.add_error_handler(errors.ProcessingError, _on_processing_error)
|
2016-03-28 12:14:50 +00:00
|
|
|
|
2016-04-03 16:00:38 +00:00
|
|
|
app.add_route('/users/', user_list_api)
|
|
|
|
app.add_route('/user/{user_name}', user_detail_api)
|
2016-04-19 09:56:09 +00:00
|
|
|
app.add_route('/tag-categories/', tag_category_list_api)
|
|
|
|
app.add_route('/tag-category/{category_name}', tag_category_detail_api)
|
2016-04-15 21:02:30 +00:00
|
|
|
app.add_route('/tags/', tag_list_api)
|
|
|
|
app.add_route('/tag/{tag_name}', tag_detail_api)
|
2016-04-20 17:02:39 +00:00
|
|
|
app.add_route('/tag-merge/', tag_merging_api)
|
2016-04-06 15:56:34 +00:00
|
|
|
app.add_route('/password-reset/{user_name}', password_reset_api)
|
2016-03-19 20:37:04 +00:00
|
|
|
|
|
|
|
return app
|