Skip to content

Commit

Permalink
Merge pull request #54 from AMP-SCZ/revert-52-revert-43-kcho/pronet
Browse files Browse the repository at this point in the history
Revert "Revert "UPENN Cognitive battery REDCap option (redcap + redcap OR rpms + redcap)""
  • Loading branch information
kcho authored Oct 13, 2021
2 parents 44d5e92 + 1077c91 commit d8148d4
Show file tree
Hide file tree
Showing 14 changed files with 652 additions and 213 deletions.
29 changes: 16 additions & 13 deletions lochness/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,26 @@ def asdict(self):
'metadata_csv': self.metadata_csv}


def initialize_metadata(Lochness, args, multiple_site_in_a_repo) -> None:
def initialize_metadata(Lochness, args,
multiple_site_in_a_repo, upenn_redcap) -> None:
'''Create (overwrite) metadata.csv using either REDCap or RPMS database'''
for study_name in args.studies:
# if 'redcap' or 'rpms' is in the sources, create (overwrite)
if 'redcap' in args.input_sources:
id_fieldname = 'chric_subject_id'
consent_fieldname = 'chric_consent_date'
REDCap.initialize_metadata(
Lochness, study_name, id_fieldname, consent_fieldname,
multiple_site_in_a_repo)

elif 'rpms' in args.input_sources:
# metadata.csv
if 'rpms' in args.input_sources:
# when rpms is included in the sources, initiate metadata using
# rpms
id_fieldname = Lochness['RPMS_id_colname']
consent_fieldname = Lochness['RPMS_consent_colname']
RPMS.initialize_metadata(
Lochness, study_name, id_fieldname, consent_fieldname,
multiple_site_in_a_repo)
multiple_site_in_a_repo, upenn_redcap)

elif 'redcap' in args.input_sources:
id_fieldname = 'chric_subject_id'
consent_fieldname = 'chric_consent_date'
REDCap.initialize_metadata(
Lochness, study_name, id_fieldname, consent_fieldname,
multiple_site_in_a_repo, upenn_redcap)

else:
pass
Expand Down Expand Up @@ -296,6 +298,7 @@ def _parse_beiwe(value, default_id=None):
def _parse_redcap(value, default_id=None):
'''helper function to parse a redcap metadata value'''
default = 'redcap.*:{ID}'.format(ID=default_id)

return _simple_parser(value, default=default)


Expand Down Expand Up @@ -376,9 +379,9 @@ def listdir(Lochness, d):
def attempt(f, Lochness, *args, **kwargs):
'''attempt a function call'''
if len(attempt.warnings) >= 5:
lochness.email.attempts_error(Lochness, attempt)
# lochness.email.attempts_error(Lochness, attempt)
attempt.warnings = []
#raise AttemptsError('too many attempt warnings')
raise AttemptsError('too many attempt warnings')
try:
f(Lochness, *args, **kwargs)
except Exception as e:
Expand Down
45 changes: 44 additions & 1 deletion lochness/box/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
import lochness.net as net
import lochness.tree as tree
from os.path import join, basename
logging.getLogger('boxsdk').setLevel(logging.CRITICAL)
from boxsdk import Client, OAuth2
from boxsdk.exception import BoxOAuthException
import cryptease as enc
import re
import requests


logger = logging.getLogger(__name__)
Expand All @@ -26,6 +29,7 @@

CHUNK_SIZE = 65536


def delete_on_success(Lochness, module_name):
''' get module-specific delete_on_success flag with a safe default '''
value = Lochness.get('box', dict()) \
Expand All @@ -44,6 +48,35 @@ def base(Lochness, module_name):
.get('base', '')


def get_access_token(client_id: str, client_secret: str, user_id: str) -> str:
'''Get new access token using Box API
Key Argument:
client_id: Client ID from the box app from box dev console, str
client_secret: Client secret from the box app from box dev console, str
user_id: user id from the box app from box dev console, str of digits.
Returns:
api_token: API TOKEN used to pull data, str.
Notes:
Current version of get_access_token function uses User ID rather
than Enterprise ID. (Both works in pulling data from BOX account)
'''
url = "https://api.box.com/oauth2/token"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
data = {"client_id": client_id,
"client_secret": client_secret,
"grant_type": "client_credentials",
"box_subject_type": "user",
"box_subject_id": user_id}

response = requests.post(url, headers=headers, data=data)
api_token = response.json()['access_token']

return api_token


def get_box_object_based_on_name(client: boxsdk.client,
box_folder_name: str,
box_path_id: str = '0') \
Expand Down Expand Up @@ -301,9 +334,11 @@ def sync_module(Lochness: 'lochness.config',
_passphrase = keyring.passphrase(Lochness, subject.study)
enc_key = enc.kdf(_passphrase)

client_id, client_secret, api_token = keyring.box_api_token(
client_id, client_secret, user_id = keyring.box_api_token(
Lochness, module_name)

api_token = get_access_token(client_id, client_secret, user_id)

# box authentication
auth = OAuth2(
client_id=client_id,
Expand All @@ -312,6 +347,14 @@ def sync_module(Lochness: 'lochness.config',
)
client = Client(auth)

# check if the login details are correct
try:
_ = client.user().get()
except BoxOAuthException:
logger.debug('Failed to login to BOX. Please check Box keyrings.')
return


bx_base = base(Lochness, module_basename)

# get the id of the bx_base path in box
Expand Down
6 changes: 3 additions & 3 deletions lochness/keyring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def box_api_token(Lochness, key):
'''get box api token from keyring'''
if key not in Lochness['keyring']:
raise KeyringError('\'{0}\' not in keyring'.format(key))
if 'API_TOKEN' not in Lochness['keyring'][key]:
raise KeyringError('\'API_TOKEN\' not in {0}'.format(key))
if 'USER_ID' not in Lochness['keyring'][key]:
raise KeyringError('\'USER_ID\' not in {0}'.format(key))
return (Lochness['keyring'][key]['CLIENT_ID'],
Lochness['keyring'][key]['CLIENT_SECRET'],
Lochness['keyring'][key]['API_TOKEN'])
Lochness['keyring'][key]['USER_ID'])


def rsync_token(Lochness, key):
Expand Down
Loading

0 comments on commit d8148d4

Please sign in to comment.