forked from data-exp-lab/girder_ythub
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Dataverse External Tools integration
- Loading branch information
1 parent
577835a
commit c706ac7
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,44 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
from tests import base | ||
|
||
|
||
def setUpModule(): | ||
base.enabledPlugins.append('wholetale') | ||
base.startServer() | ||
|
||
|
||
def tearDownModule(): | ||
base.stopServer() | ||
|
||
|
||
class IntegrationTestCase(base.TestCase): | ||
|
||
def testDataverseIntegration(self): | ||
resp = self.request( | ||
'/integration/dataverse', method='GET', | ||
params={'fileId': 'blah', 'siteUrl': 'https://dataverse.someplace'}) | ||
self.assertStatus(resp, 400) | ||
self.assertEqual(resp.json, { | ||
'message': 'Invalid fileId (should be integer)', | ||
'type': 'rest' | ||
}) | ||
|
||
resp = self.request( | ||
'/integration/dataverse', method='GET', | ||
params={'fileId': '1234', 'siteUrl': 'definitely not a URL'}) | ||
self.assertStatus(resp, 400) | ||
self.assertEqual(resp.json, { | ||
'message': 'Not a valid URL: siteUrl', | ||
'type': 'rest' | ||
}) | ||
|
||
resp = self.request( | ||
'/integration/dataverse', method='GET', | ||
params={'fileId': '1234', 'siteUrl': 'https://dataverse.someplace'}) | ||
self.assertStatus(resp, 303) | ||
self.assertEqual( | ||
resp.headers['Location'], | ||
'https://dashboard.wholetale.org/compose?uri=' | ||
'https%3A%2F%2Fdataverse.someplace%2Fapi%2Faccess%2Fdatafile%2F1234' | ||
) |
This file contains 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
This file contains 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,52 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
import os | ||
import cherrypy | ||
import validators | ||
from urllib.parse import urlparse, urlunparse, urlencode | ||
from girder.api import access | ||
from girder.api.describe import Description, autoDescribeRoute | ||
from girder.api.rest import Resource, RestException, setResponseHeader | ||
|
||
|
||
class Integration(Resource): | ||
|
||
def __init__(self): | ||
super(Integration, self).__init__() | ||
self.resourceName = 'integration' | ||
|
||
self.route('GET', ('dataverse',), self.dataverseExternalTools) | ||
|
||
@access.public | ||
@autoDescribeRoute( | ||
Description('Convert external tools request and bounce it to the dashboard.') | ||
.param('fileId', 'The Dataverse database ID of a file the external tool has ' | ||
'been launched on.', required=True) | ||
.param('siteUrl', 'The URL of the Dataverse installation that hosts the file ' | ||
'with the fileId above', required=True) | ||
.param('apiToken', 'The Dataverse API token of the user launching the external' | ||
' tool, if available.', required=False) | ||
.notes('apiToken is currently ignored.') | ||
) | ||
def dataverseExternalTools(self, fileId, siteUrl, apiToken): | ||
if not validators.url(siteUrl): | ||
raise RestException('Not a valid URL: siteUrl') | ||
try: | ||
fileId = int(fileId) | ||
except (TypeError, ValueError): | ||
raise RestException('Invalid fileId (should be integer)') | ||
|
||
site = urlparse(siteUrl) | ||
url = '{scheme}://{netloc}/api/access/datafile/{fileId}'.format( | ||
scheme=site.scheme, netloc=site.netloc, fileId=fileId | ||
) | ||
|
||
# TODO: Make base url a plugin setting, defaulting to dashboard.<domain> | ||
dashboard_url = os.environ.get('DASHBOARD_URL', 'https://dashboard.wholetale.org') | ||
location = urlunparse( | ||
urlparse(dashboard_url)._replace( | ||
path='/compose', | ||
query=urlencode({'uri': url})) | ||
) | ||
setResponseHeader('Location', location) | ||
cherrypy.response.status = 303 |