API: let exceptions bubble-up instead of unnecessary catch and raise

This commit is contained in:
freamon 2024-10-08 03:26:40 +00:00
parent 29f48f58b3
commit 8bbacc3da6
11 changed files with 94 additions and 264 deletions

View file

@ -25,13 +25,7 @@ def get_community_list(auth, data):
page = int(data['page']) if data and 'page' in data else 1 page = int(data['page']) if data and 'page' in data else 1
limit = int(data['limit']) if data and 'limit' in data else 10 limit = int(data['limit']) if data and 'limit' in data else 10
if auth: user_id = authorise_api_user(auth) if auth else None
try:
user_id = authorise_api_user(auth)
except:
raise
else:
user_id = None
communities = cached_community_list(type, user_id) communities = cached_community_list(type, user_id)
@ -57,71 +51,36 @@ def get_community(auth, data):
elif 'name' in data: elif 'name' in data:
community = data['name'] community = data['name']
if auth: user_id = authorise_api_user(auth) if auth else None
try:
user_id = authorise_api_user(auth)
except:
raise
else:
user_id = None
try: community_json = community_view(community=community, variant=3, stub=False, user_id=user_id)
community_json = community_view(community=community, variant=3, stub=False, user_id=user_id) return community_json
return community_json
except:
raise
# would be in app/constants.py # would be in app/constants.py
SRC_API = 3 SRC_API = 3
def post_community_follow(auth, data): def post_community_follow(auth, data):
try: required(['community_id', 'follow'], data)
required(['community_id', 'follow'], data) integer_expected(['community_id'], data)
integer_expected(['community_id'], data) boolean_expected(['follow'], data)
boolean_expected(['follow'], data)
except:
raise
community_id = data['community_id'] community_id = data['community_id']
follow = data['follow'] follow = data['follow']
if auth: user_id = join_community(community_id, SRC_API, auth) if follow else leave_community(community_id, SRC_API, auth)
try: community_json = community_view(community=community_id, variant=4, stub=False, user_id=user_id)
user_id = authorise_api_user(auth) return community_json
except:
raise
else:
user_id = None
try:
if follow == True:
user_id = join_community(community_id, SRC_API, auth)
else:
user_id = leave_community(community_id, SRC_API, auth)
community_json = community_view(community=community_id, variant=4, stub=False, user_id=user_id)
return community_json
except:
raise
def post_community_block(auth, data): def post_community_block(auth, data):
try: required(['community_id', 'block'], data)
required(['community_id', 'block'], data) integer_expected(['community_id'], data)
integer_expected(['community_id'], data) boolean_expected(['block'], data)
boolean_expected(['block'], data)
except:
raise
community_id = data['community_id'] community_id = data['community_id']
block = data['block'] block = data['block']
try: user_id = block_community(community_id, SRC_API, auth) if block else unblock_community(community_id, SRC_API, auth)
if block == True: community_json = community_view(community=community_id, variant=5, user_id=user_id)
user_id = block_community(community_id, SRC_API, auth) return community_json
else:
user_id = unblock_community(community_id, SRC_API, auth)
community_json = community_view(community=community_id, variant=5, user_id=user_id)
return community_json
except:
raise

View file

