-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-utils.js
68 lines (54 loc) · 1.95 KB
/
google-utils.js
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
const RNA_VIEWER_CLIENT_ID = '61200892608-qphtf65o323setqdcfj4hnf601mmetvs.apps.googleusercontent.com'
const initGoogleClient = () => new Promise(resolve => {
// init all gapi modules
gapi.load('client:auth2', () => {
gapi.client.load('storage', 'v1', () => {
gapi.client.init({
'clientId': RNA_VIEWER_CLIENT_ID,
'scope': 'https://www.googleapis.com/auth/devstorage.read_only',
'discoveryDocs': ['https://www.googleapis.com/discovery/v1/apis/storage/v1/rest']
})
resolve()
})
})
})
const googleSignIn = async () => {
const authInstance = await gapi.auth2.getAuthInstance()
if(!authInstance.isSignedIn.get()) {
await authInstance.signIn()
}
}
const getGoogleUserEmail = async () => {
const authInstance = await gapi.auth2.getAuthInstance()
const profile = authInstance.currentUser.get().getBasicProfile()
return profile.getEmail()
}
const getGoogleAccessToken = async () => {
// use OAuth2 to get an access token for RNA-seq viewer to access the google storage API on behalf of the user
const authInstance = await gapi.auth2.getAuthInstance()
const user = authInstance.currentUser.get()
if (!authInstance.isSignedIn.get()) {
user.reloadAuthResponse()
}
return user.getAuthResponse().access_token
}
const googleSignOut = async () => {
const authInstance = await gapi.auth2.getAuthInstance()
if(authInstance.isSignedIn.get()) {
await authInstance.signOut()
}
}
const listGoogleStorageFiles = async (gsPath) => {
if (!gsPath.startsWith("gs://")) {
console.error(`${gsPath} doesn't start with "gs://"` )
return
}
const gsPathParts = gsPath.split("/")
if (gsPathParts.length < 3) {
console.error(`${gsPath} must be of the form "gs://bucket-name/..."` )
return
}
const bucketName = gsPathParts[2]
const bucketSubdir = gsPathParts.slice(3).join('/')
return await gapi.client.storage.objects.list({ bucket: bucketName, prefix: bucketSubdir, maxResults:1000})
}