pod-builder 1.9.4 → 2.0.0.beta.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +9 -0
- data/Example/Frameworks/.gitignore +6 -0
- data/Example/Frameworks/.pod_builder/pod_builder +0 -0
- data/Example/Frameworks/PodBuilder.json +10 -4
- data/Example/Frameworks/Podfile +23 -0
- data/Example/Frameworks/Podfile.restore +40 -0
- data/Example/PodBuilderExample/AppDelegate.swift +4 -0
- data/Example/PodBuilderExample.xcodeproj/project.pbxproj +3 -8
- data/Example/{PodBuilderExample.xcodeproj/xcuserdata/tomas.xcuserdatad/xcschemes/xcschememanagement.plist → PodBuilderExample.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist} +2 -8
- data/Example/Podfile +44 -1
- data/Example/Podfile.lock +426 -7
- data/Example/Pods-acknowledgements.md +210 -0
- data/Example/Pods-acknowledgements.plist +206 -0
- data/README.md +38 -1
- data/exe/pod_builder +14 -14
- data/lib/pod_builder/analyze.rb +32 -7
- data/lib/pod_builder/analyzer.rb +16 -0
- data/lib/pod_builder/command/build.rb +110 -109
- data/lib/pod_builder/command/clean.rb +9 -11
- data/lib/pod_builder/command/clear_lldbinit.rb +1 -1
- data/lib/pod_builder/command/deintegrate.rb +2 -1
- data/lib/pod_builder/command/generate_lfs.rb +2 -2
- data/lib/pod_builder/command/install_sources.rb +1 -1
- data/lib/pod_builder/command/switch.rb +99 -99
- data/lib/pod_builder/command/sync_podfile.rb +2 -1
- data/lib/pod_builder/command/update.rb +1 -1
- data/lib/pod_builder/command/update_lldbinit.rb +2 -2
- data/lib/pod_builder/configuration.rb +61 -7
- data/lib/pod_builder/core.rb +60 -5
- data/lib/pod_builder/info.rb +24 -90
- data/lib/pod_builder/install.rb +148 -82
- data/lib/pod_builder/podfile/post_actions.rb +0 -1
- data/lib/pod_builder/podfile.rb +83 -14
- data/lib/pod_builder/podfile_item.rb +152 -74
- data/lib/pod_builder/podspec.rb +125 -133
- data/lib/pod_builder/rome/post_install.rb +248 -0
- data/lib/pod_builder/rome/pre_install.rb +6 -0
- data/lib/pod_builder/templates/build_podfile.template +1 -1
- data/lib/pod_builder/version.rb +1 -1
- data/pod-builder.gemspec +3 -3
- metadata +28 -60
- data/Example/Pods/Alamofire/LICENSE +0 -19
- data/Example/Pods/Alamofire/README.md +0 -242
- data/Example/Pods/Alamofire/Source/AFError.swift +0 -460
- data/Example/Pods/Alamofire/Source/Alamofire.swift +0 -465
- data/Example/Pods/Alamofire/Source/DispatchQueue+Alamofire.swift +0 -37
- data/Example/Pods/Alamofire/Source/MultipartFormData.swift +0 -580
- data/Example/Pods/Alamofire/Source/NetworkReachabilityManager.swift +0 -233
- data/Example/Pods/Alamofire/Source/Notifications.swift +0 -55
- data/Example/Pods/Alamofire/Source/ParameterEncoding.swift +0 -483
- data/Example/Pods/Alamofire/Source/Request.swift +0 -654
- data/Example/Pods/Alamofire/Source/Response.swift +0 -567
- data/Example/Pods/Alamofire/Source/ResponseSerialization.swift +0 -715
- data/Example/Pods/Alamofire/Source/Result.swift +0 -300
- data/Example/Pods/Alamofire/Source/ServerTrustPolicy.swift +0 -307
- data/Example/Pods/Alamofire/Source/SessionDelegate.swift +0 -725
- data/Example/Pods/Alamofire/Source/SessionManager.swift +0 -896
- data/Example/Pods/Alamofire/Source/TaskDelegate.swift +0 -466
- data/Example/Pods/Alamofire/Source/Timeline.swift +0 -136
- data/Example/Pods/Alamofire/Source/Validation.swift +0 -315
- data/Example/Pods/Manifest.lock +0 -16
- data/Example/Pods/Pods.xcodeproj/project.pbxproj +0 -673
- data/Example/Pods/Pods.xcodeproj/xcuserdata/tomas.xcuserdatad/xcschemes/Alamofire.xcscheme +0 -60
- data/Example/Pods/Pods.xcodeproj/xcuserdata/tomas.xcuserdatad/xcschemes/Pods-PodBuilderExample.xcscheme +0 -60
- data/Example/Pods/Pods.xcodeproj/xcuserdata/tomas.xcuserdatad/xcschemes/xcschememanagement.plist +0 -21
- data/Example/Pods/Target Support Files/Alamofire/Alamofire-dummy.m +0 -5
- data/Example/Pods/Target Support Files/Alamofire/Alamofire-prefix.pch +0 -12
- data/Example/Pods/Target Support Files/Alamofire/Alamofire-umbrella.h +0 -16
- data/Example/Pods/Target Support Files/Alamofire/Alamofire.modulemap +0 -6
- data/Example/Pods/Target Support Files/Alamofire/Alamofire.xcconfig +0 -9
- data/Example/Pods/Target Support Files/Alamofire/Info.plist +0 -26
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Info.plist +0 -26
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample-acknowledgements.markdown +0 -26
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample-acknowledgements.plist +0 -58
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample-dummy.m +0 -5
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample-frameworks.sh +0 -153
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample-resources.sh +0 -118
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample-umbrella.h +0 -16
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample.debug.xcconfig +0 -11
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample.modulemap +0 -6
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample.release.xcconfig +0 -11
- data/lib/pod_builder/cocoapods/specification.rb +0 -27
@@ -1,654 +0,0 @@
|
|
1
|
-
//
|
2
|
-
// Request.swift
|
3
|
-
//
|
4
|
-
// Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
|
5
|
-
//
|
6
|
-
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
-
// of this software and associated documentation files (the "Software"), to deal
|
8
|
-
// in the Software without restriction, including without limitation the rights
|
9
|
-
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
-
// copies of the Software, and to permit persons to whom the Software is
|
11
|
-
// furnished to do so, subject to the following conditions:
|
12
|
-
//
|
13
|
-
// The above copyright notice and this permission notice shall be included in
|
14
|
-
// all copies or substantial portions of the Software.
|
15
|
-
//
|
16
|
-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
-
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
-
// THE SOFTWARE.
|
23
|
-
//
|
24
|
-
|
25
|
-
import Foundation
|
26
|
-
|
27
|
-
/// A type that can inspect and optionally adapt a `URLRequest` in some manner if necessary.
|
28
|
-
public protocol RequestAdapter {
|
29
|
-
/// Inspects and adapts the specified `URLRequest` in some manner if necessary and returns the result.
|
30
|
-
///
|
31
|
-
/// - parameter urlRequest: The URL request to adapt.
|
32
|
-
///
|
33
|
-
/// - throws: An `Error` if the adaptation encounters an error.
|
34
|
-
///
|
35
|
-
/// - returns: The adapted `URLRequest`.
|
36
|
-
func adapt(_ urlRequest: URLRequest) throws -> URLRequest
|
37
|
-
}
|
38
|
-
|
39
|
-
// MARK: -
|
40
|
-
|
41
|
-
/// A closure executed when the `RequestRetrier` determines whether a `Request` should be retried or not.
|
42
|
-
public typealias RequestRetryCompletion = (_ shouldRetry: Bool, _ timeDelay: TimeInterval) -> Void
|
43
|
-
|
44
|
-
/// A type that determines whether a request should be retried after being executed by the specified session manager
|
45
|
-
/// and encountering an error.
|
46
|
-
public protocol RequestRetrier {
|
47
|
-
/// Determines whether the `Request` should be retried by calling the `completion` closure.
|
48
|
-
///
|
49
|
-
/// This operation is fully asynchronous. Any amount of time can be taken to determine whether the request needs
|
50
|
-
/// to be retried. The one requirement is that the completion closure is called to ensure the request is properly
|
51
|
-
/// cleaned up after.
|
52
|
-
///
|
53
|
-
/// - parameter manager: The session manager the request was executed on.
|
54
|
-
/// - parameter request: The request that failed due to the encountered error.
|
55
|
-
/// - parameter error: The error encountered when executing the request.
|
56
|
-
/// - parameter completion: The completion closure to be executed when retry decision has been determined.
|
57
|
-
func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion)
|
58
|
-
}
|
59
|
-
|
60
|
-
// MARK: -
|
61
|
-
|
62
|
-
protocol TaskConvertible {
|
63
|
-
func task(session: URLSession, adapter: RequestAdapter?, queue: DispatchQueue) throws -> URLSessionTask
|
64
|
-
}
|
65
|
-
|
66
|
-
/// A dictionary of headers to apply to a `URLRequest`.
|
67
|
-
public typealias HTTPHeaders = [String: String]
|
68
|
-
|
69
|
-
// MARK: -
|
70
|
-
|
71
|
-
/// Responsible for sending a request and receiving the response and associated data from the server, as well as
|
72
|
-
/// managing its underlying `URLSessionTask`.
|
73
|
-
open class Request {
|
74
|
-
|
75
|
-
// MARK: Helper Types
|
76
|
-
|
77
|
-
/// A closure executed when monitoring upload or download progress of a request.
|
78
|
-
public typealias ProgressHandler = (Progress) -> Void
|
79
|
-
|
80
|
-
enum RequestTask {
|
81
|
-
case data(TaskConvertible?, URLSessionTask?)
|
82
|
-
case download(TaskConvertible?, URLSessionTask?)
|
83
|
-
case upload(TaskConvertible?, URLSessionTask?)
|
84
|
-
case stream(TaskConvertible?, URLSessionTask?)
|
85
|
-
}
|
86
|
-
|
87
|
-
// MARK: Properties
|
88
|
-
|
89
|
-
/// The delegate for the underlying task.
|
90
|
-
open internal(set) var delegate: TaskDelegate {
|
91
|
-
get {
|
92
|
-
taskDelegateLock.lock() ; defer { taskDelegateLock.unlock() }
|
93
|
-
return taskDelegate
|
94
|
-
}
|
95
|
-
set {
|
96
|
-
taskDelegateLock.lock() ; defer { taskDelegateLock.unlock() }
|
97
|
-
taskDelegate = newValue
|
98
|
-
}
|
99
|
-
}
|
100
|
-
|
101
|
-
/// The underlying task.
|
102
|
-
open var task: URLSessionTask? { return delegate.task }
|
103
|
-
|
104
|
-
/// The session belonging to the underlying task.
|
105
|
-
public let session: URLSession
|
106
|
-
|
107
|
-
/// The request sent or to be sent to the server.
|
108
|
-
open var request: URLRequest? { return task?.originalRequest }
|
109
|
-
|
110
|
-
/// The response received from the server, if any.
|
111
|
-
open var response: HTTPURLResponse? { return task?.response as? HTTPURLResponse }
|
112
|
-
|
113
|
-
/// The number of times the request has been retried.
|
114
|
-
open internal(set) var retryCount: UInt = 0
|
115
|
-
|
116
|
-
let originalTask: TaskConvertible?
|
117
|
-
|
118
|
-
var startTime: CFAbsoluteTime?
|
119
|
-
var endTime: CFAbsoluteTime?
|
120
|
-
|
121
|
-
var validations: [() -> Void] = []
|
122
|
-
|
123
|
-
private var taskDelegate: TaskDelegate
|
124
|
-
private var taskDelegateLock = NSLock()
|
125
|
-
|
126
|
-
// MARK: Lifecycle
|
127
|
-
|
128
|
-
init(session: URLSession, requestTask: RequestTask, error: Error? = nil) {
|
129
|
-
self.session = session
|
130
|
-
|
131
|
-
switch requestTask {
|
132
|
-
case .data(let originalTask, let task):
|
133
|
-
taskDelegate = DataTaskDelegate(task: task)
|
134
|
-
self.originalTask = originalTask
|
135
|
-
case .download(let originalTask, let task):
|
136
|
-
taskDelegate = DownloadTaskDelegate(task: task)
|
137
|
-
self.originalTask = originalTask
|
138
|
-
case .upload(let originalTask, let task):
|
139
|
-
taskDelegate = UploadTaskDelegate(task: task)
|
140
|
-
self.originalTask = originalTask
|
141
|
-
case .stream(let originalTask, let task):
|
142
|
-
taskDelegate = TaskDelegate(task: task)
|
143
|
-
self.originalTask = originalTask
|
144
|
-
}
|
145
|
-
|
146
|
-
delegate.error = error
|
147
|
-
delegate.queue.addOperation { self.endTime = CFAbsoluteTimeGetCurrent() }
|
148
|
-
}
|
149
|
-
|
150
|
-
// MARK: Authentication
|
151
|
-
|
152
|
-
/// Associates an HTTP Basic credential with the request.
|
153
|
-
///
|
154
|
-
/// - parameter user: The user.
|
155
|
-
/// - parameter password: The password.
|
156
|
-
/// - parameter persistence: The URL credential persistence. `.ForSession` by default.
|
157
|
-
///
|
158
|
-
/// - returns: The request.
|
159
|
-
@discardableResult
|
160
|
-
open func authenticate(
|
161
|
-
user: String,
|
162
|
-
password: String,
|
163
|
-
persistence: URLCredential.Persistence = .forSession)
|
164
|
-
-> Self
|
165
|
-
{
|
166
|
-
let credential = URLCredential(user: user, password: password, persistence: persistence)
|
167
|
-
return authenticate(usingCredential: credential)
|
168
|
-
}
|
169
|
-
|
170
|
-
/// Associates a specified credential with the request.
|
171
|
-
///
|
172
|
-
/// - parameter credential: The credential.
|
173
|
-
///
|
174
|
-
/// - returns: The request.
|
175
|
-
@discardableResult
|
176
|
-
open func authenticate(usingCredential credential: URLCredential) -> Self {
|
177
|
-
delegate.credential = credential
|
178
|
-
return self
|
179
|
-
}
|
180
|
-
|
181
|
-
/// Returns a base64 encoded basic authentication credential as an authorization header tuple.
|
182
|
-
///
|
183
|
-
/// - parameter user: The user.
|
184
|
-
/// - parameter password: The password.
|
185
|
-
///
|
186
|
-
/// - returns: A tuple with Authorization header and credential value if encoding succeeds, `nil` otherwise.
|
187
|
-
open class func authorizationHeader(user: String, password: String) -> (key: String, value: String)? {
|
188
|
-
guard let data = "\(user):\(password)".data(using: .utf8) else { return nil }
|
189
|
-
|
190
|
-
let credential = data.base64EncodedString(options: [])
|
191
|
-
|
192
|
-
return (key: "Authorization", value: "Basic \(credential)")
|
193
|
-
}
|
194
|
-
|
195
|
-
// MARK: State
|
196
|
-
|
197
|
-
/// Resumes the request.
|
198
|
-
open func resume() {
|
199
|
-
guard let task = task else { delegate.queue.isSuspended = false ; return }
|
200
|
-
|
201
|
-
if startTime == nil { startTime = CFAbsoluteTimeGetCurrent() }
|
202
|
-
|
203
|
-
task.resume()
|
204
|
-
|
205
|
-
NotificationCenter.default.post(
|
206
|
-
name: Notification.Name.Task.DidResume,
|
207
|
-
object: self,
|
208
|
-
userInfo: [Notification.Key.Task: task]
|
209
|
-
)
|
210
|
-
}
|
211
|
-
|
212
|
-
/// Suspends the request.
|
213
|
-
open func suspend() {
|
214
|
-
guard let task = task else { return }
|
215
|
-
|
216
|
-
task.suspend()
|
217
|
-
|
218
|
-
NotificationCenter.default.post(
|
219
|
-
name: Notification.Name.Task.DidSuspend,
|
220
|
-
object: self,
|
221
|
-
userInfo: [Notification.Key.Task: task]
|
222
|
-
)
|
223
|
-
}
|
224
|
-
|
225
|
-
/// Cancels the request.
|
226
|
-
open func cancel() {
|
227
|
-
guard let task = task else { return }
|
228
|
-
|
229
|
-
task.cancel()
|
230
|
-
|
231
|
-
NotificationCenter.default.post(
|
232
|
-
name: Notification.Name.Task.DidCancel,
|
233
|
-
object: self,
|
234
|
-
userInfo: [Notification.Key.Task: task]
|
235
|
-
)
|
236
|
-
}
|
237
|
-
}
|
238
|
-
|
239
|
-
// MARK: - CustomStringConvertible
|
240
|
-
|
241
|
-
extension Request: CustomStringConvertible {
|
242
|
-
/// The textual representation used when written to an output stream, which includes the HTTP method and URL, as
|
243
|
-
/// well as the response status code if a response has been received.
|
244
|
-
open var description: String {
|
245
|
-
var components: [String] = []
|
246
|
-
|
247
|
-
if let HTTPMethod = request?.httpMethod {
|
248
|
-
components.append(HTTPMethod)
|
249
|
-
}
|
250
|
-
|
251
|
-
if let urlString = request?.url?.absoluteString {
|
252
|
-
components.append(urlString)
|
253
|
-
}
|
254
|
-
|
255
|
-
if let response = response {
|
256
|
-
components.append("(\(response.statusCode))")
|
257
|
-
}
|
258
|
-
|
259
|
-
return components.joined(separator: " ")
|
260
|
-
}
|
261
|
-
}
|
262
|
-
|
263
|
-
// MARK: - CustomDebugStringConvertible
|
264
|
-
|
265
|
-
extension Request: CustomDebugStringConvertible {
|
266
|
-
/// The textual representation used when written to an output stream, in the form of a cURL command.
|
267
|
-
open var debugDescription: String {
|
268
|
-
return cURLRepresentation()
|
269
|
-
}
|
270
|
-
|
271
|
-
func cURLRepresentation() -> String {
|
272
|
-
var components = ["$ curl -v"]
|
273
|
-
|
274
|
-
guard let request = self.request,
|
275
|
-
let url = request.url,
|
276
|
-
let host = url.host
|
277
|
-
else {
|
278
|
-
return "$ curl command could not be created"
|
279
|
-
}
|
280
|
-
|
281
|
-
if let httpMethod = request.httpMethod, httpMethod != "GET" {
|
282
|
-
components.append("-X \(httpMethod)")
|
283
|
-
}
|
284
|
-
|
285
|
-
if let credentialStorage = self.session.configuration.urlCredentialStorage {
|
286
|
-
let protectionSpace = URLProtectionSpace(
|
287
|
-
host: host,
|
288
|
-
port: url.port ?? 0,
|
289
|
-
protocol: url.scheme,
|
290
|
-
realm: host,
|
291
|
-
authenticationMethod: NSURLAuthenticationMethodHTTPBasic
|
292
|
-
)
|
293
|
-
|
294
|
-
if let credentials = credentialStorage.credentials(for: protectionSpace)?.values {
|
295
|
-
for credential in credentials {
|
296
|
-
guard let user = credential.user, let password = credential.password else { continue }
|
297
|
-
components.append("-u \(user):\(password)")
|
298
|
-
}
|
299
|
-
} else {
|
300
|
-
if let credential = delegate.credential, let user = credential.user, let password = credential.password {
|
301
|
-
components.append("-u \(user):\(password)")
|
302
|
-
}
|
303
|
-
}
|
304
|
-
}
|
305
|
-
|
306
|
-
if session.configuration.httpShouldSetCookies {
|
307
|
-
if
|
308
|
-
let cookieStorage = session.configuration.httpCookieStorage,
|
309
|
-
let cookies = cookieStorage.cookies(for: url), !cookies.isEmpty
|
310
|
-
{
|
311
|
-
let string = cookies.reduce("") { $0 + "\($1.name)=\($1.value);" }
|
312
|
-
|
313
|
-
#if swift(>=3.2)
|
314
|
-
components.append("-b \"\(string[..<string.index(before: string.endIndex)])\"")
|
315
|
-
#else
|
316
|
-
components.append("-b \"\(string.substring(to: string.characters.index(before: string.endIndex)))\"")
|
317
|
-
#endif
|
318
|
-
}
|
319
|
-
}
|
320
|
-
|
321
|
-
var headers: [AnyHashable: Any] = [:]
|
322
|
-
|
323
|
-
if let additionalHeaders = session.configuration.httpAdditionalHeaders {
|
324
|
-
for (field, value) in additionalHeaders where field != AnyHashable("Cookie") {
|
325
|
-
headers[field] = value
|
326
|
-
}
|
327
|
-
}
|
328
|
-
|
329
|
-
if let headerFields = request.allHTTPHeaderFields {
|
330
|
-
for (field, value) in headerFields where field != "Cookie" {
|
331
|
-
headers[field] = value
|
332
|
-
}
|
333
|
-
}
|
334
|
-
|
335
|
-
for (field, value) in headers {
|
336
|
-
let escapedValue = String(describing: value).replacingOccurrences(of: "\"", with: "\\\"")
|
337
|
-
components.append("-H \"\(field): \(escapedValue)\"")
|
338
|
-
}
|
339
|
-
|
340
|
-
if let httpBodyData = request.httpBody, let httpBody = String(data: httpBodyData, encoding: .utf8) {
|
341
|
-
var escapedBody = httpBody.replacingOccurrences(of: "\\\"", with: "\\\\\"")
|
342
|
-
escapedBody = escapedBody.replacingOccurrences(of: "\"", with: "\\\"")
|
343
|
-
|
344
|
-
components.append("-d \"\(escapedBody)\"")
|
345
|
-
}
|
346
|
-
|
347
|
-
components.append("\"\(url.absoluteString)\"")
|
348
|
-
|
349
|
-
return components.joined(separator: " \\\n\t")
|
350
|
-
}
|
351
|
-
}
|
352
|
-
|
353
|
-
// MARK: -
|
354
|
-
|
355
|
-
/// Specific type of `Request` that manages an underlying `URLSessionDataTask`.
|
356
|
-
open class DataRequest: Request {
|
357
|
-
|
358
|
-
// MARK: Helper Types
|
359
|
-
|
360
|
-
struct Requestable: TaskConvertible {
|
361
|
-
let urlRequest: URLRequest
|
362
|
-
|
363
|
-
func task(session: URLSession, adapter: RequestAdapter?, queue: DispatchQueue) throws -> URLSessionTask {
|
364
|
-
do {
|
365
|
-
let urlRequest = try self.urlRequest.adapt(using: adapter)
|
366
|
-
return queue.sync { session.dataTask(with: urlRequest) }
|
367
|
-
} catch {
|
368
|
-
throw AdaptError(error: error)
|
369
|
-
}
|
370
|
-
}
|
371
|
-
}
|
372
|
-
|
373
|
-
// MARK: Properties
|
374
|
-
|
375
|
-
/// The request sent or to be sent to the server.
|
376
|
-
open override var request: URLRequest? {
|
377
|
-
if let request = super.request { return request }
|
378
|
-
if let requestable = originalTask as? Requestable { return requestable.urlRequest }
|
379
|
-
|
380
|
-
return nil
|
381
|
-
}
|
382
|
-
|
383
|
-
/// The progress of fetching the response data from the server for the request.
|
384
|
-
open var progress: Progress { return dataDelegate.progress }
|
385
|
-
|
386
|
-
var dataDelegate: DataTaskDelegate { return delegate as! DataTaskDelegate }
|
387
|
-
|
388
|
-
// MARK: Stream
|
389
|
-
|
390
|
-
/// Sets a closure to be called periodically during the lifecycle of the request as data is read from the server.
|
391
|
-
///
|
392
|
-
/// This closure returns the bytes most recently received from the server, not including data from previous calls.
|
393
|
-
/// If this closure is set, data will only be available within this closure, and will not be saved elsewhere. It is
|
394
|
-
/// also important to note that the server data in any `Response` object will be `nil`.
|
395
|
-
///
|
396
|
-
/// - parameter closure: The code to be executed periodically during the lifecycle of the request.
|
397
|
-
///
|
398
|
-
/// - returns: The request.
|
399
|
-
@discardableResult
|
400
|
-
open func stream(closure: ((Data) -> Void)? = nil) -> Self {
|
401
|
-
dataDelegate.dataStream = closure
|
402
|
-
return self
|
403
|
-
}
|
404
|
-
|
405
|
-
// MARK: Progress
|
406
|
-
|
407
|
-
/// Sets a closure to be called periodically during the lifecycle of the `Request` as data is read from the server.
|
408
|
-
///
|
409
|
-
/// - parameter queue: The dispatch queue to execute the closure on.
|
410
|
-
/// - parameter closure: The code to be executed periodically as data is read from the server.
|
411
|
-
///
|
412
|
-
/// - returns: The request.
|
413
|
-
@discardableResult
|
414
|
-
open func downloadProgress(queue: DispatchQueue = DispatchQueue.main, closure: @escaping ProgressHandler) -> Self {
|
415
|
-
dataDelegate.progressHandler = (closure, queue)
|
416
|
-
return self
|
417
|
-
}
|
418
|
-
}
|
419
|
-
|
420
|
-
// MARK: -
|
421
|
-
|
422
|
-
/// Specific type of `Request` that manages an underlying `URLSessionDownloadTask`.
|
423
|
-
open class DownloadRequest: Request {
|
424
|
-
|
425
|
-
// MARK: Helper Types
|
426
|
-
|
427
|
-
/// A collection of options to be executed prior to moving a downloaded file from the temporary URL to the
|
428
|
-
/// destination URL.
|
429
|
-
public struct DownloadOptions: OptionSet {
|
430
|
-
/// Returns the raw bitmask value of the option and satisfies the `RawRepresentable` protocol.
|
431
|
-
public let rawValue: UInt
|
432
|
-
|
433
|
-
/// A `DownloadOptions` flag that creates intermediate directories for the destination URL if specified.
|
434
|
-
public static let createIntermediateDirectories = DownloadOptions(rawValue: 1 << 0)
|
435
|
-
|
436
|
-
/// A `DownloadOptions` flag that removes a previous file from the destination URL if specified.
|
437
|
-
public static let removePreviousFile = DownloadOptions(rawValue: 1 << 1)
|
438
|
-
|
439
|
-
/// Creates a `DownloadFileDestinationOptions` instance with the specified raw value.
|
440
|
-
///
|
441
|
-
/// - parameter rawValue: The raw bitmask value for the option.
|
442
|
-
///
|
443
|
-
/// - returns: A new log level instance.
|
444
|
-
public init(rawValue: UInt) {
|
445
|
-
self.rawValue = rawValue
|
446
|
-
}
|
447
|
-
}
|
448
|
-
|
449
|
-
/// A closure executed once a download request has successfully completed in order to determine where to move the
|
450
|
-
/// temporary file written to during the download process. The closure takes two arguments: the temporary file URL
|
451
|
-
/// and the URL response, and returns a two arguments: the file URL where the temporary file should be moved and
|
452
|
-
/// the options defining how the file should be moved.
|
453
|
-
public typealias DownloadFileDestination = (
|
454
|
-
_ temporaryURL: URL,
|
455
|
-
_ response: HTTPURLResponse)
|
456
|
-
-> (destinationURL: URL, options: DownloadOptions)
|
457
|
-
|
458
|
-
enum Downloadable: TaskConvertible {
|
459
|
-
case request(URLRequest)
|
460
|
-
case resumeData(Data)
|
461
|
-
|
462
|
-
func task(session: URLSession, adapter: RequestAdapter?, queue: DispatchQueue) throws -> URLSessionTask {
|
463
|
-
do {
|
464
|
-
let task: URLSessionTask
|
465
|
-
|
466
|
-
switch self {
|
467
|
-
case let .request(urlRequest):
|
468
|
-
let urlRequest = try urlRequest.adapt(using: adapter)
|
469
|
-
task = queue.sync { session.downloadTask(with: urlRequest) }
|
470
|
-
case let .resumeData(resumeData):
|
471
|
-
task = queue.sync { session.downloadTask(withResumeData: resumeData) }
|
472
|
-
}
|
473
|
-
|
474
|
-
return task
|
475
|
-
} catch {
|
476
|
-
throw AdaptError(error: error)
|
477
|
-
}
|
478
|
-
}
|
479
|
-
}
|
480
|
-
|
481
|
-
// MARK: Properties
|
482
|
-
|
483
|
-
/// The request sent or to be sent to the server.
|
484
|
-
open override var request: URLRequest? {
|
485
|
-
if let request = super.request { return request }
|
486
|
-
|
487
|
-
if let downloadable = originalTask as? Downloadable, case let .request(urlRequest) = downloadable {
|
488
|
-
return urlRequest
|
489
|
-
}
|
490
|
-
|
491
|
-
return nil
|
492
|
-
}
|
493
|
-
|
494
|
-
/// The resume data of the underlying download task if available after a failure.
|
495
|
-
open var resumeData: Data? { return downloadDelegate.resumeData }
|
496
|
-
|
497
|
-
/// The progress of downloading the response data from the server for the request.
|
498
|
-
open var progress: Progress { return downloadDelegate.progress }
|
499
|
-
|
500
|
-
var downloadDelegate: DownloadTaskDelegate { return delegate as! DownloadTaskDelegate }
|
501
|
-
|
502
|
-
// MARK: State
|
503
|
-
|
504
|
-
/// Cancels the request.
|
505
|
-
open override func cancel() {
|
506
|
-
downloadDelegate.downloadTask.cancel { self.downloadDelegate.resumeData = $0 }
|
507
|
-
|
508
|
-
NotificationCenter.default.post(
|
509
|
-
name: Notification.Name.Task.DidCancel,
|
510
|
-
object: self,
|
511
|
-
userInfo: [Notification.Key.Task: task as Any]
|
512
|
-
)
|
513
|
-
}
|
514
|
-
|
515
|
-
// MARK: Progress
|
516
|
-
|
517
|
-
/// Sets a closure to be called periodically during the lifecycle of the `Request` as data is read from the server.
|
518
|
-
///
|
519
|
-
/// - parameter queue: The dispatch queue to execute the closure on.
|
520
|
-
/// - parameter closure: The code to be executed periodically as data is read from the server.
|
521
|
-
///
|
522
|
-
/// - returns: The request.
|
523
|
-
@discardableResult
|
524
|
-
open func downloadProgress(queue: DispatchQueue = DispatchQueue.main, closure: @escaping ProgressHandler) -> Self {
|
525
|
-
downloadDelegate.progressHandler = (closure, queue)
|
526
|
-
return self
|
527
|
-
}
|
528
|
-
|
529
|
-
// MARK: Destination
|
530
|
-
|
531
|
-
/// Creates a download file destination closure which uses the default file manager to move the temporary file to a
|
532
|
-
/// file URL in the first available directory with the specified search path directory and search path domain mask.
|
533
|
-
///
|
534
|
-
/// - parameter directory: The search path directory. `.DocumentDirectory` by default.
|
535
|
-
/// - parameter domain: The search path domain mask. `.UserDomainMask` by default.
|
536
|
-
///
|
537
|
-
/// - returns: A download file destination closure.
|
538
|
-
open class func suggestedDownloadDestination(
|
539
|
-
for directory: FileManager.SearchPathDirectory = .documentDirectory,
|
540
|
-
in domain: FileManager.SearchPathDomainMask = .userDomainMask)
|
541
|
-
-> DownloadFileDestination
|
542
|
-
{
|
543
|
-
return { temporaryURL, response in
|
544
|
-
let directoryURLs = FileManager.default.urls(for: directory, in: domain)
|
545
|
-
|
546
|
-
if !directoryURLs.isEmpty {
|
547
|
-
return (directoryURLs[0].appendingPathComponent(response.suggestedFilename!), [])
|
548
|
-
}
|
549
|
-
|
550
|
-
return (temporaryURL, [])
|
551
|
-
}
|
552
|
-
}
|
553
|
-
}
|
554
|
-
|
555
|
-
// MARK: -
|
556
|
-
|
557
|
-
/// Specific type of `Request` that manages an underlying `URLSessionUploadTask`.
|
558
|
-
open class UploadRequest: DataRequest {
|
559
|
-
|
560
|
-
// MARK: Helper Types
|
561
|
-
|
562
|
-
enum Uploadable: TaskConvertible {
|
563
|
-
case data(Data, URLRequest)
|
564
|
-
case file(URL, URLRequest)
|
565
|
-
case stream(InputStream, URLRequest)
|
566
|
-
|
567
|
-
func task(session: URLSession, adapter: RequestAdapter?, queue: DispatchQueue) throws -> URLSessionTask {
|
568
|
-
do {
|
569
|
-
let task: URLSessionTask
|
570
|
-
|
571
|
-
switch self {
|
572
|
-
case let .data(data, urlRequest):
|
573
|
-
let urlRequest = try urlRequest.adapt(using: adapter)
|
574
|
-
task = queue.sync { session.uploadTask(with: urlRequest, from: data) }
|
575
|
-
case let .file(url, urlRequest):
|
576
|
-
let urlRequest = try urlRequest.adapt(using: adapter)
|
577
|
-
task = queue.sync { session.uploadTask(with: urlRequest, fromFile: url) }
|
578
|
-
case let .stream(_, urlRequest):
|
579
|
-
let urlRequest = try urlRequest.adapt(using: adapter)
|
580
|
-
task = queue.sync { session.uploadTask(withStreamedRequest: urlRequest) }
|
581
|
-
}
|
582
|
-
|
583
|
-
return task
|
584
|
-
} catch {
|
585
|
-
throw AdaptError(error: error)
|
586
|
-
}
|
587
|
-
}
|
588
|
-
}
|
589
|
-
|
590
|
-
// MARK: Properties
|
591
|
-
|
592
|
-
/// The request sent or to be sent to the server.
|
593
|
-
open override var request: URLRequest? {
|
594
|
-
if let request = super.request { return request }
|
595
|
-
|
596
|
-
guard let uploadable = originalTask as? Uploadable else { return nil }
|
597
|
-
|
598
|
-
switch uploadable {
|
599
|
-
case .data(_, let urlRequest), .file(_, let urlRequest), .stream(_, let urlRequest):
|
600
|
-
return urlRequest
|
601
|
-
}
|
602
|
-
}
|
603
|
-
|
604
|
-
/// The progress of uploading the payload to the server for the upload request.
|
605
|
-
open var uploadProgress: Progress { return uploadDelegate.uploadProgress }
|
606
|
-
|
607
|
-
var uploadDelegate: UploadTaskDelegate { return delegate as! UploadTaskDelegate }
|
608
|
-
|
609
|
-
// MARK: Upload Progress
|
610
|
-
|
611
|
-
/// Sets a closure to be called periodically during the lifecycle of the `UploadRequest` as data is sent to
|
612
|
-
/// the server.
|
613
|
-
///
|
614
|
-
/// After the data is sent to the server, the `progress(queue:closure:)` APIs can be used to monitor the progress
|
615
|
-
/// of data being read from the server.
|
616
|
-
///
|
617
|
-
/// - parameter queue: The dispatch queue to execute the closure on.
|
618
|
-
/// - parameter closure: The code to be executed periodically as data is sent to the server.
|
619
|
-
///
|
620
|
-
/// - returns: The request.
|
621
|
-
@discardableResult
|
622
|
-
open func uploadProgress(queue: DispatchQueue = DispatchQueue.main, closure: @escaping ProgressHandler) -> Self {
|
623
|
-
uploadDelegate.uploadProgressHandler = (closure, queue)
|
624
|
-
return self
|
625
|
-
}
|
626
|
-
}
|
627
|
-
|
628
|
-
// MARK: -
|
629
|
-
|
630
|
-
#if !os(watchOS)
|
631
|
-
|
632
|
-
/// Specific type of `Request` that manages an underlying `URLSessionStreamTask`.
|
633
|
-
@available(iOS 9.0, macOS 10.11, tvOS 9.0, *)
|
634
|
-
open class StreamRequest: Request {
|
635
|
-
enum Streamable: TaskConvertible {
|
636
|
-
case stream(hostName: String, port: Int)
|
637
|
-
case netService(NetService)
|
638
|
-
|
639
|
-
func task(session: URLSession, adapter: RequestAdapter?, queue: DispatchQueue) throws -> URLSessionTask {
|
640
|
-
let task: URLSessionTask
|
641
|
-
|
642
|
-
switch self {
|
643
|
-
case let .stream(hostName, port):
|
644
|
-
task = queue.sync { session.streamTask(withHostName: hostName, port: port) }
|
645
|
-
case let .netService(netService):
|
646
|
-
task = queue.sync { session.streamTask(with: netService) }
|
647
|
-
}
|
648
|
-
|
649
|
-
return task
|
650
|
-
}
|
651
|
-
}
|
652
|
-
}
|
653
|
-
|
654
|
-
#endif
|