@ -55,10 +55,7 @@ def get_post_list(auth, data, user_id=None):
limit = int(data['limit']) if data and 'limit' in data else 10 limit = int(data['limit']) if data and 'limit' in data else 10
if auth: if auth:
try: user_id = authorise_api_user(auth)
user_id = authorise_api_user(auth)
except:
raise
# user_id: the logged in user # user_id: the logged in user
# person_id: the author of the posts being requested # person_id: the author of the posts being requested
@ -92,13 +89,7 @@ def get_post(auth, data):
id = int(data['id']) id = int(data['id'])
if auth: user_id = authorise_api_user(auth) if auth else None
try:
user_id = authorise_api_user(auth)
except Exception as e:
raise e
else:
user_id = None
post_json = post_view(post=id, variant=3, user_id=user_id) post_json = post_view(post=id, variant=3, user_id=user_id)
if post_json: if post_json:
@ -111,11 +102,8 @@ def get_post(auth, data):
SRC_API = 3 SRC_API = 3
def post_post_like(auth, data): def post_post_like(auth, data):
try: required(['post_id', 'score'], data)
required(['post_id', 'score'], data) integer_expected(['post_id', 'score'], data)
integer_expected(['post_id', 'score'], data)
except:
raise
post_id = data['post_id'] post_id = data['post_id']
score = data['score'] score = data['score']
@ -127,58 +115,34 @@ def post_post_like(auth, data):
score = 0 score = 0
direction = 'reversal' direction = 'reversal'
try: user_id = vote_for_post(post_id, direction, SRC_API, auth)
user_id = vote_for_post(post_id, direction, SRC_API, auth) cache.delete_memoized(cached_post_list)
cache.delete_memoized(cached_post_list) post_json = post_view(post=post_id, variant=4, user_id=user_id, my_vote=score)
post_json = post_view(post=post_id, variant=4, user_id=user_id, my_vote=score) return post_json
return post_json
except:
raise
def put_post_save(auth, data): def put_post_save(auth, data):
try: required(['post_id', 'save'], data)
required(['post_id', 'save'], data) integer_expected(['post_id'], data)
integer_expected(['post_id'], data) boolean_expected(['save'], data)
boolean_expected(['save'], data)
except:
raise
post_id = data['post_id'] post_id = data['post_id']
save = data['save'] save = data['save']
try: user_id = bookmark_the_post(post_id, SRC_API, auth) if save else remove_the_bookmark_from_post(post_id, SRC_API, auth)
if save is True: post_json = post_view(post=post_id, variant=4, user_id=user_id)
user_id = bookmark_the_post(post_id, SRC_API, auth) return post_json
else:
user_id = remove_the_bookmark_from_post(post_id, SRC_API, auth)
post_json = post_view(post=post_id, variant=4, user_id=user_id)
return post_json
except:
raise
def put_post_subscribe(auth, data): def put_post_subscribe(auth, data):
try: required(['post_id', 'subscribe'], data)
required(['post_id', 'subscribe'], data) integer_expected(['post_id'], data)
integer_expected(['post_id'], data) boolean_expected(['subscribe'], data)
boolean_expected(['subscribe'], data)
except:
raise
post_id = data['post_id'] post_id = data['post_id']
subscribe = data['subscribe'] # not actually processed - is just a toggle subscribe = data['subscribe'] # not actually processed - is just a toggle
try: user_id = toggle_post_notification(post_id, SRC_API, auth)
user_id = toggle_post_notification(post_id, SRC_API, auth) post_json = post_view(post=post_id, variant=4, user_id=user_id)
post_json = post_view(post=post_id, variant=4, user_id=user_id) return post_json
return post_json
except:
raise

View file

@ -42,10 +42,7 @@ def get_reply_list(auth, data, user_id=None):
raise Exception('missing_parameters') raise Exception('missing_parameters')
else: else:
if auth: if auth:
try: user_id = authorise_api_user(auth)
user_id = authorise_api_user(auth)
except:
raise
replies = cached_reply_list(post_id, person_id, sort, max_depth, user_id) replies = cached_reply_list(post_id, person_id, sort, max_depth, user_id)
# user_id: the logged in user # user_id: the logged in user
@ -72,11 +69,8 @@ def get_reply_list(auth, data, user_id=None):
SRC_API = 3 SRC_API = 3
def post_reply_like(auth, data): def post_reply_like(auth, data):
try: required(['comment_id', 'score'], data)
required(['comment_id', 'score'], data) integer_expected(['comment_id', 'score'], data)
integer_expected(['comment_id', 'score'], data)
except:
raise
score = data['score'] score = data['score']
reply_id = data['comment_id'] reply_id = data['comment_id']
@ -88,51 +82,33 @@ def post_reply_like(auth, data):
score = 0 score = 0
direction = 'reversal' direction = 'reversal'
try: user_id = vote_for_reply(reply_id, direction, SRC_API, auth)
user_id = vote_for_reply(reply_id, direction, SRC_API, auth) cache.delete_memoized(cached_reply_list)
cache.delete_memoized(cached_reply_list) reply_json = reply_view(reply=reply_id, variant=4, user_id=user_id, my_vote=score)
reply_json = reply_view(reply=reply_id, variant=4, user_id=user_id, my_vote=score) return reply_json
return reply_json
except:
raise
def put_reply_save(auth, data): def put_reply_save(auth, data):
try: required(['comment_id', 'save'], data)
required(['comment_id', 'save'], data) integer_expected(['comment_id'], data)
integer_expected(['comment_id'], data) boolean_expected(['save'], data)
boolean_expected(['save'], data)
except:
raise
reply_id = data['comment_id'] reply_id = data['comment_id']
save = data['save'] save = data['save']
try: user_id = bookmark_the_post_reply(reply_id, SRC_API, auth) if save else remove_the_bookmark_from_post_reply(reply_id, SRC_API, auth)
if save is True: reply_json = reply_view(reply=reply_id, variant=4, user_id=user_id)
user_id = bookmark_the_post_reply(reply_id, SRC_API, auth) return reply_json
else:
user_id = remove_the_bookmark_from_post_reply(reply_id, SRC_API, auth)
reply_json = reply_view(reply=reply_id, variant=4, user_id=user_id)
return reply_json
except:
raise
def put_reply_subscribe(auth, data): def put_reply_subscribe(auth, data):
try: required(['comment_id', 'subscribe'], data)
required(['comment_id', 'subscribe'], data) integer_expected(['comment_id'], data)
integer_expected(['comment_id'], data) boolean_expected(['subscribe'], data)
boolean_expected(['subscribe'], data)
except:
raise
reply_id = data['comment_id'] reply_id = data['comment_id']
subscribe = data['subscribe'] # not actually processed - is just a toggle subscribe = data['subscribe'] # not actually processed - is just a toggle
try: user_id = toggle_post_reply_notification(reply_id, SRC_API, auth)
user_id = toggle_post_reply_notification(reply_id, SRC_API, auth) reply_json = reply_view(reply=reply_id, variant=4, user_id=user_id)
reply_json = reply_view(reply=reply_id, variant=4, user_id=user_id) return reply_json
return reply_json
except:
raise

