flagswitch-sdk 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ Metadata-Version: 2.4
2
+ Name: flagswitch-sdk
3
+ Version: 0.1.0
4
+ Summary: Python SDK for FlagSwitch feature flag management
5
+ Requires-Python: >=3.11
6
+ Requires-Dist: httpx>=0.27
@@ -0,0 +1,13 @@
1
+ from flagswitch_sdk.client import FlagSwitch
2
+ from flagswitch_sdk.exceptions import (
3
+ FlagSwitchConnectionError,
4
+ FlagSwitchError,
5
+ InvalidApiKeyError,
6
+ )
7
+
8
+ __all__ = [
9
+ "FlagSwitch",
10
+ "FlagSwitchError",
11
+ "InvalidApiKeyError",
12
+ "FlagSwitchConnectionError",
13
+ ]
@@ -0,0 +1,63 @@
1
+ import time
2
+ from typing import Any
3
+
4
+ import httpx
5
+
6
+ from flagswitch_sdk.exceptions import FlagSwitchConnectionError, InvalidApiKeyError
7
+
8
+
9
+ _BASE_URL = "http://localhost:8010/api/fs"
10
+ _CACHE_TTL = 30
11
+
12
+
13
+ class FlagSwitch:
14
+ def __init__(self, api_key: str):
15
+ self._api_key = api_key
16
+ self._cache_ttl = _CACHE_TTL
17
+ self._cache: dict[str, Any] = {}
18
+ self._cache_ts: float = 0
19
+
20
+ def _is_cache_valid(self) -> bool:
21
+ return bool(self._cache) and (time.time() - self._cache_ts) < self._cache_ttl
22
+
23
+ async def _fetch_flags(self) -> dict[str, Any]:
24
+ try:
25
+ async with httpx.AsyncClient() as http:
26
+ response = await http.get(
27
+ f"{_BASE_URL}/evaluate",
28
+ headers={"X-Api-Key": self._api_key},
29
+ timeout=5.0,
30
+ )
31
+ except httpx.RequestError as e:
32
+ raise FlagSwitchConnectionError(f"Failed to reach FlagSwitch server: {e}")
33
+
34
+ if response.status_code == 401:
35
+ raise InvalidApiKeyError("Invalid or inactive API key.")
36
+
37
+ if response.status_code != 200:
38
+ raise FlagSwitchConnectionError(
39
+ f"Unexpected response from FlagSwitch: {response.status_code}"
40
+ )
41
+
42
+ payload = response.json()
43
+ return payload.get("data") or {}
44
+
45
+ async def _get_flags(self) -> dict[str, Any]:
46
+ if self._is_cache_valid():
47
+ return self._cache
48
+
49
+ flags = await self._fetch_flags()
50
+ self._cache = flags
51
+ self._cache_ts = time.time()
52
+ return flags
53
+
54
+ async def is_enabled(self, key: str, default: bool = False) -> bool:
55
+ flags = await self._get_flags()
56
+ return bool(flags.get(key, default))
57
+
58
+ async def get_all_flags(self) -> dict[str, Any]:
59
+ return await self._get_flags()
60
+
61
+ def invalidate_cache(self) -> None:
62
+ self._cache = {}
63
+ self._cache_ts = 0
@@ -0,0 +1,10 @@
1
+ class FlagSwitchError(Exception):
2
+ pass
3
+
4
+
5
+ class InvalidApiKeyError(FlagSwitchError):
6
+ pass
7
+
8
+
9
+ class FlagSwitchConnectionError(FlagSwitchError):
10
+ pass
@@ -0,0 +1,6 @@
1
+ Metadata-Version: 2.4
2
+ Name: flagswitch-sdk
3
+ Version: 0.1.0
4
+ Summary: Python SDK for FlagSwitch feature flag management
5
+ Requires-Python: >=3.11
6
+ Requires-Dist: httpx>=0.27
@@ -0,0 +1,9 @@
1
+ pyproject.toml
2
+ flagswitch_sdk/__init__.py
3
+ flagswitch_sdk/client.py
4
+ flagswitch_sdk/exceptions.py
5
+ flagswitch_sdk.egg-info/PKG-INFO
6
+ flagswitch_sdk.egg-info/SOURCES.txt
7
+ flagswitch_sdk.egg-info/dependency_links.txt
8
+ flagswitch_sdk.egg-info/requires.txt
9
+ flagswitch_sdk.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ httpx>=0.27
@@ -0,0 +1 @@
1
+ flagswitch_sdk
@@ -0,0 +1,14 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "flagswitch-sdk"
7
+ version = "0.1.0"
8
+ description = "Python SDK for FlagSwitch feature flag management"
9
+ requires-python = ">=3.11"
10
+ dependencies = ["httpx>=0.27"]
11
+
12
+ [tool.setuptools.packages.find]
13
+ where = ["."]
14
+ include = ["flagswitch_sdk*"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+