Add json_or_text and save_to_file methods to Response class #12
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 commit adds two new convenience methods to the Response class:
json_or_text(): Attempts to parse the response as JSON, automatically falling back to text content if JSON parsing fails. This provides a convenient way to handle responses that may be in either format without explicit exception handling.
save_to_file(path, mode='auto'): Saves response content to a file with intelligent handling of different content types. Supports multiple modes:
These methods enhance the Response API by providing commonly needed functionality that previously required manual implementation. The save_to_file method is particularly useful for downloading and storing various types of content from web APIs and servers.
🤖 Generated with Claude Code
Summary
Checklist