-
Notifications
You must be signed in to change notification settings - Fork 40
Add support for opting out of streaming HTTP bodies #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
ccc11f9
e0b007b
9fd926a
a3d13fb
d62e3cf
adb6c07
a1b1562
fb49a88
9df230c
beb7d45
2f280f0
57dfdeb
43fd210
5617c4d
9fd56c1
1a685fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,31 @@ public struct URLSessionTransport: ClientTransport { | |
/// - Parameter session: The URLSession used for performing HTTP operations. | ||
/// If none is provided, the system uses the shared URLSession. | ||
public init(session: URLSession = .shared) { self.init(session: session, implementation: .platformDefault) } | ||
/// Specifies the mode in which HTTP request and response bodies are processed. | ||
public enum HTTPBodyProcessingMode { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We generally do not use enums in public API because, right now, they are not extensible without breaking API. Instead for enum-like public API, we wrap an internal enum in a public struct with public static properties. |
||
/// Processes the HTTP body incrementally as bytes become available. | ||
/// | ||
/// Use this mode to handle large payloads efficiently or to begin processing | ||
/// before the entire body has been received. Will throw a `URLSessionTransportError.streamingNotSupported` | ||
/// error if not available on the platform. | ||
case streamed | ||
|
||
/// Waits until the entire HTTP body has been received before processing begins. | ||
/// | ||
/// Use this mode when it's necessary or simpler to handle complete data payloads at once. | ||
case buffered | ||
} | ||
/// Creates a new configuration with the provided session. | ||
/// - Parameters: | ||
/// - session: The URLSession used for performing HTTP operations. | ||
/// - httpBodyProcessingMode: The mode used to process HTTP request and response bodies. | ||
public init(session: URLSession = .shared, httpBodyProcessingMode: HTTPBodyProcessingMode) { | ||
hactar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.session = session | ||
switch httpBodyProcessingMode { | ||
case .streamed: self.implementation = .defaultStreaming | ||
case .buffered: self.implementation = .buffering | ||
} | ||
} | ||
|
||
enum Implementation { | ||
case buffering | ||
|
@@ -354,7 +379,10 @@ extension URLSessionTransport.Configuration.Implementation { | |
|
||
static var platformDefault: Self { | ||
guard platformSupportsStreaming else { return .buffering } | ||
return .streaming( | ||
return .defaultStreaming | ||
} | ||
static var defaultStreaming: Self { | ||
.streaming( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part of the patch seems unnecessary now? |
||
requestBodyStreamBufferSize: 16 * 1024, | ||
responseBodyStreamWatermarks: (low: 16 * 1024, high: 32 * 1024) | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding new parameters to existing functions is an API break, as caught by CI.
We'd need to preserve this symbol that calls through to the new one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand the issue.
The new API provides
public init(session: URLSession = .shared, httpBodyProcessingMode: HTTPBodyProcessingMode = .platformDefault)
- and as there is a default parameter for httpBodyProcessingMode, doesn't Swift auto generate apublic init(session: URLSession)
for us?The following code in my app compiles fine:
So to me, there still is an (implicit)
URLSessionTransport.Configuration.init(session:)
- isn't this API breakage error a false positive?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a false positive. It's possible to refer to the function itself (e.g.
let initializer = URLSessionTransport.Configuration.init(session:)
) which won't work if the function is no longer present.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation. Interesting, I wasn't aware of this limitation of default parameter values. I have added back the old single parameter init.