Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sdk-reforge"
version = "1.0.0"
version = "1.0.1"
description = "Python sdk for Reforge Feature Flags and Config as a Service: https://www.reforge.com"
license = "MIT"
authors = ["Michael Berkowitz <michael.berkowitz@gmail.com>", "James Kebinger <james.kebinger@reforge.com>"]
Expand Down
2 changes: 1 addition & 1 deletion sdk_reforge/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0
1.0.1
23 changes: 21 additions & 2 deletions sdk_reforge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"""

from typing import Optional
import os

from . import _internal_logging
from .options import Options as Options
from .sdk import ReforgeSDK as ReforgeSDK
from importlib.metadata import version
from .read_write_lock import ReadWriteLock as _ReadWriteLock
from .context import Context, NamedContext
from .feature_flag_sdk import FeatureFlagSDK
Expand Down Expand Up @@ -52,6 +52,25 @@
log = _internal_logging.InternalLogger(__name__)


__version_cache: Optional[str] = None


def _get_version() -> str:
"""Get the SDK version from the VERSION file (cached after first read)"""
global __version_cache
if __version_cache is not None:
return __version_cache

try:
version_file = os.path.join(os.path.dirname(__file__), "VERSION")
with open(version_file, "r") as f:
__version_cache = f.read().strip()
return __version_cache
except Exception:
__version_cache = "unknown"
return __version_cache


__base_sdk: Optional[ReforgeSDK] = None
__options: Optional[Options] = None
__lock = _ReadWriteLock()
Expand All @@ -75,7 +94,7 @@ def get_sdk() -> ReforgeSDK:
if not __options:
raise Exception("Options has not been set")
if not __base_sdk:
log.info(f"Initializing Reforge SDK version {version('reforge-python')}")
log.info(f"Initializing Reforge SDK version {_get_version()}")
__base_sdk = ReforgeSDK(__options)
return __base_sdk

Expand Down
16 changes: 8 additions & 8 deletions sdk_reforge/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
from .constants import ContextDictType


class MissingApiKeyException(Exception):
class MissingSdkKeyException(Exception):
"""
Raised when no API key is found
Raised when no SDK key is found
"""

def __init__(self) -> None:
super().__init__("No API key found")
super().__init__("No SDK key found - set REFORGE_BACKEND_SDK_KEY")


class InvalidApiKeyException(Exception):
class InvalidSdkKeyException(Exception):
"""
Raised when an invalid API key is provided
Raised when an invalid SDK key is provided
"""

def __init__(self, api_key: str) -> None:
super().__init__(f"Invalid API key: {api_key}")
super().__init__(f"Invalid SDK key: {api_key}")


class InvalidApiUrlException(Exception):
Expand Down Expand Up @@ -138,10 +138,10 @@ def __set_api_key(self, api_key: Optional[str]) -> None:
return

if api_key is None:
raise MissingApiKeyException()
raise MissingSdkKeyException()
api_key = str(api_key).strip()
if "-" not in api_key:
raise InvalidApiKeyException(api_key)
raise InvalidSdkKeyException(api_key)
self.api_key = api_key
self.api_key_id = api_key.split("-")[0]

Expand Down
18 changes: 9 additions & 9 deletions tests/test_options.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from sdk_reforge import Options
from sdk_reforge.options import (
MissingApiKeyException,
InvalidApiKeyException,
MissingSdkKeyException,
InvalidSdkKeyException,
InvalidApiUrlException,
InvalidStreamUrlException,
)
Expand Down Expand Up @@ -41,16 +41,16 @@ def test_api_key_from_input_overrides_env(self):
assert options.api_key == "3-dev-api-key"
assert options.api_key_id == "3"

def test_missing_api_key_error(self):
with pytest.raises(MissingApiKeyException) as context:
def test_missing_sdk_key_error(self):
with pytest.raises(MissingSdkKeyException) as context:
Options()

assert "No API key found" in str(context)
assert "No SDK key found" in str(context)

def test_invalid_api_key_error(self):
with pytest.raises(InvalidApiKeyException) as context:
Options(sdk_key="bad_api_key")
assert "Invalid API key: bad_api_key" in str(context)
def test_invalid_sdk_key_error(self):
with pytest.raises(InvalidSdkKeyException) as context:
Options(sdk_key="bad_sdk_key")
assert "Invalid SDK key: bad_sdk_key" in str(context)

def test_api_key_doesnt_matter_local_only_set_in_env(self):
with extended_env({"REFORGE_DATASOURCES": "LOCAL_ONLY"}):
Expand Down
Loading