-
Notifications
You must be signed in to change notification settings - Fork 80
add qiita_db/studies/ endpoint #3154
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# ----------------------------------------------------------------------------- | ||
# Copyright (c) 2014--, The Qiita Development Team. | ||
# | ||
# Distributed under the terms of the BSD 3-clause License. | ||
# | ||
# The full license is in the file LICENSE, distributed with this software. | ||
# ----------------------------------------------------------------------------- | ||
|
||
from tornado.web import HTTPError | ||
|
||
from qiita_db.sql_connection import TRN | ||
from .oauth2 import OauthBaseHandler, authenticate_oauth | ||
|
||
|
||
def _generate_study_list_for_api(visibility, only_biom=True): | ||
"""Get general study information | ||
|
||
Parameters | ||
---------- | ||
visibility : string | ||
The visibility to get studies | ||
|
||
Returns | ||
------- | ||
list of dict | ||
The list of studies and their information | ||
""" | ||
artifact_type = '' | ||
if only_biom: | ||
artifact_type = "AND artifact_type = 'BIOM'" | ||
|
||
sql = f""" | ||
SELECT study_id, array_agg(DISTINCT artifact_id) FROM qiita.study | ||
INNER JOIN qiita.study_artifact USING (study_id) | ||
INNER JOIN qiita.artifact USING (artifact_id) | ||
INNER JOIN qiita.artifact_type USING (artifact_type_id) | ||
INNER JOIN qiita.visibility USING (visibility_id) | ||
WHERE visibility = %s | ||
{artifact_type} | ||
GROUP BY study_id | ||
""" | ||
with TRN: | ||
TRN.add(sql, [visibility]) | ||
return dict(TRN.execute_fetchindex()) | ||
|
||
|
||
class APIStudiesListing(OauthBaseHandler): | ||
@authenticate_oauth | ||
def get(self, visibility): | ||
"""Retrieves the studies and their BIOM artifacts in visibility | ||
|
||
Parameters | ||
---------- | ||
visibility : str {'public', 'sandbox'} | ||
The visibility of the studies and artifacts requested | ||
|
||
Returns | ||
------- | ||
see qiita_db.util.generate_study_list | ||
""" | ||
if visibility not in {'public', 'private'}: | ||
raise HTTPError( | ||
403, reason='You can only request public or private studies') | ||
|
||
response = { | ||
'data': _generate_study_list_for_api(visibility=visibility)} | ||
self.write(response) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# ----------------------------------------------------------------------------- | ||
# Copyright (c) 2014--, The Qiita Development Team. | ||
# | ||
# Distributed under the terms of the BSD 3-clause License. | ||
# | ||
# The full license is in the file LICENSE, distributed with this software. | ||
# ----------------------------------------------------------------------------- | ||
|
||
from unittest import main | ||
from json import loads | ||
|
||
from qiita_db.handlers.tests.oauthbase import OauthTestingBase | ||
|
||
|
||
class TestAPIStudiesListing(OauthTestingBase): | ||
def setUp(self): | ||
super(TestAPIStudiesListing, self).setUp() | ||
|
||
def test_get_studies_failure(self): | ||
obs = self.get('/qiita_db/studies/not-valid', headers=self.header) | ||
self.assertEqual(obs.code, 403) | ||
self.assertEqual(str(obs.error), 'HTTP 403: You can only request ' | ||
'public or private studies') | ||
|
||
def test_get_studies_private(self): | ||
obs = self.get('/qiita_db/studies/private', headers=self.header) | ||
exp = {'data': {'1': [4, 5, 6, 7]}} | ||
self.assertEqual(loads(obs.body), exp) | ||
|
||
def test_get_studies_public(self): | ||
obs = self.get('/qiita_db/studies/public', headers=self.header) | ||
exp = {'data': {}} | ||
self.assertEqual(loads(obs.body), exp) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.