Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support putting the API key in the params and header #12

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading