feat: connect() falls back to CAPISCIO_API_KEY env var#62
Conversation
CapiscIO.connect() now reads api_key, agent_id, and server_url from environment variables when not passed explicitly: export CAPISCIO_API_KEY=sk_live_... agent = CapiscIO.connect() # just works This makes the zero-argument call the simplest path for users who have already set their env vars. from_env() still exists for the additional CAPISCIO_AGENT_NAME and CAPISCIO_DEV_MODE env vars.
|
✅ Documentation validation passed!
|
|
✅ All checks passed! Ready for review. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
✅ SDK server contract tests passed (test_server_integration.py). Cross-product scenarios are validated in capiscio-e2e-tests. |
There was a problem hiding this comment.
Pull request overview
This PR updates CapiscIO.connect() to support zero-argument usage by resolving api_key, agent_id, and server_url from environment variables when they aren’t provided explicitly, and simplifies from_env() to delegate to connect().
Changes:
- Made
connect()acceptapi_key: Optional[str] = Noneand added env var fallbacks forCAPISCIO_API_KEY,CAPISCIO_AGENT_ID, andCAPISCIO_SERVER_URL. - Updated
from_env()to only passname/dev_modefrom env and rely onconnect()for the rest. - Adjusted unit tests to match the new
from_env()delegation behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
capiscio_sdk/connect.py |
Adds env var fallback logic to connect() and simplifies from_env() to delegate to it. |
tests/unit/test_connect.py |
Updates from_env() tests to reflect the new delegation behavior. |
| agent_id = os.environ.get("CAPISCIO_AGENT_ID") | ||
| if server_url is None: | ||
| server_url = os.environ.get("CAPISCIO_SERVER_URL", PROD_REGISTRY) |
| if api_key is None: | ||
| api_key = os.environ.get("CAPISCIO_API_KEY") | ||
| if not api_key: | ||
| raise ValueError( | ||
| "api_key is required. Pass it directly or set the " | ||
| "CAPISCIO_API_KEY environment variable. " | ||
| "Get your API key at https://app.capisc.io" | ||
| ) |
| mock_connect.assert_called_once_with( | ||
| api_key="sk_test_env", | ||
| agent_id="env-agent-id", | ||
| name="Env Agent", | ||
| server_url="https://env.server.com", | ||
| dev_mode=True, | ||
| ) |
| mock_connect.assert_called_once_with( | ||
| api_key="sk_test_only", | ||
| agent_id=None, | ||
| name=None, | ||
| server_url=PROD_REGISTRY, | ||
| dev_mode=False, | ||
| ) |
…lback tests - Empty/whitespace CAPISCIO_SERVER_URL now falls back to PROD_REGISTRY instead of passing empty string to httpx - Added 4 unit tests for connect() env var fallback behavior: reads from env, raises when missing, handles empty server_url, reads custom server_url
|
✅ Documentation validation passed!
|
|
✅ All checks passed! Ready for review. |
|
✅ SDK server contract tests passed (test_server_integration.py). Cross-product scenarios are validated in capiscio-e2e-tests. |
CapiscIO.connect()now readsapi_key,agent_id, andserver_urlfrom environment variables when not passed explicitly.Before
After
Env vars read by
connect():CAPISCIO_API_KEY— API key (falls back whenapi_keyparam is None)CAPISCIO_AGENT_ID— Agent ID (falls back whenagent_idparam is None)CAPISCIO_SERVER_URL— Server URL (falls back whenserver_urlparam is None, then production default)Changes
connect():api_keyparameter changed from requiredstrtoOptional[str] = Noneconnect():server_urlparameter changed fromstr = PROD_REGISTRYtoOptional[str] = Nonewith env fallbackconnect(): Added env var resolution at the top of the method bodyfrom_env(): Simplified to delegate toconnect()— only passesnameanddev_modefrom env sinceconnect()handles the rest