-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: ♻️ commonise REDCap API and JSON logic #154
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
Open
martonvago
wants to merge
2
commits into
main
Choose a base branch
from
refactor/commonise-code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """REDCap common functions.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import os | ||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
| from typing import Any | ||
|
|
||
| import requests | ||
| from dotenv import load_dotenv | ||
|
|
||
| load_dotenv() | ||
|
|
||
|
|
||
| @dataclass | ||
| class APIConfig: | ||
| """Configuration for the REDCap API.""" | ||
|
|
||
| env_key: str | ||
| url: str | ||
|
|
||
|
|
||
| class Center(Enum): | ||
| """The centers in the study.""" | ||
|
|
||
| # TODO: Update these once we can connect them. | ||
| Aarhus = APIConfig(env_key="REDC_AAR_API_KEY", url="https://redcap.auh.dk/api/") | ||
| Copenhagen = APIConfig( | ||
| env_key="REDC_CPH_API_KEY", url="https://redcap.regionh.dk/api/" | ||
| ) | ||
| Odense = APIConfig(env_key="REDC_ODN_API_KEY", url="https://redcap.sdu.dk/api/") | ||
| Test = APIConfig(env_key="TEST_API_KEY", url="https://redcap.au.dk/api/") | ||
|
|
||
|
|
||
| def get_from_redcap( | ||
| data: dict[str, str], | ||
| center: Center = Center.Copenhagen, | ||
| ) -> requests.Response: | ||
| """Send a request to the REDCap API.""" | ||
| token = os.environ.get(center.value.env_key) | ||
| if not token: | ||
| raise RuntimeError(f"{center.value.env_key} environment variable is not set.") | ||
|
|
||
| data["token"] = token | ||
|
|
||
| response = requests.post(center.value.url, data=data, timeout=60) | ||
| response.raise_for_status() | ||
|
|
||
| return response | ||
|
|
||
|
|
||
| def get_json_from_redcap( | ||
| content: str, | ||
| center: Center = Center.Copenhagen, | ||
| ) -> Any: | ||
| """Send a request to the REDCap API and return the JSON response.""" | ||
| data = { | ||
| "content": content, | ||
| "format": "json", | ||
| "returnFormat": "json", | ||
| } | ||
| response = get_from_redcap(data, center) | ||
| return response.json() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import json | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
|
|
||
| def read_json(path: Path) -> Any: | ||
| """Read a JSON file and return the data.""" | ||
| with open(path) as f: | ||
| return json.load(f) | ||
|
|
||
|
|
||
| def write_json(path: Path, data: Any) -> None: | ||
| """Write data to a JSON file.""" | ||
| path.parent.mkdir(parents=True, exist_ok=True) | ||
| with open(path, "w") as f: | ||
| json.dump(data, f, indent=2, ensure_ascii=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """REDCap data functions.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,37 @@ | ||
| import os | ||
| from datetime import datetime | ||
| from io import StringIO | ||
| from pathlib import Path | ||
|
|
||
| import polars as pl | ||
| import requests | ||
| import seedcase_soil as so | ||
| from dotenv import load_dotenv | ||
|
|
||
| load_dotenv() | ||
| from feasibility_data.common.redcap.api import Center, get_from_redcap | ||
|
|
||
|
|
||
| def write_raw() -> Path: | ||
| """Writes the raw data to `raw/redcap/<timestamp>.csv.gz`.""" | ||
| data = _request_raw() | ||
| def download_redcap_data( | ||
| raw_data_dir: Path, | ||
| center: Center, | ||
| ) -> None: | ||
| """Download the data.""" | ||
| response = get_from_redcap( | ||
| data={ | ||
| "content": "record", | ||
| "action": "export", | ||
| "format": "csv", | ||
| "type": "flat", | ||
| "csvDelimiter": ";", | ||
| "rawOrLabel": "raw", | ||
| "rawOrLabelHeaders": "raw", | ||
| "exportCheckboxLabel": "false", | ||
| "exportSurveyFields": "false", | ||
| "exportDataAccessGroups": "false", | ||
| "returnFormat": "json", | ||
| }, | ||
| center=center, | ||
| ) | ||
| data = response.text | ||
|
|
||
| df = pl.read_csv(StringIO(data), separator=";", infer_schema=False) | ||
| timestamp = datetime.now().strftime("%Y-%m-%dT%H-%M-%S") | ||
| file_path = Path("raw") / "redcap" / f"{timestamp}.csv.gz" | ||
| file_path.parent.mkdir(parents=True, exist_ok=True) | ||
| df.write_csv(file_path, compression="gzip") | ||
| so.pretty_print(f"Saved data to '{file_path}'.") | ||
| return file_path | ||
|
|
||
|
|
||
| def _request_raw() -> str: | ||
| """Gets the data from REDCap as CSV.""" | ||
| token = os.environ.get("REDC_CPH_API_KEY") | ||
| if not token: | ||
| raise RuntimeError("REDC_CPH_API_KEY environment variable is not set.") | ||
|
|
||
| data = { | ||
| "token": token, | ||
| "content": "record", | ||
| "action": "export", | ||
| "format": "csv", | ||
| "type": "flat", | ||
| "csvDelimiter": ";", | ||
| "rawOrLabel": "raw", | ||
| "rawOrLabelHeaders": "raw", | ||
| "exportCheckboxLabel": "false", | ||
| "exportSurveyFields": "false", | ||
| "exportDataAccessGroups": "false", | ||
| "returnFormat": "json", | ||
| } | ||
| response = requests.post("https://redcap.regionh.dk/api/", data=data, timeout=60) | ||
| response.raise_for_status() | ||
| return response.text | ||
| data_path = raw_data_dir / f"{timestamp}.csv.gz" | ||
| raw_data_dir.parent.mkdir(parents=True, exist_ok=True) | ||
| df.write_csv(data_path, compression="gzip") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """REDCap metadata functions.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,67 +1,10 @@ | ||
| import json | ||
| import os | ||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
| from pathlib import Path | ||
|
|
||
| import requests | ||
| from dotenv import load_dotenv | ||
| from feasibility_data.common.redcap.api import get_json_from_redcap | ||
| from feasibility_data.common.redcap.json import write_json | ||
|
|
||
| load_dotenv() | ||
|
|
||
|
|
||
| @dataclass | ||
| class APIConfig: | ||
| """Configuration for the REDCap API.""" | ||
|
|
||
| env_key: str | ||
| url: str | ||
|
|
||
|
|
||
| class Center(Enum): | ||
| """The centers in the study.""" | ||
|
|
||
| # TODO: Update these once we can connect them. | ||
| Aarhus = APIConfig(env_key="REDC_AAR_API_KEY", url="https://redcap.auh.dk/api/") | ||
| Copenhagen = APIConfig( | ||
| env_key="REDC_CPH_API_KEY", url="https://redcap.regionh.dk/api/" | ||
| ) | ||
| Odense = APIConfig(env_key="REDC_ODN_API_KEY", url="https://redcap.sdu.dk/api/") | ||
|
|
||
|
|
||
| # TODO: Update path to point to pytask build folder once we have one. | ||
| def write_dictionary( | ||
| raw_dictionary: list[dict[str, str]], | ||
| path: Path = Path("src/feasibility_data/metadata/redcap/dictionary.json"), | ||
| ) -> Path: | ||
| """Write dictionary from REDCap. | ||
|
|
||
| Args: | ||
| raw_dictionary: The raw dictionary data from REDCap. Gotten from `request_dictionary()`. | ||
| path: The path to write the dictionary to. | ||
|
|
||
| Returns: | ||
| The path to the new dictionary file. | ||
| """ | ||
| path.parent.mkdir(parents=True, exist_ok=True) | ||
| with open(path, "w") as f: | ||
| json.dump(raw_dictionary, f, indent=2, ensure_ascii=False) | ||
| return path | ||
|
|
||
|
|
||
| def request_raw_dictionary(center: Center = Center.Copenhagen) -> list[dict[str, str]]: | ||
| """Gets the data dictionary from REDCap.""" | ||
| token = os.environ.get(center.value.env_key) | ||
| if not token: | ||
| raise RuntimeError(f"{center.value.env_key} environment variable is not set.") | ||
|
|
||
| data = { | ||
| "token": token, | ||
| "content": "metadata", | ||
| "format": "json", | ||
| "returnFormat": "json", | ||
| } | ||
| response = requests.post(center.value.url, data=data, timeout=30) | ||
| response.raise_for_status() | ||
| dictionary: list[dict[str, str]] = response.json() | ||
| return dictionary | ||
| def download_redcap_metadata(path: Path, content: str) -> None: | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling this metadata because it will be used to download different types of metadata (event info, field metadata, repeating instruments) |
||
| """Download data from REDCap and save it to a JSON file.""" | ||
| data = get_json_from_redcap(content) | ||
| write_json(path, data) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this live here?