View file

@ -13,10 +13,7 @@ def users_total():
def get_site(auth): def get_site(auth):
if auth: if auth:
try: user = authorise_api_user(auth, return_type='model')
user = authorise_api_user(auth, return_type='model')
except Exception as e:
raise e
else: else:
user = None user = None

View file

@ -19,48 +19,33 @@ def get_user(auth, data):
user_id = None user_id = None
if auth: if auth:
try: user_id = authorise_api_user(auth)
user_id = authorise_api_user(auth) if 'username' in data:
if 'username' in data: data['person_id'] = user_id
data['person_id'] = user_id person_id = int(user_id)
person_id = int(user_id) auth = None # avoid authenticating user again in get_post_list and get_reply_list
auth = None # avoid authenticating user again in get_post_list and get_reply_list
except Exception as e:
raise e
# bit unusual. have to help construct the json here rather than in views, to avoid circular dependencies # bit unusual. have to help construct the json here rather than in views, to avoid circular dependencies
post_list = get_post_list(auth, data, user_id) post_list = get_post_list(auth, data, user_id)
reply_list = get_reply_list(auth, data, user_id) reply_list = get_reply_list(auth, data, user_id)
try: user_json = user_view(user=person_id, variant=3)
user_json = user_view(user=person_id, variant=3) user_json['posts'] = post_list['posts']
user_json['posts'] = post_list['posts'] user_json['comments'] = reply_list['comments']
user_json['comments'] = reply_list['comments'] return user_json
return user_json
except:
raise
# would be in app/constants.py # would be in app/constants.py
SRC_API = 3 SRC_API = 3
def post_user_block(auth, data): def post_user_block(auth, data):
try: required(['person_id', 'block'], data)
required(['person_id', 'block'], data) integer_expected(['post_id'], data)
integer_expected(['post_id'], data) boolean_expected(['block'], data)
boolean_expected(['block'], data)
except:
raise
person_id = data['person_id'] person_id = data['person_id']
block = data['block'] block = data['block']
try: user_id = block_another_user(person_id, SRC_API, auth) if block else unblock_another_user(person_id, SRC_API, auth)
if block == True: user_json = user_view(user=person_id, variant=4, user_id=user_id)
user_id = block_another_user(person_id, SRC_API, auth) return user_json
else:
user_id = unblock_another_user(person_id, SRC_API, auth)
user_json = user_view(user=person_id, variant=4, user_id=user_id)
return user_json
except:
raise

View file

