Skip to content

Commit 164c87c

Browse files
committed
chore: update examples
1 parent b71a51c commit 164c87c

16 files changed

Lines changed: 254 additions & 282 deletions

File tree

docs/source/errors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ReplaneError (base class)
1515
└── MissingDependencyError
1616
```
1717

18-
Note: Accessing a missing config via `client.configs["name"]` raises a standard `KeyError`, not `ConfigNotFoundError`. Use `client.configs.get("name", default)` to avoid exceptions.
18+
Note: Accessing a missing config via `replane.configs["name"]` raises a standard `KeyError`, not `ConfigNotFoundError`. Use `replane.configs.get("name", default)` to avoid exceptions.
1919

2020
## Error Codes
2121

examples/basic-async/main.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,22 @@ async def main():
2727
},
2828
# Optional: enable debug logging
2929
debug=True,
30-
) as client:
30+
) as replane:
3131
# Read a boolean feature flag (sync - reads from local cache)
32-
is_feature_enabled = client.get("feature-enabled")
32+
is_feature_enabled = replane.configs["feature-enabled"]
3333
print(f"Feature enabled: {is_feature_enabled}")
3434

3535
# Read a numeric config
36-
max_items = client.get("max-items")
36+
max_items = replane.configs["max-items"]
3737
print(f"Max items: {max_items}")
3838

3939
# Read with context for override evaluation
40-
rate_limit = client.get(
41-
"rate-limit",
42-
context={"plan": "premium", "user_id": "user-123"},
43-
)
40+
user_client = replane.with_context({"plan": "premium", "user_id": "user-123"})
41+
rate_limit = user_client.configs["rate-limit"]
4442
print(f"Rate limit: {rate_limit}")
4543

4644
# Read with default value if config doesn't exist
47-
timeout = client.get("request-timeout", default=30)
45+
timeout = replane.configs.get("request-timeout", 30)
4846
print(f"Timeout: {timeout}")
4947

5048
# Keep running to receive real-time updates
@@ -53,49 +51,49 @@ async def main():
5351
while True:
5452
await asyncio.sleep(5)
5553
# Re-read to see any updates
56-
current_value = client.get("feature-enabled")
54+
current_value = replane.configs["feature-enabled"]
5755
print(f"Current feature-enabled value: {current_value}")
5856
except KeyboardInterrupt:
5957
print("\nStopping...")
6058

6159

6260
async def example_manual_lifecycle():
6361
"""Example showing manual connect/close lifecycle."""
64-
client = AsyncReplane(
62+
replane = AsyncReplane(
6563
base_url=BASE_URL,
6664
sdk_key=SDK_KEY,
6765
)
6866

6967
try:
7068
# Connect and wait for initial configs
71-
await client.connect(wait=True)
69+
await replane.connect(wait=True)
7270

7371
# Now you can read configs
74-
value = client.get("my-config")
72+
value = replane.configs["my-config"]
7573
print(f"Config value: {value}")
7674

7775
finally:
7876
# Always close the client when done
79-
await client.close()
77+
await replane.close()
8078

8179

8280
async def example_with_subscriptions():
8381
"""Example showing how to subscribe to config changes."""
8482
async with AsyncReplane(
8583
base_url=BASE_URL,
8684
sdk_key=SDK_KEY,
87-
) as client:
85+
) as replane:
8886
# Subscribe to all config changes
8987
def on_any_change(name, config):
9088
print(f"Config '{name}' changed to: {config.value}")
9189

92-
unsubscribe_all = client.subscribe(on_any_change)
90+
unsubscribe_all = replane.subscribe(on_any_change)
9391

9492
# Subscribe to a specific config
9593
def on_feature_change(config):
9694
print(f"Feature flag updated: {config.value}")
9795

98-
unsubscribe_feature = client.subscribe_config("feature-enabled", on_feature_change)
96+
unsubscribe_feature = replane.subscribe_config("feature-enabled", on_feature_change)
9997

10098
# Keep running to receive updates
10199
print("Listening for changes...")
@@ -111,15 +109,15 @@ async def example_async_callback():
111109
async with AsyncReplane(
112110
base_url=BASE_URL,
113111
sdk_key=SDK_KEY,
114-
) as client:
112+
) as replane:
115113
# Async callbacks are supported
116114
async def on_change(name, config):
117115
print(f"Config '{name}' changed")
118116
# Can do async operations here
119117
await asyncio.sleep(0.1)
120118
print(f"Processed change for '{name}'")
121119

122-
client.subscribe(on_change)
120+
replane.subscribe(on_change)
123121

124122
await asyncio.sleep(60)
125123

examples/basic-sync/main.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,70 +25,68 @@ def main():
2525
},
2626
# Optional: enable debug logging
2727
debug=True,
28-
) as client:
28+
) as replane:
2929
# Read a boolean feature flag
30-
is_feature_enabled = client.get("feature-enabled")
30+
is_feature_enabled = replane.configs["feature-enabled"]
3131
print(f"Feature enabled: {is_feature_enabled}")
3232

3333
# Read a numeric config
34-
max_items = client.get("max-items")
34+
max_items = replane.configs["max-items"]
3535
print(f"Max items: {max_items}")
3636

3737
# Read with context for override evaluation
38-
rate_limit = client.get(
39-
"rate-limit",
40-
context={"plan": "premium", "user_id": "user-123"},
41-
)
38+
user_client = replane.with_context({"plan": "premium", "user_id": "user-123"})
39+
rate_limit = user_client.configs["rate-limit"]
4240
print(f"Rate limit: {rate_limit}")
4341

4442
# Read with default value if config doesn't exist
45-
timeout = client.get("request-timeout", default=30)
43+
timeout = replane.configs.get("request-timeout", 30)
4644
print(f"Timeout: {timeout}")
4745

4846

4947
def example_manual_lifecycle():
5048
"""Example showing manual connect/close lifecycle."""
51-
client = Replane(
49+
replane = Replane(
5250
base_url=BASE_URL,
5351
sdk_key=SDK_KEY,
5452
)
5553

5654
try:
5755
# Connect and wait for initial configs
58-
client.connect(wait=True)
56+
replane.connect(wait=True)
5957

6058
# Now you can read configs
61-
value = client.get("my-config")
59+
value = replane.configs["my-config"]
6260
print(f"Config value: {value}")
6361

6462
finally:
6563
# Always close the client when done
66-
client.close()
64+
replane.close()
6765

6866

6967
def example_non_blocking_connect():
7068
"""Example showing non-blocking connection."""
71-
client = Replane(
69+
replane = Replane(
7270
base_url=BASE_URL,
7371
sdk_key=SDK_KEY,
7472
defaults={"my-config": "default-value"},
7573
)
7674

7775
# Start connection without waiting
78-
client.connect(wait=False)
76+
replane.connect(wait=False)
7977

8078
# Can use default values immediately
81-
value = client.get("my-config")
79+
value = replane.configs.get("my-config", "default-value")
8280
print(f"Initial value (may be default): {value}")
8381

8482
# Later, wait for initialization if needed
85-
client.wait_for_init()
83+
replane.wait_for_init()
8684

8785
# Now we have fresh values from server
88-
value = client.get("my-config")
86+
value = replane.configs["my-config"]
8987
print(f"Server value: {value}")
9088

91-
client.close()
89+
replane.close()
9290

9391

9492
if __name__ == "__main__":

examples/django-integration/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ class DemoConfig(AppConfig):
140140
```python
141141
from demo.replane_client import get_replane
142142

