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: introduce Customizable Base URLs for API Providers #524

Merged
merged 9 commits into from
Feb 15, 2025
9 changes: 8 additions & 1 deletion basilisk/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
"""Configuration module for Basilisk."""

from .account_config import Account, AccountManager, AccountOrganization
from .account_config import (
CUSTOM_BASE_URL_PATTERN,
Account,
AccountManager,
AccountOrganization,
)
from .account_config import get_account_config as accounts
from .config_enums import (
AccountSource,
Expand All @@ -27,6 +32,8 @@
"conf",
"ConversationProfile",
"conversation_profiles",
"CUSTOM_BASE_URL_PATTERN",
"get_account_source_labels",
"KeyStorageMethodEnum",
"LogLevelEnum",
"ReleaseChannelEnum",
Expand Down
12 changes: 12 additions & 0 deletions basilisk/config/account_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import logging
import re
from functools import cache, cached_property
from os import getenv
from typing import Annotated, Any, Iterable, Optional, Union
Expand Down Expand Up @@ -38,6 +39,10 @@

log = logging.getLogger(__name__)

CUSTOM_BASE_URL_PATTERN = re.compile(
r"^https?://[\w.-]+(?::\d{1,5})?(?:/[\w-]+)*/?$"
)

Comment on lines +42 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Definition of CUSTOM_BASE_URL_PATTERN.

The regex appears sufficiently flexible for basic HTTP/HTTPS URL validation, including optional ports and simple path segments.

Consider clarifying whether you need to support IPv6 addresses, user info, or other URL components. If not, this pattern is acceptable.


class AccountOrganization(BaseModel):
"""Manage organization key for an account."""
Expand Down Expand Up @@ -131,6 +136,11 @@ class Account(BaseModel):
organizations: Optional[list[AccountOrganization]] = Field(default=None)
active_organization_id: Optional[UUID4] = Field(default=None)
source: AccountSource = Field(default=AccountSource.CONFIG, exclude=True)
custom_base_url: Optional[str] = Field(
default=None,
pattern=CUSTOM_BASE_URL_PATTERN,
description="Custom base URL for the API provider. Must be a valid HTTP/HTTPS URL.",
)

def __init__(self, **data: Any):
"""Initialize an account instance. If an error occurs, log the error and raise an exception."""
Expand Down Expand Up @@ -186,6 +196,8 @@ def validate_api_key(
if not value:
raise ValueError("API key not found in keyring")
return SecretStr(value)
elif not data["provider"].require_api_key and value is None:
return None
else:
raise ValueError("Invalid API key storage method")

Expand Down
Loading
Loading