Skip to content

Instantly share code, notes, and snippets.

@xbrianh
Last active May 20, 2020 22:27
Show Gist options
  • Save xbrianh/c24fdbd8c5d24d570da3a965b3bfa782 to your computer and use it in GitHub Desktop.
Save xbrianh/c24fdbd8c5d24d570da3a965b3bfa782 to your computer and use it in GitHub Desktop.
Resolve DRS urls with the Broad's "martha" service
#!/usr/bin/env python
"""
Resolve DRS urls with the Broad's "martha" service:
https://github.com/broadinstitute/martha
Before using, authenticate with `gcloud auth application-default login`
"""
import os
import sys
import json
import requests
import google.auth.transport.requests
stage = os.environ.get('TERRA_DEPLOYMENT_STAGE', "prod")
def get_access_token():
creds, projects = google.auth.default()
creds.refresh(google.auth.transport.requests.Request())
return creds.token
def fetch_drs_info(drs_url):
access_token = get_access_token()
martha_url = f"https://us-central1-broad-dsde-{stage}.cloudfunctions.net/martha_v2"
headers = {
'authorization': f"Bearer {access_token}",
'content-type': "application/json"
}
resp = requests.post(martha_url, headers=headers, data=json.dumps(dict(url=drs_url)))
if 200 == resp.status_code:
return resp.json()
else:
print(resp.content)
raise Exception(f"expected status 200, got {resp.status_code}")
if __name__ == "__main__":
drs_info = fetch_drs_info(sys.argv[1])
print(drs_info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment