You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add context manager support with HTTP connection pooling (#117)
## Summary
- Enable `with DataverseClient(...) as client:` for automatic resource
cleanup and HTTP connection pooling
- Add `close()` method for explicit lifecycle management (idempotent,
safe to call multiple times)
- Thread `requests.Session` through `DataverseClient` → `_ODataClient` →
`_HttpClient` for TCP/TLS reuse
- Guard all operations against use-after-close via `_check_closed()` in
`_scoped_odata()` — raises `RuntimeError("DataverseClient is closed")`
- Clear all internal caches (entity set, primary ID, picklist) on close
- Full backward compatibility: client works identically without `with`
statement
### Files changed
- `src/PowerPlatform/Dataverse/core/_http.py` — session parameter +
`close()`
- `src/PowerPlatform/Dataverse/data/_odata.py` — session threading +
`close()` with cache clearing
- `src/PowerPlatform/Dataverse/client.py` —
`__enter__`/`__exit__`/`close()`/`_check_closed()`
- `tests/unit/test_context_manager.py` — 34 new tests (protocol, session
lifecycle, close behavior, closed-state guards, backward compat,
exception handling)
## Test plan
- [x] 34 new unit tests covering all context manager behavior
- [x] Full test suite passes (195/195)
- [ ] Manual verification with live Dataverse environment
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: tpellissier <tpellissier@microsoft.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: README.md
+10-8Lines changed: 10 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -113,6 +113,7 @@ The SDK provides a simple, pythonic interface for Dataverse operations:
113
113
| Concept | Description |
114
114
|---------|-------------|
115
115
|**DataverseClient**| Main entry point; provides `records`, `query`, `tables`, and `files` namespaces |
116
+
|**Context Manager**| Use `with DataverseClient(...) as client:` for automatic cleanup and HTTP connection pooling |
116
117
|**Namespaces**| Operations are organized into `client.records` (CRUD & OData queries), `client.query` (query & search), `client.tables` (metadata), and `client.files` (file uploads) |
117
118
|**Records**| Dataverse records represented as Python dictionaries with column schema names |
118
119
|**Schema names**| Use table schema names (`"account"`, `"new_MyTestTable"`) and column schema names (`"name"`, `"new_MyTestColumn"`). See: [Table definitions in Microsoft Dataverse](https://learn.microsoft.com/en-us/power-apps/developer/data-platform/entity-metadata)|
@@ -131,17 +132,18 @@ from PowerPlatform.Dataverse.client import DataverseClient
0 commit comments