feat: add enhanced API key validation functions #73
+1,084
−0
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
This PR enhances API key validation by providing comprehensive validation of actual Gradient client instances, addressing the need for runtime validation of authentication credentials before making API calls.
Problem
Currently, developers can create Gradient clients with invalid or missing API keys, only discovering issues when making actual API calls. This leads to:
Solution
Add three new validation functions that provide comprehensive client authentication validation:
validate_api_key()- Validates individual API key formatvalidate_client_credentials()- Validates credential combinations with detailed error messagesvalidate_client_instance()- Validates actual client instances (addresses reviewer feedback)Key Improvements
-Client Instance Validation: Validates actual Gradient/AsyncGradient objects, not just parameters
-Comprehensive Error Messages: Specific, actionable error messages for different failure scenarios
-URL Validation: Validates agent endpoint URLs are properly formatted
-Type Safety: Full type hints and proper error handling
-Backward Compatible: No breaking changes to existing APIs
Testing
Added comprehensive test suite covering:
-Valid and invalid API key formats
-Client credential validation scenarios
-Client instance validation
-Error message accuracy
-Edge cases and type checking
-All tests pass with 100% coverage of the new validation logic.
Usage Examples