Skip to content

Commit

Permalink
feat: support putting the API key in the header (#12)
Browse files Browse the repository at this point in the history
This is in addition to the API key in the query params.
  • Loading branch information
paulswartz authored Feb 20, 2024
1 parent 80d3f2a commit c2bbb4d
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/dmap_import/util_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ class ApiResponse(TypedDict):
results: List[ApiResult]


def apikey_from_environment(url: str) -> Optional[str]:
"""
Get the `apikey` value from the environment
"""
default = None
if "datasetpublicusersapi" in url:
return os.getenv("PUBLIC_KEY", default)
if "datasetcontrolleduserapi" in url:
return os.getenv("CONTROLLED_KEY", default)
return default


def download_from_url(url: str, local_path: str) -> Optional[str]:
"""
Download file from url to local_path.
Expand Down Expand Up @@ -122,6 +134,7 @@ def get_api_results(url: str, db_manager: DatabaseManager) -> List[ApiResult]:
# add params to GET request
# last_updated if last_update_dt available from ApiMetadata table
# apikey based on contents of url endpoint string
headers = {}
params = {}
if db_result:
last_updated_dt: datetime.datetime = db_result[0]["last_updated"]
Expand All @@ -132,10 +145,11 @@ def get_api_results(url: str, db_manager: DatabaseManager) -> List[ApiResult]:
last_updated_dt=last_updated_dt.isoformat()
)

if "datasetpublicusersapi" in url:
params["apikey"] = os.getenv("PUBLIC_KEY", "")
elif "datasetcontrolleduserapi" in url:
params["apikey"] = os.getenv("CONTROLLED_KEY", "")
apikey = apikey_from_environment(url)

if apikey:
headers["apikey"] = apikey
params["apikey"] = apikey

# execute GET request from CUBIC API Endpoint
# will log and throw if 200 status_code not recieved
Expand All @@ -144,7 +158,9 @@ def get_api_results(url: str, db_manager: DatabaseManager) -> List[ApiResult]:
for retry_count in range(max_retries + 1):
api_results_log.add_metadata(retry_count=retry_count)
try:
response = requests.get(url, params=params, timeout=15)
response = requests.get(
url, headers=headers, params=params, timeout=15
)
response.raise_for_status()
response.close()

Expand Down

0 comments on commit c2bbb4d

Please sign in to comment.