configvault-sdk 0.1.0__py3-none-any.whl

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,25 @@
1
+ """ConfigVault Python SDK."""
2
+
3
+ from configvault.client import ConfigVaultClient
4
+ from configvault.models import ConfigResponse, ConfigListResponse, HealthResponse
5
+ from configvault.exceptions import (
6
+ ConfigVaultError,
7
+ ConfigNotFoundError,
8
+ AuthenticationError,
9
+ ServiceUnavailableError,
10
+ )
11
+ from configvault.watcher import ConfigWatcher, ConfigChangedEvent
12
+
13
+ __version__ = "0.1.0"
14
+ __all__ = [
15
+ "ConfigVaultClient",
16
+ "ConfigResponse",
17
+ "ConfigListResponse",
18
+ "HealthResponse",
19
+ "ConfigVaultError",
20
+ "ConfigNotFoundError",
21
+ "AuthenticationError",
22
+ "ServiceUnavailableError",
23
+ "ConfigWatcher",
24
+ "ConfigChangedEvent",
25
+ ]
configvault/client.py ADDED
@@ -0,0 +1,165 @@
1
+ """ConfigVault API client."""
2
+
3
+ from typing import Optional
4
+
5
+ import httpx
6
+
7
+ from configvault.exceptions import (
8
+ AuthenticationError,
9
+ ConfigNotFoundError,
10
+ ConfigVaultError,
11
+ ServiceUnavailableError,
12
+ )
13
+ from configvault.models import ConfigListResponse, ConfigResponse, HealthResponse
14
+
15
+
16
+ class ConfigVaultClient:
17
+ """Async client for ConfigVault API."""
18
+
19
+ def __init__(
20
+ self,
21
+ base_url: str,
22
+ api_key: str,
23
+ timeout: float = 30.0,
24
+ ) -> None:
25
+ """
26
+ Initialize the ConfigVault client.
27
+
28
+ Args:
29
+ base_url: Base URL of the ConfigVault API (e.g., "http://localhost:5000")
30
+ api_key: API key for authentication
31
+ timeout: Request timeout in seconds
32
+ """
33
+ self._base_url = base_url.rstrip("/")
34
+ self._api_key = api_key
35
+ self._timeout = timeout
36
+ self._client: Optional[httpx.AsyncClient] = None
37
+
38
+ async def _get_client(self) -> httpx.AsyncClient:
39
+ """Get or create the HTTP client."""
40
+ if self._client is None or self._client.is_closed:
41
+ self._client = httpx.AsyncClient(
42
+ base_url=self._base_url,
43
+ headers={"X-Api-Key": self._api_key},
44
+ timeout=self._timeout,
45
+ )
46
+ return self._client
47
+
48
+ async def close(self) -> None:
49
+ """Close the HTTP client."""
50
+ if self._client is not None:
51
+ await self._client.aclose()
52
+ self._client = None
53
+
54
+ async def __aenter__(self) -> "ConfigVaultClient":
55
+ """Async context manager entry."""
56
+ return self
57
+
58
+ async def __aexit__(self, *args) -> None:
59
+ """Async context manager exit."""
60
+ await self.close()
61
+
62
+ def watch(self, filter_pattern: str | None = None) -> "ConfigWatcher":
63
+ """
64
+ Create a watcher for configuration changes.
65
+
66
+ Args:
67
+ filter_pattern: Optional glob pattern to filter keys (e.g., "production/*")
68
+
69
+ Returns:
70
+ ConfigWatcher instance for async iteration
71
+ """
72
+ from configvault.watcher import ConfigWatcher
73
+
74
+ return ConfigWatcher(
75
+ base_url=self._base_url,
76
+ api_key=self._api_key,
77
+ filter_pattern=filter_pattern,
78
+ )
79
+
80
+ def _handle_error_response(self, response: httpx.Response, key: Optional[str] = None) -> None:
81
+ """Handle error responses from the API."""
82
+ if response.status_code == 401:
83
+ raise AuthenticationError()
84
+ if response.status_code == 404 and key:
85
+ raise ConfigNotFoundError(key)
86
+ if response.status_code == 503:
87
+ raise ServiceUnavailableError()
88
+ if response.status_code >= 400:
89
+ raise ConfigVaultError(f"API error: {response.status_code}")
90
+
91
+ async def get(self, key: str) -> str:
92
+ """
93
+ Get a configuration value by key.
94
+
95
+ Args:
96
+ key: Hierarchical key (e.g., "production/database/connection")
97
+
98
+ Returns:
99
+ The configuration value
100
+
101
+ Raises:
102
+ ConfigNotFoundError: If the key does not exist
103
+ AuthenticationError: If the API key is invalid
104
+ ServiceUnavailableError: If the service is unavailable
105
+ """
106
+ client = await self._get_client()
107
+ response = await client.get(f"/config/{key}")
108
+
109
+ self._handle_error_response(response, key)
110
+
111
+ data = ConfigResponse.model_validate(response.json())
112
+ return data.value
113
+
114
+ async def exists(self, key: str) -> bool:
115
+ """
116
+ Check if a configuration key exists.
117
+
118
+ Args:
119
+ key: Hierarchical key to check
120
+
121
+ Returns:
122
+ True if the key exists, False otherwise
123
+ """
124
+ client = await self._get_client()
125
+ response = await client.head(f"/config/{key}")
126
+
127
+ if response.status_code == 404:
128
+ return False
129
+ if response.status_code == 401:
130
+ raise AuthenticationError()
131
+ if response.status_code == 503:
132
+ raise ServiceUnavailableError()
133
+
134
+ return response.status_code == 200
135
+
136
+ async def list(self, namespace: str) -> dict[str, str]:
137
+ """
138
+ List all configurations in a namespace.
139
+
140
+ Args:
141
+ namespace: The namespace (folder) to list
142
+
143
+ Returns:
144
+ Dictionary mapping keys to values
145
+ """
146
+ client = await self._get_client()
147
+ response = await client.get("/config", params={"prefix": namespace})
148
+
149
+ self._handle_error_response(response)
150
+
151
+ data = ConfigListResponse.model_validate(response.json())
152
+ return data.configs
153
+
154
+ async def health(self) -> HealthResponse:
155
+ """
156
+ Check the health of the ConfigVault service.
157
+
158
+ Returns:
159
+ Health status information
160
+ """
161
+ client = await self._get_client()
162
+ # Health endpoint doesn't require API key, but we send it anyway
163
+ response = await client.get("/health")
164
+
165
+ return HealthResponse.model_validate(response.json())
@@ -0,0 +1,29 @@
1
+ """ConfigVault SDK exceptions."""
2
+
3
+
4
+ class ConfigVaultError(Exception):
5
+ """Base exception for ConfigVault SDK."""
6
+
7
+ pass
8
+
9
+
10
+ class ConfigNotFoundError(ConfigVaultError):
11
+ """Raised when a configuration key is not found."""
12
+
13
+ def __init__(self, key: str) -> None:
14
+ self.key = key
15
+ super().__init__(f"Configuration key '{key}' not found")
16
+
17
+
18
+ class AuthenticationError(ConfigVaultError):
19
+ """Raised when API key authentication fails."""
20
+
21
+ def __init__(self, message: str = "Invalid or missing API key") -> None:
22
+ super().__init__(message)
23
+
24
+
25
+ class ServiceUnavailableError(ConfigVaultError):
26
+ """Raised when the ConfigVault service is unavailable."""
27
+
28
+ def __init__(self, message: str = "ConfigVault service unavailable") -> None:
29
+ super().__init__(message)
configvault/models.py ADDED
@@ -0,0 +1,27 @@
1
+ """ConfigVault SDK models."""
2
+
3
+ from datetime import datetime
4
+
5
+ from pydantic import BaseModel, Field
6
+
7
+
8
+ class ConfigResponse(BaseModel):
9
+ """Response model for a single configuration value."""
10
+
11
+ key: str
12
+ value: str
13
+
14
+
15
+ class ConfigListResponse(BaseModel):
16
+ """Response model for listing configurations."""
17
+
18
+ namespace: str = Field(alias="namespace")
19
+ configs: dict[str, str]
20
+
21
+
22
+ class HealthResponse(BaseModel):
23
+ """Response model for health check."""
24
+
25
+ status: str
26
+ vault: str
27
+ timestamp: datetime
configvault/py.typed ADDED
File without changes
configvault/watcher.py ADDED
@@ -0,0 +1,94 @@
1
+ """Configuration change watcher using SSE."""
2
+
3
+ from collections.abc import AsyncIterator
4
+ from dataclasses import dataclass
5
+ from datetime import datetime
6
+ import asyncio
7
+ import json
8
+
9
+ import httpx
10
+ from httpx_sse import aconnect_sse
11
+
12
+ from configvault.exceptions import AuthenticationError, ServiceUnavailableError
13
+
14
+
15
+ @dataclass
16
+ class ConfigChangedEvent:
17
+ """Event emitted when configuration changes are detected."""
18
+
19
+ keys: list[str]
20
+ timestamp: datetime
21
+
22
+
23
+ class ConfigWatcher:
24
+ """Watches for configuration changes via SSE."""
25
+
26
+ def __init__(
27
+ self,
28
+ base_url: str,
29
+ api_key: str,
30
+ filter_pattern: str | None = None,
31
+ reconnect_delay: float = 5.0,
32
+ ) -> None:
33
+ """
34
+ Initialize the watcher.
35
+
36
+ Args:
37
+ base_url: Base URL of the ConfigVault API.
38
+ api_key: API key for authentication.
39
+ filter_pattern: Optional glob pattern to filter keys.
40
+ reconnect_delay: Delay before reconnecting after a failure.
41
+ """
42
+ self._base_url = base_url.rstrip("/")
43
+ self._api_key = api_key
44
+ self._filter_pattern = filter_pattern
45
+ self._reconnect_delay = reconnect_delay
46
+ self._running = False
47
+
48
+ async def watch(self) -> AsyncIterator[ConfigChangedEvent]:
49
+ """
50
+ Watch for configuration changes.
51
+
52
+ Yields:
53
+ ConfigChangedEvent instances when changes are detected.
54
+ """
55
+ self._running = True
56
+
57
+ while self._running:
58
+ try:
59
+ async for event in self._connect():
60
+ yield event
61
+ except httpx.HTTPStatusError as exc:
62
+ if exc.response.status_code == 401:
63
+ raise AuthenticationError() from exc
64
+ if exc.response.status_code == 503:
65
+ raise ServiceUnavailableError() from exc
66
+ raise
67
+ except (httpx.ConnectError, httpx.ReadError):
68
+ if self._running:
69
+ await asyncio.sleep(self._reconnect_delay)
70
+
71
+ async def _connect(self) -> AsyncIterator[ConfigChangedEvent]:
72
+ """Connect to the SSE endpoint and yield events."""
73
+ url = f"{self._base_url}/events"
74
+ if self._filter_pattern:
75
+ url += f"?filter={self._filter_pattern}"
76
+
77
+ async with httpx.AsyncClient() as client:
78
+ async with aconnect_sse(
79
+ client,
80
+ "GET",
81
+ url,
82
+ headers={"X-Api-Key": self._api_key},
83
+ ) as event_source:
84
+ async for sse in event_source.aiter_sse():
85
+ if sse.event == "config-changed":
86
+ data = json.loads(sse.data)
87
+ timestamp = datetime.fromisoformat(
88
+ data["timestamp"].replace("Z", "+00:00")
89
+ )
90
+ yield ConfigChangedEvent(keys=data["keys"], timestamp=timestamp)
91
+
92
+ def stop(self) -> None:
93
+ """Stop watching for changes."""
94
+ self._running = False
@@ -0,0 +1,85 @@
1
+ Metadata-Version: 2.4
2
+ Name: configvault-sdk
3
+ Version: 0.1.0
4
+ Summary: Python SDK for ConfigVault API
5
+ Project-URL: Homepage, https://github.com/sitien173/config-vault
6
+ Project-URL: Repository, https://github.com/sitien173/config-vault
7
+ Project-URL: Issues, https://github.com/sitien173/config-vault/issues
8
+ Author: nst173
9
+ License-Expression: MIT
10
+ Keywords: config,configvault,sdk,vault
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Typing :: Typed
20
+ Requires-Python: >=3.10
21
+ Requires-Dist: httpx-sse>=0.4.0
22
+ Requires-Dist: httpx>=0.27.0
23
+ Requires-Dist: pydantic>=2.0.0
24
+ Provides-Extra: dev
25
+ Requires-Dist: pytest-asyncio>=0.23.0; extra == 'dev'
26
+ Requires-Dist: pytest>=8.0.0; extra == 'dev'
27
+ Requires-Dist: respx>=0.21.0; extra == 'dev'
28
+ Description-Content-Type: text/markdown
29
+
30
+ # ConfigVault Python SDK
31
+
32
+ Python client for the ConfigVault configuration management API.
33
+
34
+ ## Installation
35
+
36
+ ```bash
37
+ pip install configvault-sdk
38
+ ```
39
+
40
+ ## Usage
41
+
42
+ ```python
43
+ from configvault import ConfigVaultClient
44
+
45
+ client = ConfigVaultClient(
46
+ base_url="http://localhost:5000",
47
+ api_key="your-api-key"
48
+ )
49
+
50
+ # Get a configuration value
51
+ value = await client.get("production/database/connection")
52
+
53
+ # Check if key exists
54
+ exists = await client.exists("production/database/connection")
55
+
56
+ # List all configs in namespace
57
+ configs = await client.list("production")
58
+
59
+ # Check service health
60
+ health = await client.health()
61
+ ```
62
+
63
+ ## Watching for Changes
64
+
65
+ ```python
66
+ from configvault import ConfigVaultClient
67
+
68
+ client = ConfigVaultClient(
69
+ base_url="http://localhost:5000",
70
+ api_key="your-api-key"
71
+ )
72
+
73
+ # Watch all changes
74
+ watcher = client.watch()
75
+
76
+ # Or filter by pattern
77
+ watcher = client.watch("production/*")
78
+
79
+ async for event in watcher.watch():
80
+ print(f"Changed keys: {event.keys}")
81
+ print(f"Timestamp: {event.timestamp}")
82
+
83
+ # Stop watching
84
+ watcher.stop()
85
+ ```
@@ -0,0 +1,9 @@
1
+ configvault/__init__.py,sha256=ESiYECMXPPpbT6nKSZK2uleDtoQ09NV-fsS6t-ohbqg,681
2
+ configvault/client.py,sha256=wBGLlSXG7XFA-2tzur5CkUC8I508rDeMyrsnF_YsQqo,5181
3
+ configvault/exceptions.py,sha256=9ixmSz5tu-AkAIer9y3h3nxjZS0fwyzl_dRKsZyyUbg,829
4
+ configvault/models.py,sha256=r9aWA1707i97KZwBHEgrAFx9UusU5HMMN0rV8n2lK2g,549
5
+ configvault/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ configvault/watcher.py,sha256=LTZDa2yb16KPFnSsrE9D87iyWlSsEdsRtvmpVcvDxXk,3146
7
+ configvault_sdk-0.1.0.dist-info/METADATA,sha256=AbN9IsaLfmiGl1S698rBhTjZ_B1uiwFnJjRoMfjAWo4,2170
8
+ configvault_sdk-0.1.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
9
+ configvault_sdk-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.28.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any