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
|
|
|
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-24 14:34:06 +00:00
|
|
|
app.add_route('/users/', api.UserListApi())
|
|
|
|
app.add_route('/user/{user_name}', api.UserDetailApi())
|
|
|
|
app.add_route('/password-reset/{user_name}', api.PasswordResetApi())
|
|
|
|
|
|
|
|
app.add_route('/tag-categories/', api.TagCategoryListApi())
|
|
|
|
app.add_route('/tag-category/{category_name}', api.TagCategoryDetailApi())
|
|
|
|
app.add_route('/tags/', api.TagListApi())
|
|
|
|
app.add_route('/tag/{tag_name}', api.TagDetailApi())
|
|
|
|
app.add_route('/tag-merge/', api.TagMergeApi())
|
|
|
|
app.add_route('/tag-siblings/{tag_name}', api.TagSiblingsApi())
|
|
|
|
|
2016-04-25 08:48:15 +00:00
|
|
|
app.add_route('/post/{post_id}', api.PostDetailApi())
|
2016-04-24 14:34:06 +00:00
|
|
|
app.add_route('/post/{post_id}/score', api.PostScoreApi())
|
2016-04-28 17:04:44 +00:00
|
|
|
app.add_route('/post/{post_id}/favorite', api.PostFavoriteApi())
|
2016-04-24 14:34:06 +00:00
|
|
|
|
|
|
|
app.add_route('/comments/', api.CommentListApi())
|
|
|
|
app.add_route('/comment/{comment_id}', api.CommentDetailApi())
|
|
|
|
app.add_route('/comment/{comment_id}/score', api.CommentScoreApi())
|
|
|
|
|
|
|
|
app.add_route('/info/', api.InfoApi())
|
|
|
|
app.add_route('/featured-post/', api.PostFeatureApi())
|
|
|
|
app.add_route('/snapshots/', api.SnapshotListApi())
|
2016-03-19 20:37:04 +00:00
|
|
|
|
|
|
|
return app
|