Skip to content

Commit 46f1b2d

Browse files
committed
Make Authenticated Requests if Envvars are Set
1 parent 2fbecd0 commit 46f1b2d

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

settings/.env.dev

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ POSTGRES_DB=gene_normalizer
1919

2020
UTA_DB_URL=postgresql://anonymous:[email protected]:5432/uta/uta_20180821
2121

22+
####################################################################################################
23+
# Environment variables for MaveDB connection
24+
####################################################################################################
25+
26+
MAVEDB_BASE_URL=http://localhost:8000
27+
MAVEDB_API_KEY=
28+
2229
####################################################################################################
2330
# Environment variables for seqrepo
2431
####################################################################################################

src/dcd_mapping/mavedb_data.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
from dcd_mapping.resource_utils import (
1717
LOCAL_STORE_PATH,
18+
MAVEDB_BASE_URL,
1819
ResourceAcquisitionError,
20+
authentication_header,
1921
http_download,
2022
)
2123
from dcd_mapping.schemas import ScoreRow, ScoresetMetadata, UniProtRef
@@ -42,7 +44,11 @@ def get_scoreset_urns() -> set[str]:
4244
4345
:return: set of URN strings
4446
"""
45-
r = requests.get("https://api.mavedb.org/api/v1/experiments/", timeout=30)
47+
r = requests.get(
48+
f"{MAVEDB_BASE_URL}/api/v1/experiments/",
49+
timeout=30,
50+
headers=authentication_header(),
51+
)
4652
r.raise_for_status()
4753
scoreset_urn_lists = [
4854
experiment["scoreSetUrns"]
@@ -78,7 +84,11 @@ def get_human_urns() -> list[str]:
7884
scoreset_urns = get_scoreset_urns()
7985
human_scoresets: list[str] = []
8086
for urn in scoreset_urns:
81-
r = requests.get(f"https://api.mavedb.org/api/v1/score-sets/{urn}", timeout=30)
87+
r = requests.get(
88+
f"{MAVEDB_BASE_URL}/api/v1/score-sets/{urn}",
89+
timeout=30,
90+
headers=authentication_header(),
91+
)
8292
try:
8393
r.raise_for_status()
8494
except requests.exceptions.HTTPError:
@@ -127,8 +137,8 @@ def get_raw_scoreset_metadata(
127137
dcd_mapping_dir = LOCAL_STORE_PATH
128138
metadata_file = dcd_mapping_dir / f"{scoreset_urn}_metadata.json"
129139
if not metadata_file.exists():
130-
url = f"https://api.mavedb.org/api/v1/score-sets/{scoreset_urn}"
131-
r = requests.get(url, timeout=30)
140+
url = f"{MAVEDB_BASE_URL}/api/v1/score-sets/{scoreset_urn}"
141+
r = requests.get(url, timeout=30, headers=authentication_header())
132142
try:
133143
r.raise_for_status()
134144
except requests.HTTPError as e:
@@ -246,7 +256,7 @@ def get_scoreset_records(
246256
if urn == "urn:mavedb:00000053-a-1":
247257
_get_experiment_53_scores(scores_csv, silent)
248258
else:
249-
url = f"https://api.mavedb.org/api/v1/score-sets/{urn}/scores"
259+
url = f"{MAVEDB_BASE_URL}/api/v1/score-sets/{urn}/scores"
250260
try:
251261
http_download(url, scores_csv, silent)
252262
except requests.HTTPError as e:

src/dcd_mapping/resource_utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import requests
77
from tqdm import tqdm
88

9+
MAVEDB_API_KEY = os.environ.get("MAVEDB_API_KEY")
10+
MAVEDB_BASE_URL = os.environ.get("MAVEDB_BASE_URL")
11+
912
LOCAL_STORE_PATH = Path(
1013
os.environ.get(
1114
"DCD_MAPPING_RESOURCES_DIR", Path.home() / ".local" / "share" / "dcd_mapping"
@@ -19,6 +22,11 @@ class ResourceAcquisitionError(Exception):
1922
"""Raise when resource acquisition fails."""
2023

2124

25+
def authentication_header() -> dict | None:
26+
"""Fetch with api key envvar, if available."""
27+
return {"X-API-key": MAVEDB_API_KEY} if MAVEDB_API_KEY is not None else None
28+
29+
2230
def http_download(url: str, out_path: Path, silent: bool = True) -> Path:
2331
"""Download a file via HTTP.
2432
@@ -30,7 +38,9 @@ def http_download(url: str, out_path: Path, silent: bool = True) -> Path:
3038
"""
3139
if not silent:
3240
click.echo(f"Downloading {out_path.name} to {out_path.parents[0].absolute()}")
33-
with requests.get(url, stream=True, timeout=30) as r:
41+
with requests.get(
42+
url, stream=True, timeout=30, headers=authentication_header()
43+
) as r:
3444
r.raise_for_status()
3545
total_size = int(r.headers.get("content-length", 0))
3646
with out_path.open("wb") as h:

0 commit comments

Comments
 (0)