-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconnect_to_synapse.py
103 lines (74 loc) · 2.97 KB
/
connect_to_synapse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# ------------------------- Synapse endpoints ----------------------------------
# Synapse REST API docs:
# https://rest-docs.synapse.org/rest/index.html
# Note: we're using the Python client because the R synapse client is incompatible
# with reticulate and it uses PythonEmbedInR, which has issues on shinyapps.io
import synapseclient
import requests
import json
syn = synapseclient.Synapse()
def login_to_synapse(username, api_key):
syn.login(email=username, apiKey=api_key, rememberMe=True)
def get_synapse_userinfo(access_token):
endpoint = "https://repo-prod.prod.sagebase.org/auth/v1/oauth2/userinfo"
headers = {"Authorization": "Bearer " + access_token}
response = requests.get(endpoint, headers=headers).json()
return(response)
def get_synapse_user_profile():
response = syn.getUserProfile()
return(response)
def get_synapse_teams(user_id):
endpoint = "https://repo-prod.prod.sagebase.org/repo/v1/user/" + user_id + "/team"
response = requests.get(endpoint).json()
return(response)
def get_synapse_projects(access_token):
endpoint = "https://repo-prod.prod.sagebase.org/repo/v1/projects/"
headers = {"Authorization": "Bearer " + access_token}
response = requests.get(endpoint).json()
return(response)
def fetch_synapse_filepath(entity_id):
entity = syn.get(entity_id, downloadLocation='data', ifcollision='overwrite.local')
return(entity.path)
def create_prod_client():
client_meta_data = {
'client_name': '<YOUR NAME HERE>',
'redirect_uris': [
'<YOUR URI HERE>'
],
'client_uri': '<YOUR URI HERE>'
}
# Create the client:
client_meta_data = syn.restPOST(uri='/oauth2/client',
endpoint=syn.authEndpoint, body=json.dumps(client_meta_data))
client_id = client_meta_data['client_id']
# Generate and retrieve the client secret:
client_id_and_secret = syn.restPOST(uri='/oauth2/client/secret/'+client_id,
endpoint=syn.authEndpoint, body='')
return(client_id_and_secret)
def create_local_client():
client_meta_data = {
'client_name': '<YOUR LOCAL NAME HERE>',
'redirect_uris': [
'http://127.0.0.1:7450'
]
}
# Create the client:
client_meta_data = syn.restPOST(uri='/oauth2/client',
endpoint=syn.authEndpoint, body=json.dumps(client_meta_data))
client_id = client_meta_data['client_id']
# Generate and retrieve the client secret:
client_id_and_secret = syn.restPOST(uri='/oauth2/client/secret/'+client_id,
endpoint=syn.authEndpoint, body='')
return(client_id_and_secret)
"""
# To change client meta-data
# Retrieve a client using its ID:
client_meta_data = syn.restGET(uri='/oauth2/client/'+client_id,
endpoint=syn.authEndpoint)
# Alter the desired field
client_meta_data['client_name'] = 'Predictive BioAnalytics at Wyss Institute'
client_meta_data.pop('userinfo_signed_response_alg')
# Update a client's metadata:
client_meta_data = syn.restPUT(uri='/oauth2/client/'+client_id,
endpoint=syn.authEndpoint, body=json.dumps(client_meta_data))
"""