Skip to content

Conversation

@AndreyLebedev345
Copy link
Owner

This commit adds two new convenience methods to the Response class:

  1. 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.

  2. save_to_file(path, mode='auto'): Saves response content to a file with intelligent handling of different content types. Supports multiple modes:

    • 'auto': Automatically detects format based on Content-Type header
    • 'binary': Saves raw bytes (images, PDFs, etc.)
    • 'text': Saves as text with encoding support
    • 'json': Parses and saves formatted JSON with indentation

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

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

AndreyLebedev345 and others added 2 commits October 8, 2025 12:46
This commit adds two new convenience methods to the Response class:

1. 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.

2. save_to_file(path, mode='auto'): Saves response content to a file with
   intelligent handling of different content types. Supports multiple modes:
   - 'auto': Automatically detects format based on Content-Type header
   - 'binary': Saves raw bytes (images, PDFs, etc.)
   - 'text': Saves as text with encoding support
   - 'json': Parses and saves formatted JSON with indentation

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](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Auto-generated documentation update for PR #12

Docs-Updated-By: docs-bot
@AndreyLebedev345
Copy link
Owner Author

📚 Documentation Updates

Auto-committed:

🤖 Docs Bot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants