Skip to content

Commit

Permalink
make APIs async and use context
Browse files Browse the repository at this point in the history
  • Loading branch information
roadscape committed Jan 16, 2019
1 parent a78fbaa commit 15a4394
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 223 deletions.
25 changes: 0 additions & 25 deletions hive/db/methods.py

This file was deleted.

40 changes: 20 additions & 20 deletions hive/server/condenser_api/call.py
Expand Up @@ -70,7 +70,7 @@ def _strict_query(params):
return query

@return_error_info
async def call(api, method, params):
async def call(context, api, method, params):
"""Routes legacy-style `call` method requests.
Example:
Expand All @@ -83,56 +83,56 @@ async def call(api, method, params):

# Follows
if method == 'get_followers':
return await get_followers(*_strict_list(params, 4))
return await get_followers(context, *_strict_list(params, 4))
elif method == 'get_following':
return await get_following(*_strict_list(params, 4))
return await get_following(context, *_strict_list(params, 4))
elif method == 'get_follow_count':
return await get_follow_count(*_strict_list(params, 1))
return await get_follow_count(context, *_strict_list(params, 1))

# Content primitives
elif method == 'get_content':
return await get_content(*_strict_list(params, 2))
return await get_content(context, *_strict_list(params, 2))
elif method == 'get_content_replies':
return await get_content_replies(*_strict_list(params, 2))
return await get_content_replies(context, *_strict_list(params, 2))

# Trending tags
elif method == 'get_trending_tags':
return await get_trending_tags(*_strict_list(params, 2))
return await get_trending_tags(context, *_strict_list(params, 2))

# Content monolith
elif method == 'get_state':
return await get_state(*_strict_list(params, 1))
return await get_state(context, *_strict_list(params, 1))

# Global discussion queries
elif method == 'get_discussions_by_trending':
return await get_discussions_by_trending(**_strict_query(params))
return await get_discussions_by_trending(context, **_strict_query(params))
elif method == 'get_discussions_by_hot':
return await get_discussions_by_hot(**_strict_query(params))
return await get_discussions_by_hot(context, **_strict_query(params))
elif method == 'get_discussions_by_promoted':
return await get_discussions_by_promoted(**_strict_query(params))
return await get_discussions_by_promoted(context, **_strict_query(params))
elif method == 'get_discussions_by_created':
return await get_discussions_by_created(**_strict_query(params))
return await get_discussions_by_created(context, **_strict_query(params))

# Account discussion queries
elif method == 'get_discussions_by_blog':
return await get_discussions_by_blog(**_strict_query(params))
return await get_discussions_by_blog(context, **_strict_query(params))
elif method == 'get_discussions_by_feed':
return await get_discussions_by_feed(**_strict_query(params))
return await get_discussions_by_feed(context, **_strict_query(params))
elif method == 'get_discussions_by_comments':
return await get_discussions_by_comments(**_strict_query(params))
return await get_discussions_by_comments(context, **_strict_query(params))
elif method == 'get_replies_by_last_update':
return await get_replies_by_last_update(*_strict_list(params, 3))
return await get_replies_by_last_update(context, *_strict_list(params, 3))

# Exotic account discussion queries
elif method == 'get_discussions_by_author_before_date':
return await get_discussions_by_author_before_date(*_strict_list(params, 4))
return await get_discussions_by_author_before_date(context, *_strict_list(params, 4))
elif method == 'get_blog':
return await get_blog(*_strict_list(params, 3, 2))
return await get_blog(context, *_strict_list(params, 3, 2))
elif method == 'get_blog_entries':
return await get_blog_entries(*_strict_list(params, 3, 2))
return await get_blog_entries(context, *_strict_list(params, 3, 2))

# Misc/dummy
elif method == 'get_account_votes':
return await get_account_votes(*_strict_list(params, 1))
return await get_account_votes(context, *_strict_list(params, 1))

raise ApiError("unknown method: %s.%s" % (api, method))
10 changes: 4 additions & 6 deletions hive/server/condenser_api/common.py
Expand Up @@ -3,8 +3,6 @@
import re
from functools import wraps

from hive.db.methods import query_one, query_col

class ApiError(Exception):
"""API-specific errors: unimplemented/bad params. Pass back to client."""
pass
Expand Down Expand Up @@ -87,13 +85,13 @@ def valid_follow_type(follow_type: str):
assert follow_type in ['blog', 'ignore'], 'invalid follow_type `%s`' % follow_type
return follow_type

def get_post_id(author, permlink):
async def get_post_id(db, author, permlink):
"""Given an author/permlink, retrieve the id from db."""
sql = ("SELECT id FROM hive_posts WHERE author = :a "
"AND permlink = :p AND is_deleted = '0' LIMIT 1")
return query_one(sql, a=author, p=permlink)
return await db.query_one(sql, a=author, p=permlink)

def get_child_ids(post_id):
async def get_child_ids(db, post_id):
"""Given a parent post id, retrieve all child ids."""
sql = "SELECT id FROM hive_posts WHERE parent_id = :id AND is_deleted = '0'"
return query_col(sql, id=post_id)
return await db.query_col(sql, id=post_id)

0 comments on commit 15a4394

Please sign in to comment.