|
5 | 5 | import Foundation
|
6 | 6 |
|
7 | 7 | public protocol URLSessionProtocol {
|
8 |
| - var sessionDescription: String? { get set } |
9 |
| - |
10 |
| - func finishTasksAndInvalidate() |
11 |
| - |
12 |
| - /// Cancels all outstanding tasks and then invalidates the session. |
13 |
| - /// |
14 |
| - /// Once invalidated, references to the delegate and callback objects are broken. After invalidation, session objects cannot be reused. To allow outstanding tasks to run until completion, call finishTasksAndInvalidate() instead. |
15 |
| - func invalidateAndCancel() |
| 8 | + // MARK: - Core |
16 | 9 |
|
17 | 10 | func dataTask(with request: URLRequest) -> URLSessionDataTask
|
18 | 11 |
|
@@ -101,32 +94,6 @@ public protocol URLSessionProtocol {
|
101 | 94 |
|
102 | 95 | // MARK: - Swift Concurrency
|
103 | 96 |
|
104 |
| - /// Convenience method to load data using a URLRequest, creates and resumes a URLSessionDataTask internally. |
105 |
| - /// |
106 |
| - /// - Parameter request: The URLRequest for which to load data. |
107 |
| - /// - Returns: Data and response. |
108 |
| - func data(for request: URLRequest) async throws -> (Data, URLResponse) |
109 |
| - |
110 |
| - /// Convenience method to load data using a URL, creates and resumes a URLSessionDataTask internally. |
111 |
| - /// |
112 |
| - /// - Parameter url: The URL for which to load data. |
113 |
| - /// - Returns: Data and response. |
114 |
| - func data(from url: URL) async throws -> (Data, URLResponse) |
115 |
| - |
116 |
| - /// Convenience method to upload data using a URLRequest, creates and resumes a URLSessionUploadTask internally. |
117 |
| - /// |
118 |
| - /// - Parameter request: The URLRequest for which to upload data. |
119 |
| - /// - Parameter fileURL: File to upload. |
120 |
| - /// - Returns: Data and response. |
121 |
| - func upload(for request: URLRequest, fromFile fileURL: URL) async throws -> (Data, URLResponse) |
122 |
| - |
123 |
| - /// Convenience method to upload data using a URLRequest, creates and resumes a URLSessionUploadTask internally. |
124 |
| - /// |
125 |
| - /// - Parameter request: The URLRequest for which to upload data. |
126 |
| - /// - Parameter bodyData: Data to upload. |
127 |
| - /// - Returns: Data and response. |
128 |
| - func upload(for request: URLRequest, from bodyData: Data) async throws -> (Data, URLResponse) |
129 |
| - |
130 | 97 | /// Convenience method to load data using a URLRequest, creates and resumes a URLSessionDataTask internally.
|
131 | 98 | ///
|
132 | 99 | /// - Parameter request: The URLRequest for which to load data.
|
@@ -193,4 +160,44 @@ public protocol URLSessionProtocol {
|
193 | 160 | func bytes(from url: URL, delegate: (any URLSessionTaskDelegate)?) async throws -> (URLSession.AsyncBytes, URLResponse)
|
194 | 161 | }
|
195 | 162 |
|
| 163 | +public extension URLSessionProtocol { |
| 164 | + /// Convenience method to load data using a URLRequest, creates and resumes a URLSessionDataTask internally. |
| 165 | + /// |
| 166 | + /// - Parameter request: The URLRequest for which to load data. |
| 167 | + /// - Returns: Data and response. |
| 168 | + func data(for request: URLRequest) async throws -> (Data, URLResponse) { |
| 169 | + try await data(for: request, delegate: nil) |
| 170 | + } |
| 171 | + |
| 172 | + /// Convenience method to load data using a URL, creates and resumes a URLSessionDataTask internally. |
| 173 | + /// |
| 174 | + /// - Parameter url: The URL for which to load data. |
| 175 | + /// - Returns: Data and response. |
| 176 | + func data(from url: URL) async throws -> (Data, URLResponse) { |
| 177 | + try await data(from: url, delegate: nil) |
| 178 | + } |
| 179 | + |
| 180 | + /// Convenience method to upload data using a URLRequest, creates and resumes a URLSessionUploadTask internally. |
| 181 | + /// |
| 182 | + /// - Parameter request: The URLRequest for which to upload data. |
| 183 | + /// - Parameter fileURL: File to upload. |
| 184 | + /// - Returns: Data and response. |
| 185 | + func upload(for request: URLRequest, fromFile fileURL: URL) async throws -> (Data, URLResponse) { |
| 186 | + try await upload(for: request, fromFile: fileURL, delegate: nil) |
| 187 | + } |
| 188 | + |
| 189 | + /// Convenience method to upload data using a URLRequest, creates and resumes a URLSessionUploadTask internally. |
| 190 | + /// |
| 191 | + /// - Parameter request: The URLRequest for which to upload data. |
| 192 | + /// - Parameter bodyData: Data to upload. |
| 193 | + /// - Returns: Data and response. |
| 194 | + func upload(for request: URLRequest, from bodyData: Data) async throws -> (Data, URLResponse) { |
| 195 | + try await upload(for: request, from: bodyData, delegate: nil) |
| 196 | + } |
| 197 | + |
| 198 | + func bytes(from url: URL) async throws -> (URLSession.AsyncBytes, URLResponse) { |
| 199 | + try await bytes(from: url, delegate: nil) |
| 200 | + } |
| 201 | +} |
| 202 | + |
196 | 203 | extension URLSession: URLSessionProtocol {}
|
0 commit comments