@ -82,12 +82,9 @@ def post_view(post: Post | int, variant, stub=False, user_id=None, my_vote=0):
'saved': saved, 'read': False, 'hidden': False, 'unread_comments': post.reply_count, 'my_vote': my_vote, 'activity_alert': activity_alert, 'saved': saved, 'read': False, 'hidden': False, 'unread_comments': post.reply_count, 'my_vote': my_vote, 'activity_alert': activity_alert,
'creator_banned_from_community': creator_banned_from_community, 'creator_is_moderator': creator_is_moderator, 'creator_is_admin': creator_is_admin} 'creator_banned_from_community': creator_banned_from_community, 'creator_is_moderator': creator_is_moderator, 'creator_is_admin': creator_is_admin}
try: creator = user_view(user=post.user_id, variant=1, stub=True)
creator = user_view(user=post.user_id, variant=1, stub=True) community = community_view(community=post.community_id, variant=1, stub=True)
community = community_view(community=post.community_id, variant=1, stub=True) v2.update({'creator': creator, 'community': community})
v2.update({'creator': creator, 'community': community})
except:
raise
return v2 return v2
@ -319,13 +316,10 @@ def reply_view(reply: PostReply | int, variant, user_id=None, my_vote=0):
v2 = {'comment': reply_view(reply=reply, variant=1), 'counts': counts, 'banned_from_community': False, 'subscribed': 'NotSubscribed', v2 = {'comment': reply_view(reply=reply, variant=1), 'counts': counts, 'banned_from_community': False, 'subscribed': 'NotSubscribed',
'saved': saved, 'creator_blocked': False, 'my_vote': my_vote, 'activity_alert': activity_alert, 'saved': saved, 'creator_blocked': False, 'my_vote': my_vote, 'activity_alert': activity_alert,
'creator_banned_from_community': creator_banned_from_community, 'creator_is_moderator': creator_is_moderator, 'creator_is_admin': creator_is_admin} 'creator_banned_from_community': creator_banned_from_community, 'creator_is_moderator': creator_is_moderator, 'creator_is_admin': creator_is_admin}
try: creator = user_view(user=reply.user_id, variant=1, stub=True)
creator = user_view(user=reply.user_id, variant=1, stub=True) community = community_view(community=reply.community_id, variant=1, stub=True)
community = community_view(community=reply.community_id, variant=1, stub=True) post = post_view(post=reply.post_id, variant=1)
post = post_view(post=reply.post_id, variant=1) v2.update({'creator': creator, 'community': community, 'post': post})
v2.update({'creator': creator, 'community': community, 'post': post})
except:
raise
return v2 return v2

View file

@ -22,11 +22,8 @@ def log_user_in(input, src):
username = input.user_name.data username = input.user_name.data
password = input.password.data password = input.password.data
elif src == SRC_API: elif src == SRC_API:
try: required(["username_or_email", "password"], input)
required(["username_or_email", "password"], input) string_expected(["username_or_email", "password"], input)
string_expected(["username_or_email", "password"], input)
except Exception:
raise
username = input['username_or_email'] username = input['username_or_email']
password = input['password'] password = input['password']

View file