143-
client = get_replane()
144-
value = client.get("feature-flag", context={"user_id": "123"})
143+
replane = get_replane()
144+
user_client = replane.with_context({"user_id": "123"})
145+
value = user_client.configs["feature-flag"]
145146
```
146147

147148
### 4. Middleware (`demo/middleware.py`)

examples/django-integration/demo/views.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ class IndexView(View):
99
"""Homepage with feature flag check."""
1010

1111
def get(self, request):
12-
client = get_replane()
12+
replane = get_replane()
1313
ctx = getattr(request, "replane_context", {})
1414

1515
# Check if new dashboard is enabled for this user
16-
new_dashboard = client.get("new-dashboard-enabled", context=ctx)
16+
user_client = replane.with_context(ctx)
17+
new_dashboard = user_client.configs["new-dashboard-enabled"]
1718

1819
if new_dashboard:
1920
return JsonResponse(
@@ -35,11 +36,12 @@ class ItemsView(View):
3536
"""List items with configurable rate limiting."""
3637

3738
def get(self, request):
38-
client = get_replane()
39+
replane = get_replane()
3940
ctx = getattr(request, "replane_context", {})
4041

4142
# Get rate limit for this user
42-
rate_limit = client.get("rate-limit", context=ctx)
43+
user_client = replane.with_context(ctx)
44+
rate_limit = user_client.configs["rate-limit"]
4345

4446
items = [
4547
{"id": 1, "name": "Item 1"},
@@ -60,11 +62,12 @@ class UploadView(View):
6062
"""Upload endpoint with configurable size limit."""
6163

6264
def post(self, request):
63-
client = get_replane()
65+
replane = get_replane()
6466
ctx = getattr(request, "replane_context", {})
6567

6668
# Get the max upload size based on user's plan
67-
max_size_mb = client.get("max-upload-size-mb", context=ctx)
69+
user_client = replane.with_context(ctx)
70+
max_size_mb = user_client.configs["max-upload-size-mb"]
6871

6972
content_length = int(request.headers.get("Content-Length", 0))
7073
max_bytes = max_size_mb * 1024 * 1024
@@ -90,17 +93,18 @@ class ConfigView(View):
9093
"""Debug endpoint to view current config values."""
9194

9295
def get(self, request):
93-
client = get_replane()
96+
replane = get_replane()
9497
ctx = getattr(request, "replane_context", {})
9598

99+
user_client = replane.with_context(ctx)
96100
return JsonResponse(
97101
{
98102
"context": ctx,
99103
"configs": {
100-
"new-dashboard-enabled": client.get("new-dashboard-enabled", context=ctx),
101-
"rate-limit": client.get("rate-limit", context=ctx),
102-
"max-upload-size-mb": client.get("max-upload-size-mb", context=ctx),
103-
"maintenance-mode": client.get("maintenance-mode", context=ctx),
104+
"new-dashboard-enabled": user_client.configs["new-dashboard-enabled"],
105+
"rate-limit": user_client.configs["rate-limit"],
106+
"max-upload-size-mb": user_client.configs["max-upload-size-mb"],
107+
"maintenance-mode": user_client.configs["maintenance-mode"],
104108
},
105109
}
106110
)
@@ -111,8 +115,8 @@ class HealthView(View):
111115

112116
def get(self, request):
113117
try:
114-
client = get_replane()
115-
replane_connected = client.is_initialized()
118+
replane = get_replane()
119+
replane_connected = replane.is_initialized()
116120
except RuntimeError:
117121
replane_connected = False
118122

examples/fastapi-integration/app.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class UploadResponse(BaseModel):
9898
@app.middleware("http")
9999
async def check_maintenance_mode(request: Request, call_next):
100100
if replane_client and request.url.path != "/health":
101-
maintenance = replane_client.get("maintenance-mode", default=False)
101+
maintenance = replane_client.configs.get("maintenance-mode", False)
102102
if maintenance:
103103
return HTTPException(
104104
status_code=503,
@@ -113,7 +113,8 @@ async def index(
113113
ctx: Annotated[dict, Depends(get_user_context)],
114114
):
115115
"""Homepage with feature flag check."""
116-
new_dashboard = replane.get("new-dashboard-enabled", context=ctx)
116+
user_client = replane.with_context(ctx)
117+
new_dashboard = user_client.configs["new-dashboard-enabled"]
117118

118119
if new_dashboard:
119120
return WelcomeResponse(message="Welcome to the new dashboard!", version="v2")
@@ -127,7 +128,8 @@ async def get_items(
127128
ctx: Annotated[dict, Depends(get_user_context)],
128129
):
129130
"""List items with configurable rate limiting."""
130-
rate_limit = replane.get("rate-limit", context=ctx)
131+
user_client = replane.with_context(ctx)
132+
rate_limit = user_client.configs["rate-limit"]
131133

132134
items = [
133135
{"id": 1, "name": "Item 1"},
@@ -149,7 +151,8 @@ async def upload(
149151
ctx: Annotated[dict, Depends(get_user_context)],
150152
):
151153
"""Upload endpoint with configurable size limit."""
152-
max_size_mb = replane.get("max-upload-size-mb", context=ctx)
154+
user_client = replane.with_context(ctx)
155+
max_size_mb = user_client.configs["max-upload-size-mb"]
153156

154157
content_length = request.headers.get("content-length", 0)
155158
max_bytes = max_size_mb * 1024 * 1024
@@ -172,13 +175,14 @@ async def get_config(
172175
ctx: Annotated[dict, Depends(get_user_context)],
173176
):
174177
"""Debug endpoint to view current config values."""
178+
user_client = replane.with_context(ctx)
175179
return ConfigResponse(
176180
context=ctx,
177181
configs={
178-
"new-dashboard-enabled": replane.get("new-dashboard-enabled", context=ctx),
179-
"rate-limit": replane.get("rate-limit", context=ctx),
180-
"max-upload-size-mb": replane.get("max-upload-size-mb", context=ctx),
181-
"maintenance-mode": replane.get("maintenance-mode", context=ctx),
182+
"new-dashboard-enabled": user_client.configs["new-dashboard-enabled"],
183+
"rate-limit": user_client.configs["rate-limit"],
184+
"max-upload-size-mb": user_client.configs["max-upload-size-mb"],
185+
"maintenance-mode": user_client.configs["maintenance-mode"],
182186
},
183187
)
184188

0 commit comments

Comments
 (0)