diff options
| author | rr- | 2016-04-28 18:20:50 +0200 |
|---|---|---|
| committer | rr- | 2016-04-28 18:20:50 +0200 |
| commit | 0b20132a2f4726023fb63e8aeec0830fd9c4e827 (patch) | |
| tree | db690f69e737b03f5ab3d9d394a45833b1d3fbad /server | |
| parent | 2b69e9b46145f7124054bacda9857c1338e8a5d6 (diff) | |
server/api + docs/api: organize responses
Diffstat (limited to 'server')
| -rw-r--r-- | server/szurubooru/api/comment_api.py | 13 | ||||
| -rw-r--r-- | server/szurubooru/api/post_api.py | 4 | ||||
| -rw-r--r-- | server/szurubooru/api/snapshot_api.py | 2 | ||||
| -rw-r--r-- | server/szurubooru/api/tag_api.py | 2 | ||||
| -rw-r--r-- | server/szurubooru/api/tag_category_api.py | 2 | ||||
| -rw-r--r-- | server/szurubooru/api/user_api.py | 8 | ||||
| -rw-r--r-- | server/szurubooru/func/comments.py | 3 | ||||
| -rw-r--r-- | server/szurubooru/func/users.py | 3 | ||||
| -rw-r--r-- | server/szurubooru/search/search_executor.py | 4 | ||||
| -rw-r--r-- | server/szurubooru/tests/api/test_comment_retrieving.py | 2 | ||||
| -rw-r--r-- | server/szurubooru/tests/api/test_post_featuring.py | 5 | ||||
| -rw-r--r-- | server/szurubooru/tests/api/test_snapshot_retrieving.py | 2 | ||||
| -rw-r--r-- | server/szurubooru/tests/api/test_tag_category_retrieving.py | 2 | ||||
| -rw-r--r-- | server/szurubooru/tests/api/test_tag_retrieving.py | 2 | ||||
| -rw-r--r-- | server/szurubooru/tests/api/test_user_retrieving.py | 2 |
15 files changed, 32 insertions, 24 deletions
diff --git a/server/szurubooru/api/comment_api.py b/server/szurubooru/api/comment_api.py index c3afda1..1b5c413 100644 --- a/server/szurubooru/api/comment_api.py +++ b/server/szurubooru/api/comment_api.py @@ -13,8 +13,7 @@ class CommentListApi(BaseApi): auth.verify_privilege(ctx.user, 'comments:list') return self._search_executor.execute_and_serialize( ctx, - lambda comment: comments.serialize_comment(comment, ctx.user), - 'comments') + lambda comment: comments.serialize_comment(comment, ctx.user)) def post(self, ctx): auth.verify_privilege(ctx.user, 'comments:create') @@ -24,13 +23,13 @@ class CommentListApi(BaseApi): comment = comments.create_comment(ctx.user, post, text) ctx.session.add(comment) ctx.session.commit() - return {'comment': comments.serialize_comment(comment, ctx.user)} + return comments.serialize_comment_with_details(comment, ctx.user) class CommentDetailApi(BaseApi): def get(self, ctx, comment_id): auth.verify_privilege(ctx.user, 'comments:view') comment = comments.get_comment_by_id(comment_id) - return {'comment': comments.serialize_comment(comment, ctx.user)} + return comments.serialize_comment_with_details(comment, ctx.user) def put(self, ctx, comment_id): comment = comments.get_comment_by_id(comment_id) @@ -40,7 +39,7 @@ class CommentDetailApi(BaseApi): comment.last_edit_time = datetime.datetime.now() comments.update_comment_text(comment, text) ctx.session.commit() - return {'comment': comments.serialize_comment(comment, ctx.user)} + return comments.serialize_comment_with_details(comment, ctx.user) def delete(self, ctx, comment_id): comment = comments.get_comment_by_id(comment_id) @@ -57,11 +56,11 @@ class CommentScoreApi(BaseApi): comment = comments.get_comment_by_id(comment_id) scores.set_score(comment, ctx.user, score) ctx.session.commit() - return {'comment': comments.serialize_comment(comment, ctx.user)} + return comments.serialize_comment_with_details(comment, ctx.user) def delete(self, ctx, comment_id): auth.verify_privilege(ctx.user, 'comments:score') comment = comments.get_comment_by_id(comment_id) scores.delete_score(comment, ctx.user) ctx.session.commit() - return {'comment': comments.serialize_comment(comment, ctx.user)} + return comments.serialize_comment_with_details(comment, ctx.user) diff --git a/server/szurubooru/api/post_api.py b/server/szurubooru/api/post_api.py index 81992bd..0c08e82 100644 --- a/server/szurubooru/api/post_api.py +++ b/server/szurubooru/api/post_api.py @@ -43,11 +43,11 @@ class PostScoreApi(BaseApi): score = ctx.get_param_as_int('score', required=True) scores.set_score(post, ctx.user, score) ctx.session.commit() - return {'post': posts.serialize_post(post, ctx.user)} + return posts.serialize_post_with_details(post, ctx.user) def delete(self, ctx, post_id): auth.verify_privilege(ctx.user, 'posts:score') post = posts.get_post_by_id(post_id) scores.delete_score(post, ctx.user) ctx.session.commit() - return {'post': posts.serialize_post(post, ctx.user)} + return posts.serialize_post_with_details(post, ctx.user) diff --git a/server/szurubooru/api/snapshot_api.py b/server/szurubooru/api/snapshot_api.py index d0dabc1..9f16e48 100644 --- a/server/szurubooru/api/snapshot_api.py +++ b/server/szurubooru/api/snapshot_api.py @@ -10,4 +10,4 @@ class SnapshotListApi(BaseApi): def get(self, ctx): auth.verify_privilege(ctx.user, 'snapshots:list') return self._search_executor.execute_and_serialize( - ctx, snapshots.serialize_snapshot, 'snapshots') + ctx, snapshots.serialize_snapshot) diff --git a/server/szurubooru/api/tag_api.py b/server/szurubooru/api/tag_api.py index 9907821..8df1bf7 100644 --- a/server/szurubooru/api/tag_api.py +++ b/server/szurubooru/api/tag_api.py @@ -11,7 +11,7 @@ class TagListApi(BaseApi): def get(self, ctx): auth.verify_privilege(ctx.user, 'tags:list') return self._search_executor.execute_and_serialize( - ctx, tags.serialize_tag, 'tags') + ctx, tags.serialize_tag) def post(self, ctx): auth.verify_privilege(ctx.user, 'tags:create') diff --git a/server/szurubooru/api/tag_category_api.py b/server/szurubooru/api/tag_category_api.py index 798fa1f..342cbc8 100644 --- a/server/szurubooru/api/tag_category_api.py +++ b/server/szurubooru/api/tag_category_api.py @@ -6,7 +6,7 @@ class TagCategoryListApi(BaseApi): auth.verify_privilege(ctx.user, 'tag_categories:list') categories = tag_categories.get_all_categories() return { - 'tagCategories': [ + 'results': [ tag_categories.serialize_category(category) \ for category in categories], } diff --git a/server/szurubooru/api/user_api.py b/server/szurubooru/api/user_api.py index 4c91fee..ca3491c 100644 --- a/server/szurubooru/api/user_api.py +++ b/server/szurubooru/api/user_api.py @@ -10,7 +10,7 @@ class UserListApi(BaseApi): def get(self, ctx): auth.verify_privilege(ctx.user, 'users:list') return self._search_executor.execute_and_serialize( - ctx, lambda user: users.serialize_user(user, ctx.user), 'users') + ctx, lambda user: users.serialize_user(user, ctx.user)) def post(self, ctx): auth.verify_privilege(ctx.user, 'users:create') @@ -27,13 +27,13 @@ class UserListApi(BaseApi): ctx.get_file('avatar')) ctx.session.add(user) ctx.session.commit() - return {'user': users.serialize_user(user, ctx.user)} + return users.serialize_user_with_details(user, ctx.user) class UserDetailApi(BaseApi): def get(self, ctx, user_name): auth.verify_privilege(ctx.user, 'users:view') user = users.get_user_by_name(user_name) - return {'user': users.serialize_user(user, ctx.user)} + return users.serialize_user_with_details(user, ctx.user) def put(self, ctx, user_name): user = users.get_user_by_name(user_name) @@ -57,7 +57,7 @@ class UserDetailApi(BaseApi): ctx.get_param_as_string('avatarStyle'), ctx.get_file('avatar')) ctx.session.commit() - return {'user': users.serialize_user(user, ctx.user)} + return users.serialize_user_with_details(user, ctx.user) def delete(self, ctx, user_name): user = users.get_user_by_name(user_name) diff --git a/server/szurubooru/func/comments.py b/server/szurubooru/func/comments.py index edd7887..2ebb30e 100644 --- a/server/szurubooru/func/comments.py +++ b/server/szurubooru/func/comments.py @@ -18,6 +18,9 @@ def serialize_comment(comment, authenticated_user): ret['ownScore'] = scores.get_score(comment, authenticated_user) return ret +def serialize_comment_with_details(comment, authenticated_user): + return {'comment': serialize_comment(comment, authenticated_user)} + def try_get_comment_by_id(comment_id): return db.session \ .query(db.Comment) \ diff --git a/server/szurubooru/func/users.py b/server/szurubooru/func/users.py index 5a41881..d0c5a49 100644 --- a/server/szurubooru/func/users.py +++ b/server/szurubooru/func/users.py @@ -41,6 +41,9 @@ def serialize_user(user, authenticated_user): return ret +def serialize_user_with_details(user, authenticated_user): + return {'user': serialize_user(user, authenticated_user)} + def get_user_count(): return db.session.query(db.User).count() diff --git a/server/szurubooru/search/search_executor.py b/server/szurubooru/search/search_executor.py index 276973b..0f7f303 100644 --- a/server/szurubooru/search/search_executor.py +++ b/server/szurubooru/search/search_executor.py @@ -28,7 +28,7 @@ class SearchExecutor(object): .scalar() return (count, entities) - def execute_and_serialize(self, ctx, serializer, key_name): + def execute_and_serialize(self, ctx, serializer): query = ctx.get_param_as_string('query') page = ctx.get_param_as_int('page', default=1, min=1) page_size = ctx.get_param_as_int('pageSize', default=100, min=1, max=100) @@ -38,7 +38,7 @@ class SearchExecutor(object): 'page': page, 'pageSize': page_size, 'total': count, - key_name: [serializer(entity) for entity in entities], + 'results': [serializer(entity) for entity in entities], } def _prepare(self, query_text): diff --git a/server/szurubooru/tests/api/test_comment_retrieving.py b/server/szurubooru/tests/api/test_comment_retrieving.py index 1358081..411cf56 100644 --- a/server/szurubooru/tests/api/test_comment_retrieving.py +++ b/server/szurubooru/tests/api/test_comment_retrieving.py @@ -34,7 +34,7 @@ def test_retrieving_multiple(test_ctx): assert result['page'] == 1 assert result['pageSize'] == 100 assert result['total'] == 2 - assert [c['text'] for c in result['comments']] == ['text 1', 'text 2'] + assert [c['text'] for c in result['results']] == ['text 1', 'text 2'] def test_trying_to_retrieve_multiple_without_privileges(test_ctx): with pytest.raises(errors.AuthError): diff --git a/server/szurubooru/tests/api/test_post_featuring.py b/server/szurubooru/tests/api/test_post_featuring.py index 61babc7..005b04b 100644 --- a/server/szurubooru/tests/api/test_post_featuring.py +++ b/server/szurubooru/tests/api/test_post_featuring.py @@ -38,13 +38,16 @@ def test_featuring(test_ctx): assert posts.try_get_featured_post().post_id == 1 assert posts.get_post_by_id(1).is_featured assert 'post' in result - assert 'snapshots' in result assert 'id' in result['post'] + assert 'snapshots' in result + assert 'comments' in result result = test_ctx.api.get( test_ctx.context_factory( user=test_ctx.user_factory(rank='regular_user'))) assert 'post' in result assert 'id' in result['post'] + assert 'snapshots' in result + assert 'comments' in result def test_trying_to_feature_the_same_post_twice(test_ctx): db.session.add(test_ctx.post_factory(id=1)) diff --git a/server/szurubooru/tests/api/test_snapshot_retrieving.py b/server/szurubooru/tests/api/test_snapshot_retrieving.py index c6b4367..72d82c4 100644 --- a/server/szurubooru/tests/api/test_snapshot_retrieving.py +++ b/server/szurubooru/tests/api/test_snapshot_retrieving.py @@ -41,7 +41,7 @@ def test_retrieving_multiple(test_ctx): assert result['page'] == 1 assert result['pageSize'] == 100 assert result['total'] == 2 - assert len(result['snapshots']) == 2 + assert len(result['results']) == 2 def test_trying_to_retrieve_multiple_without_privileges(test_ctx): with pytest.raises(errors.AuthError): diff --git a/server/szurubooru/tests/api/test_tag_category_retrieving.py b/server/szurubooru/tests/api/test_tag_category_retrieving.py index 737dd7e..c60b602 100644 --- a/server/szurubooru/tests/api/test_tag_category_retrieving.py +++ b/server/szurubooru/tests/api/test_tag_category_retrieving.py @@ -31,7 +31,7 @@ def test_retrieving_multiple(test_ctx): result = test_ctx.list_api.get( test_ctx.context_factory( user=test_ctx.user_factory(rank='regular_user'))) - assert [cat['name'] for cat in result['tagCategories']] == ['c1', 'c2'] + assert [cat['name'] for cat in result['results']] == ['c1', 'c2'] def test_retrieving_single(test_ctx): db.session.add(test_ctx.tag_category_factory(name='cat')) diff --git a/server/szurubooru/tests/api/test_tag_retrieving.py b/server/szurubooru/tests/api/test_tag_retrieving.py index f7430b2..e7979ef 100644 --- a/server/szurubooru/tests/api/test_tag_retrieving.py +++ b/server/szurubooru/tests/api/test_tag_retrieving.py @@ -34,7 +34,7 @@ def test_retrieving_multiple(test_ctx): assert result['page'] == 1 assert result['pageSize'] == 100 assert result['total'] == 2 - assert [t['names'] for t in result['tags']] == [['t1'], ['t2']] + assert [t['names'] for t in result['results']] == [['t1'], ['t2']] def test_trying_to_retrieve_multiple_without_privileges(test_ctx): with pytest.raises(errors.AuthError): diff --git a/server/szurubooru/tests/api/test_user_retrieving.py b/server/szurubooru/tests/api/test_user_retrieving.py index e74e479..6d3ca88 100644 --- a/server/szurubooru/tests/api/test_user_retrieving.py +++ b/server/szurubooru/tests/api/test_user_retrieving.py @@ -33,7 +33,7 @@ def test_retrieving_multiple(test_ctx): assert result['page'] == 1 assert result['pageSize'] == 100 assert result['total'] == 2 - assert [u['name'] for u in result['users']] == ['u1', 'u2'] + assert [u['name'] for u in result['results']] == ['u1', 'u2'] def test_trying_to_retrieve_multiple_without_privileges(test_ctx): with pytest.raises(errors.AuthError): |