Skip to content

Commit

Permalink
Add Dataverse External Tools integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Xarthisius committed Nov 28, 2018
1 parent 577835a commit c706ac7
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ add_python_test(dataverse
plugins/wholetale/dataverse_lookup.txt
plugins/wholetale/dataverse_listFiles.json
)
add_python_test(integration PLUGIN wholetale)
add_python_style_test(python_static_analysis_wholetale
"${PROJECT_SOURCE_DIR}/plugins/wholetale/server")
44 changes: 44 additions & 0 deletions plugin_tests/integration_test.py
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'
)
2 changes: 2 additions & 0 deletions server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .rest.dataset import Dataset
from .rest.recipe import Recipe
from .rest.image import Image
from .rest.integration import Integration
from .rest.repository import Repository
from .rest.publish import Publish
from .rest.harvester import listImportedData
Expand Down Expand Up @@ -374,6 +375,7 @@ def load(info):
events.bind('model.user.save.created', 'wholetale', addDefaultFolders)
info['apiRoot'].repository = Repository()
info['apiRoot'].publish = Publish()
info['apiRoot'].integration = Integration()
info['apiRoot'].folder.route('GET', ('registered',), listImportedData)
info['apiRoot'].folder.route('GET', (':id', 'listing'), listFolder)
info['apiRoot'].folder.route('GET', (':id', 'dataset'), getDataSet)
Expand Down
52 changes: 52 additions & 0 deletions server/rest/integration.py
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

0 comments on commit c706ac7

Please sign in to comment.