Document upload with Stream support#143
Open
lyubobg wants to merge 5 commits intobraintree:masterfrom
Open
Conversation
Extend DocumentUpload.Create() and CreateAsync() to accept any Stream type (not just FileStream), enabling uploads from MemoryStream and other stream sources. This is useful for scenarios where files are already loaded in memory or come from non-file sources. Changes: - Add FileStream and FileName properties to DocumentUploadRequest - Implement new overloads for PostMultipart/PostMultipartAsync accepting Stream + fileName parameters - Add StreamWithName internal class to BraintreeService for stream handling - Update HttpService.GetMultipartFormData to handle StreamWithName via reflection - Modify validation message from "File must not be null" to "Either FileStream or File must not be null" - Add comprehensive unit and integration tests for Stream functionality When both File and FileStream are provided, FileStream takes precedence. Tests include: - Successful uploads with MemoryStream (sync and async) - Priority validation (FileStream over File) - Error handling for unsupported file types via Stream - Validation for null parameters
The previous name 'FileStream' was confusing because: - FileStream is a .NET system type - Having a property named FileStream alongside File property was ambiguous Renamed to ContentStream which more clearly indicates: - It accepts any Stream type (MemoryStream, FileStream, etc.) - It represents the content to upload, not specifically a file stream Changes: - Rename DocumentUploadRequest.FileStream to ContentStream - Update DocumentUploadGateway.cs to use ContentStream - Update validation message to reference ContentStream - Update all unit and integration tests All tests passing.
When handling StreamWithName, reset the stream position to 0 before copying if the stream is seekable. This prevents uploading empty or partial files when the caller passes a stream that has already been read from. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- Add changelog entry describing Stream support feature - Alphabetize properties in DocumentUploadRequest for consistency with Braintree SDK conventions (ContentStream, DocumentKind, File, FileName) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Refactored HttpService to use direct type checking and property access instead of reflection-based approach: - Added `using static Braintree.BraintreeService` for StreamWithName access - Replaced `param.Value?.GetType().Name == "StreamWithName"` with pattern matching: `param.Value is StreamWithName streamToUpload` - Removed reflection calls (GetProperty, GetValue) in favor of direct property access to `.Stream` and `.Name` - Improved performance by eliminating runtime reflection overhead - Enhanced type safety with compile-time checking - Cleaner, more readable code No functional changes - all tests passing. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
lyubobg, thank you for providing this PR. Just to let you know that this PR is prioritized for the next server SDK release. |
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
Checklist