handler_ada.py 1.94 KB
Newer Older
1
2
"""This module provides AdaHandler, an implementation of ETLHandler for ADA."""

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

Sascha Herzinger's avatar
Sascha Herzinger committed
5
6
import requests

7
8
9
from fractalis.data.etlhandler import ETLHandler


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


13
class AdaHandler(ETLHandler):
14
    """This ETLHandler provides integration with ADA.
15

Sascha Herzinger's avatar
Sascha Herzinger committed
16
    'Ada provides key infrastructure for secured integration, visualization,
17
18
19
20
21
    and analysis of anonymized clinical and experimental data stored in CSV
    and tranSMART format, or provided by RedCAP and Synapse apps.'

    Project URL: https://git-r3lab.uni.lu/peter.banda/ncer-pd
    """
22

Sascha Herzinger's avatar
Sascha Herzinger committed
23
    _handler = 'ada'
24
25

    def _heartbeat(self):
26
27
28
        raise NotImplementedError()

    @staticmethod
29
    def make_label(descriptor: dict) -> str:
30
31
        return '{} ({})'.format(descriptor['dictionary']['label'],
                                descriptor['data_set'])
32

Sascha Herzinger's avatar
Sascha Herzinger committed
33
34
35
36
37
38
    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
39
40
41
42
        except KeyError as e:
            logger.exception(e)
            raise ValueError("The authentication object must contain the "
                             "non-empty fields 'user' and 'passwd'.")
Sascha Herzinger's avatar
Sascha Herzinger committed
43
44
        r = requests.post(url='{}/login'.format(server),
                          headers={'Accept': 'application/json'},
45
46
                          data={'id': user, 'password': passwd},
                          timeout=10)
47
        if r.status_code != 200:
48
49
50
51
            error = "Could not authenticate. " \
                    "Reason: [{}]: {}".format(r.status_code, r.text)
            logger.error(error)
            raise ValueError(error)
Sascha Herzinger's avatar
Sascha Herzinger committed
52
53
54
        cookie = r.headers['Set-Cookie']
        token = [s for s in cookie.split(';')
                 if s.startswith('PLAY2AUTH_SESS_ID')][0]
Sascha Herzinger's avatar
Sascha Herzinger committed
55
        token = '='.join(token.split('=')[1:])  # remove PLAY2AUTH_SESS_ID=
Sascha Herzinger's avatar
Sascha Herzinger committed
56
        return token