Skip to content

Commit e0b007b

Browse files
committed
Adding HTTPBodyProcessingMode
1 parent ccc11f9 commit e0b007b

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

Sources/OpenAPIURLSession/URLSessionTransport.swift

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,41 @@ public struct URLSessionTransport: ClientTransport {
7070
/// - Parameter session: The URLSession used for performing HTTP operations.
7171
/// If none is provided, the system uses the shared URLSession.
7272
public init(session: URLSession = .shared) { self.init(session: session, implementation: .platformDefault) }
73+
74+
/// Specifies the mode in which HTTP request and response bodies are processed.
75+
public enum HTTPBodyProcessingMode {
76+
/// Processes the HTTP body incrementally as bytes become available.
77+
///
78+
/// Use this mode to handle large payloads efficiently or to begin processing
79+
/// before the entire body has been received. Will throw a `URLSessionTransportError.streamingNotSupported`
80+
/// error if not available on the platform.
81+
case streamed
7382

74-
public enum Implementation: Sendable {
83+
/// Waits until the entire HTTP body has been received before processing begins.
84+
///
85+
/// Use this mode when it's necessary or simpler to handle complete data payloads at once.
86+
case buffered
87+
}
88+
89+
public init(session: URLSession = .shared, httpBodyProcessingMode: HTTPBodyProcessingMode) {
90+
self.session = session
91+
switch httpBodyProcessingMode {
92+
93+
case .streamed:
94+
self.implementation = .defaultStreaming
95+
case .buffered:
96+
self.implementation = .buffering
97+
}
98+
}
99+
100+
enum Implementation: Sendable {
75101
case buffering
76102
case streaming(requestBodyStreamBufferSize: Int, responseBodyStreamWatermarks: (low: Int, high: Int))
77103
}
78104

79-
public var implementation: Implementation
105+
var implementation: Implementation
80106

81-
public init(session: URLSession = .shared, implementation: Implementation = .platformDefault) {
107+
init(session: URLSession = .shared, implementation: Implementation = .platformDefault) {
82108
self.session = session
83109
if case .streaming = implementation {
84110
precondition(Implementation.platformSupportsStreaming, "Streaming not supported on platform")
@@ -354,7 +380,11 @@ extension URLSessionTransport.Configuration.Implementation {
354380

355381
public static var platformDefault: Self {
356382
guard platformSupportsStreaming else { return .buffering }
357-
return .streaming(
383+
return .defaultStreaming
384+
}
385+
386+
static var defaultStreaming: Self {
387+
.streaming(
358388
requestBodyStreamBufferSize: 16 * 1024,
359389
responseBodyStreamWatermarks: (low: 16 * 1024, high: 32 * 1024)
360390
)

0 commit comments

Comments
 (0)