Skip to content

Make api_key_env_var optional in LoadFromAPI #1799

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 13 additions & 7 deletions src/unitxt/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ class LoadFromAPI(Loader):
chunksize: int = 100000
loader_limit: Optional[int] = None
streaming: bool = False
api_key_env_var: str = "SQL_API_KEY"
api_key_env_var: Optional[str] = ""
headers: Optional[Dict[str, Any]] = None
data_field: str = "data"
method: str = "GET"
Expand All @@ -1049,17 +1049,23 @@ def _maybe_set_classification_policy(self):
self.set_default_data_classification(["proprietary"], "when loading from API")

def load_iterables(self) -> Dict[str, Iterable]:
api_key = os.getenv(self.api_key_env_var, None)
if not api_key:
raise ValueError(
f"The environment variable '{self.api_key_env_var}' must be set to use the LoadFromAPI loader."
)
if self.api_key_env_var is not None:
api_key = os.getenv(self.api_key_env_var, None)
if not api_key:
raise ValueError(
f"The environment variable '{self.api_key_env_var}' must be set to use the LoadFromAPI loader."
)
else:
api_key = None

base_headers = {
"Content-Type": "application/json",
"accept": "application/json",
"Authorization": f"Bearer {api_key}",
}

if api_key is not None:
base_headers["Authorization"] = f"Bearer {api_key}"

if self.headers:
base_headers.update(self.headers)

Expand Down
Loading