Skip to content

Commit aafd0ae

Browse files
committed
feat: Update schema caching mechanism and add example for fetching enums
- Changed default schema cache TTL from 24 hours to 15 days in VNDB client. - Refactored get_schema method to utilize the new caching system. - Added get_enums method to retrieve enum definitions from the cached schema. - Created a new example script (schema_enums_example.py) demonstrating how to fetch and display schema enums with caching. - Added initial schema cache file (.veedb_cache/schema.json) containing the VNDB API schema.
1 parent 5131b7d commit aafd0ae

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

examples/schema_enums_example.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Example demonstrating how to get schema enums with caching.
4+
The schema is cached for 15 days to avoid unnecessary API calls.
5+
"""
6+
7+
import asyncio
8+
from veedb import VNDB
9+
10+
async def main():
11+
async with VNDB() as client:
12+
print("Fetching VNDB schema enums (cached for 15 days)...")
13+
14+
# Get all enums from the schema
15+
enums = await client.get_enums()
16+
17+
print(f"\nFound {len(enums)} enum types:")
18+
for enum_name in enums.keys():
19+
print(f" - {enum_name}")
20+
21+
# Example: Show language enum values
22+
if 'language' in enums:
23+
languages = enums['language']
24+
print(f"\nLanguage enum has {len(languages)} values:")
25+
for lang in languages[:5]: # Show first 5
26+
print(f" {lang['id']}: {lang['label']}")
27+
if len(languages) > 5:
28+
print(f" ... and {len(languages) - 5} more")
29+
30+
# Example: Show platform enum values
31+
if 'platform' in enums:
32+
platforms = enums['platform']
33+
print(f"\nPlatform enum has {len(platforms)} values:")
34+
for platform in platforms[:5]: # Show first 5
35+
print(f" {platform['id']}: {platform['label']}")
36+
if len(platforms) > 5:
37+
print(f" ... and {len(platforms) - 5} more")
38+
39+
if __name__ == "__main__":
40+
asyncio.run(main())

src/veedb/client.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def __init__(
263263
session: Optional[aiohttp.ClientSession] = None,
264264
local_schema_path: Optional[str] = None,
265265
schema_cache_dir: str = ".veedb_cache",
266-
schema_cache_ttl_hours: float = 24.0,
266+
schema_cache_ttl_hours: float = 15 * 24, # Default to 15 days
267267
):
268268
self.api_token = api_token
269269
self.base_url = SANDBOX_URL if use_sandbox else BASE_URL
@@ -322,9 +322,17 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
322322
await self.close()
323323

324324
async def get_schema(self) -> dict:
325-
url = f"{self.base_url}/schema"
326-
session = self._get_session()
327-
return await _fetch_api(session=session, method="GET", url=url, token=self.api_token)
325+
"""
326+
Get the VNDB API schema, using cache if available and not older than configured TTL.
327+
Downloads and caches the schema if cache doesn't exist or is expired.
328+
Uses the same schema cache as the filter validation system.
329+
"""
330+
return await self._schema_cache_instance.get_schema(self)
331+
332+
async def get_enums(self) -> Dict[str, Any]:
333+
"""Get enum definitions from the VNDB API schema (uses shared schema cache)."""
334+
schema = await self.get_schema()
335+
return schema.get("enums", {})
328336

329337
async def get_stats(self) -> UserStats:
330338
url = f"{self.base_url}/stats"

0 commit comments

Comments
 (0)