Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Get API_Keys from Database #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions functions/db/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ def get_persisted_results(obj: Union[Review, Query], page: int = 0, page_length:
list of results
"""

if(isinstance(obj, Query)):
if (isinstance(obj, Query)):
result_collection = obj.parent_review.result_collection

elif (isinstance(obj, Review)):
result_collection = obj.result_collection

with switch_collection(Result, result_collection):
if(isinstance(obj, Query)):
if (isinstance(obj, Query)):
result_ids = obj.results
results = Result.objects.raw({"_id": {"$in": result_ids}})

Expand Down Expand Up @@ -238,7 +238,7 @@ def get_result_by_doi(review: Review, doi: str):
result object
"""
with switch_collection(Result, review.result_collection):
return Result.objects.raw({"_id": doi}).first()
return Result.objects.raw({"_id": doi}).first()


def calc_start_at(page, page_length):
Expand Down Expand Up @@ -486,6 +486,37 @@ def get_reviews(user: User) -> list:
return [review.to_son().to_dict() for review in list(reviews)],


def get_api_keys_from_user(username: None) -> dict:
"""Gets dict of api_keys for specific user from database

Args:
username: string of username, if username is None (e.g. dry_query), the username will be admin

Returns:
dictionary of all api_keys for specific user
"""
if username is None:
username = "admin"
api_keys = dict()
user = get_user_by_username(username)
for data in user.databases:
api_keys[data.db_name] = data.api_key
return api_keys


def get_username_by_review_id(review_id: str) -> str:
"""Gets username by review_id

Args:
review_id: string is review_id

Returns:
username as string
"""
review = get_review_by_id(review_id)
return review.owner.username


if __name__ == "__main__":
new_user = User(username="my_new_user").save()
other_user = User(username="my_other_user").save()
Expand Down
37 changes: 14 additions & 23 deletions functions/slr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,29 @@
db_wrappers = list()


def get_api_keys():
def get_api_keys(username: None):
"""Get api keys.

TODO: get from user collection in mongodb
Args:
username: username as string

Returns:
dict of api-keys.
dictionary keys are the same as the wrapper names defined in the wrapper module.
"""
return connector.get_api_keys_from_user(username)

api_keys = dict()
for wrapper_class in ALL_WRAPPERS:
# remove Wrapper suffix from class name
var_name = wrapper_class.__name__
if var_name.endswith('Wrapper'):
var_name = var_name[:-7]

# bring in env var format
var_name = var_name.upper()
var_name += "_API_KEY"

api_keys[wrapper_class.__name__] = os.getenv(var_name)

return api_keys


def instantiate_wrappers():
def instantiate_wrappers(username: None):
"""Instantiate wrappers with api keys.

Args:
username: username as string

Returns:
list of instantiated wrapper objects, each for each data base wrapper
"""
api_keys = get_api_keys()
api_keys = get_api_keys(username)

instantiated_wrappers = []
for wrapper_class in ALL_WRAPPERS:
Expand Down Expand Up @@ -74,7 +64,7 @@ def call_api(db_wrapper, search: dict, page: int, page_length: int):
return db_wrapper.call_api(search)


def conduct_query(search: dict, page: int, page_length="max") -> list:
def conduct_query(search: dict, page: int, page_length="max", username=None) -> list:
"""Get page of specific length. Aggregates results from all available literature data bases.

The number of results from each data base will be n/page_length with n being the number of data bases.
Expand All @@ -84,6 +74,7 @@ def conduct_query(search: dict, page: int, page_length="max") -> list:
page: page number
page_length: length of page. If set to "max", the respective maxmimum number of results
results is returned by each wrapper.
username: id of the user that is using this function

Returns:
list of results in format https://github.com/DaWeSys/backend/blob/simple_persistance/wrapper/output_format.py.
Expand All @@ -93,7 +84,7 @@ def conduct_query(search: dict, page: int, page_length="max") -> list:
results = []

if not db_wrappers:
db_wrappers = instantiate_wrappers()
db_wrappers = instantiate_wrappers(username)

if len(db_wrappers) == 0:
print("No wrappers existing.")
Expand Down Expand Up @@ -189,11 +180,11 @@ def persistent_query(query: models.Query, review: models.Review, max_num_results
"match": "AND"
}
# sample usage of dry search
#results = conduct_query(search, 1, 100)
# results = conduct_query(search, 1, 100)
pass

# sample usage of persistent query
from functions.db.connector import add_review, new_query
from functions.db.connector import add_review, new_query, get_api_keys_from_user, test111
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's test111?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Forgot to delete that


review = add_review("test REVIEW")
query = new_query(review, search)
Expand Down
3 changes: 2 additions & 1 deletion handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def persist_pages_of_query(event, *args):

review_id = event.get('pathParameters').get('review_id')
review = connector.get_review_by_id(review_id)
username = connector.get_username_by_review_id(review_id)

search = body.get('search')
query = connector.new_query(review, search)
Expand All @@ -243,7 +244,7 @@ def persist_pages_of_query(event, *args):

num_persisted = 0
for page in pages:
results = slr.conduct_query(search, page, page_length)
results = slr.conduct_query(search, page, page_length, username)
for wrapper_results in results:
connector.save_results(
wrapper_results.get('records'), review, query)
Expand Down