protocol_connect: centralize base64 encoding/decoding logic #93
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.
This pull request introduces improvements to the Connect protocol implementation, focusing on enhanced base64 handling, thread safety, default port configuration, and code readability. The most significant changes include the addition of utility functions for base64 encoding/decoding with padding, thread-safe caching in error handling, and the introduction of a default HTTP port constant.
Base64 Handling Enhancements:
src/connect/protocol_connect/base64_utils.py
: Added utility functions for base64 encoding/decoding, includingdecode_base64_with_padding
,decode_urlsafe_base64_with_padding
, andencode_base64_without_padding
, to handle padding and improve code reuse.src/connect/protocol_connect/connect_handler.py
: Replaced direct base64 decoding withdecode_urlsafe_base64_with_padding
for better readability and consistency.src/connect/protocol_connect/error_json.py
: Updated error serialization/deserialization to use the new base64 utility functions. [1] [2]Thread-Safe Caching:
src/connect/protocol_connect/error_json.py
: Implemented thread-safe initialization for the_string_to_code
cache using a double-checked locking pattern to ensure safe concurrent access. [1] [2]Default Port Configuration:
src/connect/protocol_connect/constants.py
: AddedDEFAULT_HTTP_PORT
constant for cleaner and centralized default port handling.src/connect/protocol_connect/connect_client.py
: Updated default port handling inAddress
initialization to useDEFAULT_HTTP_PORT
.Code Readability Improvements:
src/connect/protocol_connect/end_stream.py
: Refactored metadata and error handling for clarity by introducing intermediate variables and simplifying logic.src/connect/protocol_connect/marshaler.py
: Simplified URL query parameter updates by reusing encoded data in base64 operations.Miscellaneous Fixes:
src/connect/protocol_connect/connect_handler.py
: Corrected a typo in the module docstring ("ConnectHander" → "ConnectHandler").src/connect/protocol_connect/constants.py
: Improved user agent string formatting to include only the major and minor Python version for brevity.