@@ -31,8 +31,8 @@ public final class FileDownloadDelegate: HTTPClientResponseDelegate {
31
31
32
32
private let filePath : String
33
33
private( set) var fileIOThreadPool : NIOThreadPool ?
34
- private let reportHead : ( ( HTTPResponseHead ) -> Void ) ?
35
- private let reportProgress : ( ( Progress ) -> Void ) ?
34
+ private let reportHead : ( ( HTTPClient . Task < Progress > , HTTPResponseHead ) -> Void ) ?
35
+ private let reportProgress : ( ( HTTPClient . Task < Progress > , Progress ) -> Void ) ?
36
36
37
37
private var fileHandleFuture : EventLoopFuture < NIOFileHandle > ?
38
38
private var writeFuture : EventLoopFuture < Void > ?
@@ -41,63 +41,99 @@ public final class FileDownloadDelegate: HTTPClientResponseDelegate {
41
41
///
42
42
/// - parameters:
43
43
/// - path: Path to a file you'd like to write the download to.
44
- /// - pool: A thread pool to use for asynchronous file I/O.
44
+ /// - pool: A thread pool to use for asynchronous file I/O. If nil, a shared thread pool will be used. Defaults to nil.
45
45
/// - reportHead: A closure called when the response head is available.
46
46
/// - reportProgress: A closure called when a body chunk has been downloaded, with
47
47
/// the total byte count and download byte count passed to it as arguments. The callbacks
48
48
/// will be invoked in the same threading context that the delegate itself is invoked,
49
49
/// as controlled by `EventLoopPreference`.
50
- public convenience init (
50
+ public init (
51
51
path: String ,
52
- pool: NIOThreadPool ,
53
- reportHead: ( ( HTTPResponseHead ) -> Void ) ? = nil ,
54
- reportProgress: ( ( Progress ) -> Void ) ? = nil
52
+ pool: NIOThreadPool ? = nil ,
53
+ reportHead: ( ( HTTPClient . Task < Response > , HTTPResponseHead ) -> Void ) ? = nil ,
54
+ reportProgress: ( ( HTTPClient . Task < Response > , Progress ) -> Void ) ? = nil
55
55
) throws {
56
- try self . init ( path: path, pool: . some( pool) , reportHead: reportHead, reportProgress: reportProgress)
56
+ if let pool = pool {
57
+ self . fileIOThreadPool = pool
58
+ } else {
59
+ // we should use the shared thread pool from the HTTPClient which
60
+ // we will get from the `HTTPClient.Task`
61
+ self . fileIOThreadPool = nil
62
+ }
63
+
64
+ self . filePath = path
65
+
66
+ self . reportHead = reportHead
67
+ self . reportProgress = reportProgress
57
68
}
58
69
59
- /// Initializes a new file download delegate and uses the shared thread pool of the ``HTTPClient`` for file I/O .
70
+ /// Initializes a new file download delegate.
60
71
///
61
72
/// - parameters:
62
73
/// - path: Path to a file you'd like to write the download to.
74
+ /// - pool: A thread pool to use for asynchronous file I/O.
63
75
/// - reportHead: A closure called when the response head is available.
64
76
/// - reportProgress: A closure called when a body chunk has been downloaded, with
65
77
/// the total byte count and download byte count passed to it as arguments. The callbacks
66
78
/// will be invoked in the same threading context that the delegate itself is invoked,
67
79
/// as controlled by `EventLoopPreference`.
68
80
public convenience init (
69
81
path: String ,
82
+ pool: NIOThreadPool ,
70
83
reportHead: ( ( HTTPResponseHead ) -> Void ) ? = nil ,
71
84
reportProgress: ( ( Progress ) -> Void ) ? = nil
72
85
) throws {
73
- try self . init ( path: path, pool: nil , reportHead: reportHead, reportProgress: reportProgress)
86
+ try self . init (
87
+ path: path,
88
+ pool: . some( pool) ,
89
+ reportHead: reportHead. map { reportHead in
90
+ return { _, head in
91
+ reportHead ( head)
92
+ }
93
+ } ,
94
+ reportProgress: reportProgress. map { reportProgress in
95
+ return { _, head in
96
+ reportProgress ( head)
97
+ }
98
+ }
99
+ )
74
100
}
75
101
76
- private init (
102
+ /// Initializes a new file download delegate and uses the shared thread pool of the ``HTTPClient`` for file I/O.
103
+ ///
104
+ /// - parameters:
105
+ /// - path: Path to a file you'd like to write the download to.
106
+ /// - reportHead: A closure called when the response head is available.
107
+ /// - reportProgress: A closure called when a body chunk has been downloaded, with
108
+ /// the total byte count and download byte count passed to it as arguments. The callbacks
109
+ /// will be invoked in the same threading context that the delegate itself is invoked,
110
+ /// as controlled by `EventLoopPreference`.
111
+ public convenience init (
77
112
path: String ,
78
- pool: NIOThreadPool ? ,
79
113
reportHead: ( ( HTTPResponseHead ) -> Void ) ? = nil ,
80
114
reportProgress: ( ( Progress ) -> Void ) ? = nil
81
115
) throws {
82
- if let pool = pool {
83
- self . fileIOThreadPool = pool
84
- } else {
85
- // we should use the shared thread pool from the HTTPClient which
86
- // we will get from the `HTTPClient.Task`
87
- self . fileIOThreadPool = nil
88
- }
89
-
90
- self . filePath = path
91
-
92
- self . reportHead = reportHead
93
- self . reportProgress = reportProgress
116
+ try self . init (
117
+ path: path,
118
+ pool: nil ,
119
+ reportHead: reportHead. map { reportHead in
120
+ return { _, head in
121
+ reportHead ( head)
122
+ }
123
+ } ,
124
+ reportProgress: reportProgress. map { reportProgress in
125
+ return { _, head in
126
+ reportProgress ( head)
127
+ }
128
+ }
129
+ )
94
130
}
95
131
96
132
public func didReceiveHead(
97
133
task: HTTPClient . Task < Response > ,
98
134
_ head: HTTPResponseHead
99
135
) -> EventLoopFuture < Void > {
100
- self . reportHead ? ( head)
136
+ self . reportHead ? ( task , head)
101
137
102
138
if let totalBytesString = head. headers. first ( name: " Content-Length " ) ,
103
139
let totalBytes = Int ( totalBytesString) {
@@ -121,7 +157,7 @@ public final class FileDownloadDelegate: HTTPClientResponseDelegate {
121
157
} ( )
122
158
let io = NonBlockingFileIO ( threadPool: threadPool)
123
159
self . progress. receivedBytes += buffer. readableBytes
124
- self . reportProgress ? ( self . progress)
160
+ self . reportProgress ? ( task , self . progress)
125
161
126
162
let writeFuture : EventLoopFuture < Void >
127
163
if let fileHandleFuture = self . fileHandleFuture {
0 commit comments