@ -20,10 +20,7 @@ def join_community(community_id: int, src, auth=None, user_id=None, main_user_na
community = Community.query.get(community_id) community = Community.query.get(community_id)
if not community: if not community:
raise Exception('community_not_found') raise Exception('community_not_found')
try: user = authorise_api_user(auth, return_type='model')
user = authorise_api_user(auth, return_type='model')
except:
raise
else: else:
community = Community.query.get_or_404(community_id) community = Community.query.get_or_404(community_id)
if not user_id: if not user_id:
@ -118,10 +115,7 @@ def leave_community(community_id: int, src, auth=None):
community = Community.query.get(community_id) community = Community.query.get(community_id)
if not community: if not community:
raise Exception('community_not_found') raise Exception('community_not_found')
try: user = authorise_api_user(auth, return_type='model')
user = authorise_api_user(auth, return_type='model')
except:
raise
else: else:
community = Community.query.get_or_404(community_id) community = Community.query.get_or_404(community_id)
user = current_user user = current_user
@ -183,10 +177,7 @@ def leave_community(community_id: int, src, auth=None):
def block_community(community_id, src, auth=None): def block_community(community_id, src, auth=None):
if src == SRC_API: if src == SRC_API:
try: user_id = authorise_api_user(auth)
user_id = authorise_api_user(auth)
except:
raise
else: else:
user_id = current_user.id user_id = current_user.id
@ -204,10 +195,7 @@ def block_community(community_id, src, auth=None):
def unblock_community(community_id, src, auth=None): def unblock_community(community_id, src, auth=None):
if src == SRC_API: if src == SRC_API:
try: user_id = authorise_api_user(auth)
user_id = authorise_api_user(auth)
except:
raise
else: else:
user_id = current_user.id user_id = current_user.id

View file

@ -23,10 +23,7 @@ def vote_for_post(post_id: int, vote_direction, src, auth=None):
post = Post.query.get(post_id) post = Post.query.get(post_id)
if not post: if not post:
raise Exception('post_not_found') raise Exception('post_not_found')
try: user = authorise_api_user(auth, return_type='model')
user = authorise_api_user(auth, return_type='model')
except:
raise
else: else:
post = Post.query.get_or_404(post_id) post = Post.query.get_or_404(post_id)
user = current_user user = current_user
@ -103,10 +100,7 @@ def bookmark_the_post(post_id: int, src, auth=None):
post = Post.query.get(post_id) post = Post.query.get(post_id)
if not post or post.deleted: if not post or post.deleted:
raise Exception('post_not_found') raise Exception('post_not_found')
try: user_id = authorise_api_user(auth)
user_id = authorise_api_user(auth)
except:
raise
else: else:
post = Post.query.get_or_404(post_id) post = Post.query.get_or_404(post_id)
if post.deleted: if post.deleted:
@ -136,10 +130,7 @@ def remove_the_bookmark_from_post(post_id: int, src, auth=None):
post = Post.query.get(post_id) post = Post.query.get(post_id)
if not post or post.deleted: if not post or post.deleted:
raise Exception('post_not_found') raise Exception('post_not_found')
try: user_id = authorise_api_user(auth)
user_id = authorise_api_user(auth)
except:
raise
else: else:
post = Post.query.get_or_404(post_id) post = Post.query.get_or_404(post_id)
if post.deleted: if post.deleted:
@ -168,10 +159,7 @@ def toggle_post_notification(post_id: int, src, auth=None):
post = Post.query.get(post_id) post = Post.query.get(post_id)
if not post or post.deleted: if not post or post.deleted:
raise Exception('post_not_found') raise Exception('post_not_found')
try: user_id = authorise_api_user(auth)
user_id = authorise_api_user(auth)
except:
raise
else: else:
post = Post.query.get_or_404(post_id) post = Post.query.get_or_404(post_id)
if post.deleted: if post.deleted:

View file

@ -23,10 +23,7 @@ def vote_for_reply(reply_id: int, vote_direction, src, auth=None):
reply = PostReply.query.get(reply_id) reply = PostReply.query.get(reply_id)
if not reply: if not reply:
raise Exception('reply_not_found') raise Exception('reply_not_found')
try: user = authorise_api_user(auth, return_type='model')
user = authorise_api_user(auth, return_type='model')
except:
raise
else: else:
reply = PostReply.query.get_or_404(post_id) reply = PostReply.query.get_or_404(post_id)
user = current_user user = current_user
@ -103,10 +100,7 @@ def bookmark_the_post_reply(comment_id: int, src, auth=None):
post_reply = PostReply.query.get(comment_id) post_reply = PostReply.query.get(comment_id)
if not post_reply or post_reply.deleted: if not post_reply or post_reply.deleted:
raise Exception('comment_not_found') raise Exception('comment_not_found')
try: user_id = authorise_api_user(auth)
user_id = authorise_api_user(auth)
except:
raise
else: else:
post_reply = PostReply.query.get_or_404(comment_id) post_reply = PostReply.query.get_or_404(comment_id)
if post_reply.deleted: if post_reply.deleted:
@ -137,10 +131,7 @@ def remove_the_bookmark_from_post_reply(comment_id: int, src, auth=None):
post_reply = PostReply.query.get(comment_id) post_reply = PostReply.query.get(comment_id)
if not post_reply or post_reply.deleted: if not post_reply or post_reply.deleted:
raise Exception('comment_not_found') raise Exception('comment_not_found')
try: user_id = authorise_api_user(auth)
user_id = authorise_api_user(auth)
except:
raise
else: else:
post_reply = PostReply.query.get_or_404(comment_id) post_reply = PostReply.query.get_or_404(comment_id)
if post_reply.deleted: if post_reply.deleted:
@ -169,10 +160,7 @@ def toggle_post_reply_notification(post_reply_id: int, src, auth=None):
post_reply = PostReply.query.get(post_reply_id) post_reply = PostReply.query.get(post_reply_id)
if not post_reply or post_reply.deleted: if not post_reply or post_reply.deleted:
raise Exception('comment_not_found') raise Exception('comment_not_found')
try: user_id = authorise_api_user(auth)
user_id = authorise_api_user(auth)
except:
raise
else: else:
post_reply = PostReply.query.get_or_404(post_reply_id) post_reply = PostReply.query.get_or_404(post_reply_id)
if post_reply.deleted: if post_reply.deleted:

View file

@ -21,10 +21,7 @@ SRC_API = 3
def block_another_user(person_id, src, auth=None): def block_another_user(person_id, src, auth=None):
if src == SRC_API: if src == SRC_API:
try: user_id = authorise_api_user(auth)
user_id = authorise_api_user(auth)
except:
raise
else: else:
user_id = current_user.id user_id = current_user.id
@ -63,10 +60,7 @@ def block_another_user(person_id, src, auth=None):
def unblock_another_user(person_id, src, auth=None): def unblock_another_user(person_id, src, auth=None):
if src == SRC_API: if src == SRC_API:
try: user_id = authorise_api_user(auth)
user_id = authorise_api_user(auth)
except:
raise
else: else:
user_id = current_user.id user_id = current_user.id