diff --git a/pyproject.toml b/pyproject.toml index 6c1da0d..e67ae17 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "rushdb" -version = "1.9.0" +version = "1.10.0" description = "RushDB Python SDK" authors = [ {name = "RushDB Team", email = "hi@rushdb.com"} diff --git a/src/rushdb/client.py b/src/rushdb/client.py index 9bc07a0..415f575 100644 --- a/src/rushdb/client.py +++ b/src/rushdb/client.py @@ -16,6 +16,7 @@ from .api.records import RecordsAPI from .api.transactions import TransactionsAPI from .common import RushDBError +from .utils.token_prefix import extract_mixed_properties_from_token class RushDB: @@ -102,6 +103,18 @@ def __init__(self, api_key: str, base_url: Optional[str] = None): ... base_url="https://my-rushdb.company.com/api/v1" ... ) """ + settings, raw_key = extract_mixed_properties_from_token(api_key) + + self.server_settings = ( + { + "customDB": settings["customDB"], + "managedDB": settings["managedDB"], + "selfHosted": settings["selfHosted"], + "plan_type": settings["planType"], + } + if settings + else None + ) self.base_url = (base_url or self.DEFAULT_BASE_URL).rstrip("/") self.api_key = api_key self.records = RecordsAPI(self) diff --git a/src/rushdb/utils/token_prefix.py b/src/rushdb/utils/token_prefix.py new file mode 100644 index 0000000..9a870c5 --- /dev/null +++ b/src/rushdb/utils/token_prefix.py @@ -0,0 +1,30 @@ +import re +from typing import Dict, Optional, Tuple + +PlanPrefix = { + "initial": "in", + "extended": "ex", + "fullFeatured": "ff", +} + + +def extract_mixed_properties_from_token( + prefixed_token: str, +) -> Tuple[Optional[Dict[str, bool]], str]: + matched = re.match(r"^([a-z]{2})_([01]{4}\d*)_(.+)$", prefixed_token) + if not matched: + return None, prefixed_token + + prefix, bits, raw = matched.groups() + plan = next((p for p, plan in PlanPrefix.items() if plan == prefix), None) + if plan is None: + return None, prefixed_token + + b_custom, b_managed, b_self = tuple(bits[:3]) + settings = { + "planType": plan, + "customDB": b_custom == "1", + "managedDB": b_managed == "1", + "selfHosted": b_self == "1", + } + return settings, raw