handler_transmart.py 2.28 KB
Newer Older
1
2
3
"""This module provides TransmartHandler, an implementation of ETLHandler for
tranSMART."""

Sascha Herzinger's avatar
Sascha Herzinger committed
4
5
import logging

6
7
8
9
10
import requests

from fractalis.data.etlhandler import ETLHandler


Sascha Herzinger's avatar
Sascha Herzinger committed
11
12
13
logger = logging.getLogger(__name__)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class TransmartHandler(ETLHandler):
    """This ETLHandler provides integration with tranSMART.

    'tranSMART is a knowledge management platform that enables scientists to
    develop and refine research hypotheses by investigating correlations between
    genetic and phenotypic data, and assessing their analytical results in the
    context of published literature and other work.'

    Project URL: https://github.com/transmart
    """

    _handler = 'transmart'

    def _heartbeat(self):
        raise NotImplementedError()

    @staticmethod
    def make_label(descriptor: dict) -> str:
32
        return descriptor['path']
33

Sascha Herzinger's avatar
Sascha Herzinger committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    def _get_token_for_credentials(self, server: str, auth: dict) -> str:
        try:
            user = auth['user']
            passwd = auth['passwd']
            if len(user) == 0 or len(passwd) == 0:
                raise KeyError
        except KeyError:
            error = "The authentication object must contain the non-empty " \
                    "fields 'user' and 'passwd'."
            logger.error(error)
            raise ValueError(error)
        r = requests.post(url=server + '/oauth/token',
                          params={
                              'grant_type': 'password',
                              'client_id': 'glowingbear-js',
                              'client_secret': '',
                              'username': user,
                              'password': passwd
                          },
53
                          headers={'Accept': 'application/json'})
Sascha Herzinger's avatar
Sascha Herzinger committed
54
        auth_error = ''
55
        if r.status_code != 200:
Sascha Herzinger's avatar
Sascha Herzinger committed
56
57
58
59
60
61
62
63
64
65
66
67
            auth_error = "Could not authenticate. " \
                         "Reason: [{}]: {}".format(r.status_code, r.text)
            logger.error(auth_error)
            raise ValueError(auth_error)
        try:
            response = r.json()
            return response['access_token']
        except ValueError:
            auth_error = "Could not authenticate. " \
                         "Got unexpected response: '{}'".format(r.text)
            logger.error(auth_error)
            raise ValueError(auth_error)