Add context manager and close() to ClientBase#121
Open
hownowstephen wants to merge 1 commit intomainfrom
Open
Conversation
Allows using CustomerIO and APIClient as context managers to properly close the underlying requests Session and avoid ResourceWarning for unclosed SSL sockets. Closes #87
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
__enter__,__exit__, andclose()toClientBaseCustomerIOandAPIClientinherit this, enabling:close()is also available standalone for non-context-manager usageCloses #87
Test plan
test_context_manager_closes_session— session closed on__exit__,_current_sessionresettest_close_without_session— callingclose()before any request is safetest_close_resets_session— callingclose()after requests cleans up properlyNote
Low Risk
Low risk: adds optional lifecycle helpers for HTTP session cleanup without changing request/response behavior. Main risk is unexpected session closure if callers reuse a client after
with/close()without re-sending a request to recreate the session.Overview
Adds context-manager support to
ClientBasevia__enter__/__exit__plus a newclose()method that closes and clears the pooled_current_session.Extends unit tests to verify sessions are automatically closed when using
with client: ..., thatclose()is safe before any request, and thatclose()resets/cleans up an existing session.Reviewed by Cursor Bugbot for commit 4f51730. Bugbot is set up for automated code reviews on this repo. Configure here.