Skip to content
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

Replace all internal references to AFResult<T> with Result<T, Error> #2891

Merged
merged 2 commits into from Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 18 additions & 8 deletions Alamofire.xcodeproj/project.pbxproj
Expand Up @@ -142,6 +142,10 @@
31FB2F8822C828D8007FD6D5 /* URLEncodedFormEncoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31FB2F8622C828D8007FD6D5 /* URLEncodedFormEncoder.swift */; };
31FB2F8922C828D8007FD6D5 /* URLEncodedFormEncoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31FB2F8622C828D8007FD6D5 /* URLEncodedFormEncoder.swift */; };
31FB2F8A22C828D8007FD6D5 /* URLEncodedFormEncoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31FB2F8622C828D8007FD6D5 /* URLEncodedFormEncoder.swift */; };
4196936222FA1E05001EA5D5 /* Result+Alamofire.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4196936122FA1E05001EA5D5 /* Result+Alamofire.swift */; };
4196936322FA1E05001EA5D5 /* Result+Alamofire.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4196936122FA1E05001EA5D5 /* Result+Alamofire.swift */; };
4196936422FA1E05001EA5D5 /* Result+Alamofire.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4196936122FA1E05001EA5D5 /* Result+Alamofire.swift */; };
4196936522FA1EAD001EA5D5 /* Result+Alamofire.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4196936122FA1E05001EA5D5 /* Result+Alamofire.swift */; };
4C0CB631220BC70300604EDC /* RequestInterceptorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0CB630220BC70300604EDC /* RequestInterceptorTests.swift */; };
4C0CB632220BC70300604EDC /* RequestInterceptorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0CB630220BC70300604EDC /* RequestInterceptorTests.swift */; };
4C0CB633220BC70300604EDC /* RequestInterceptorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0CB630220BC70300604EDC /* RequestInterceptorTests.swift */; };
Expand Down Expand Up @@ -244,9 +248,9 @@
4C743D321C22772F00BCB23E /* signed-by-ca2.cer in Resources */ = {isa = PBXBuildFile; fileRef = 4C812C511B535F540017E0BF /* signed-by-ca2.cer */; };
4C743D331C22772F00BCB23E /* valid-dns-name.cer in Resources */ = {isa = PBXBuildFile; fileRef = 4C812C521B535F540017E0BF /* valid-dns-name.cer */; };
4C743D341C22772F00BCB23E /* valid-uri.cer in Resources */ = {isa = PBXBuildFile; fileRef = 4C812C531B535F540017E0BF /* valid-uri.cer */; };
4C7DD7EB224C627300249836 /* AFResult+Alamofire.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7DD7EA224C627300249836 /* AFResult+Alamofire.swift */; };
4C7DD7EC224C627300249836 /* AFResult+Alamofire.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7DD7EA224C627300249836 /* AFResult+Alamofire.swift */; };
4C7DD7ED224C627300249836 /* AFResult+Alamofire.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7DD7EA224C627300249836 /* AFResult+Alamofire.swift */; };
4C7DD7EB224C627300249836 /* Result+Alamofire.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7DD7EA224C627300249836 /* Result+Alamofire.swift */; };
4C7DD7EC224C627300249836 /* Result+Alamofire.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7DD7EA224C627300249836 /* Result+Alamofire.swift */; };
4C7DD7ED224C627300249836 /* Result+Alamofire.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7DD7EA224C627300249836 /* Result+Alamofire.swift */; };
4C811F8D1B51856D00E0F59A /* ServerTrustEvaluation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C811F8C1B51856D00E0F59A /* ServerTrustEvaluation.swift */; };
4C811F8E1B51856D00E0F59A /* ServerTrustEvaluation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C811F8C1B51856D00E0F59A /* ServerTrustEvaluation.swift */; };
4CB928291C66BFBC00CE5F08 /* Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CB928281C66BFBC00CE5F08 /* Notifications.swift */; };
Expand Down Expand Up @@ -383,6 +387,7 @@
31F5085C20B50DC400FE2A0C /* URLSessionConfiguration+Alamofire.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URLSessionConfiguration+Alamofire.swift"; sourceTree = "<group>"; };
31F9683B20BB70290009606F /* NSLoggingEventMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSLoggingEventMonitor.swift; sourceTree = "<group>"; };
31FB2F8622C828D8007FD6D5 /* URLEncodedFormEncoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLEncodedFormEncoder.swift; sourceTree = "<group>"; };
4196936122FA1E05001EA5D5 /* Result+Alamofire.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Result+Alamofire.swift"; sourceTree = "<group>"; };
4C0B58381B747A4400C0B99C /* ResponseSerializationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResponseSerializationTests.swift; sourceTree = "<group>"; };
4C0CB630220BC70300604EDC /* RequestInterceptorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestInterceptorTests.swift; sourceTree = "<group>"; };
4C0CB640220CA89400604EDC /* URLRequest+Alamofire.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "URLRequest+Alamofire.swift"; sourceTree = "<group>"; };
Expand All @@ -403,7 +408,7 @@
4C3D00571C66A8B900D1F709 /* NetworkReachabilityManagerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkReachabilityManagerTests.swift; sourceTree = "<group>"; };
4C43669A1D7BB93D00C38AAD /* DispatchQueue+Alamofire.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "DispatchQueue+Alamofire.swift"; sourceTree = "<group>"; };
4C4466EA21F8F5D800AC9703 /* CachedResponseHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CachedResponseHandler.swift; sourceTree = "<group>"; };
4C7DD7EA224C627300249836 /* AFResult+Alamofire.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AFResult+Alamofire.swift"; sourceTree = "<group>"; };
4C7DD7EA224C627300249836 /* Result+Alamofire.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Result+Alamofire.swift"; sourceTree = "<group>"; };
4C811F8C1B51856D00E0F59A /* ServerTrustEvaluation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServerTrustEvaluation.swift; sourceTree = "<group>"; };
4C812C3A1B535F220017E0BF /* alamofire-root-ca.cer */ = {isa = PBXFileReference; lastKnownFileType = file; name = "alamofire-root-ca.cer"; path = "alamofire.org/alamofire-root-ca.cer"; sourceTree = "<group>"; };
4C812C3D1B535F2E0017E0BF /* alamofire-signing-ca1.cer */ = {isa = PBXFileReference; lastKnownFileType = file; name = "alamofire-signing-ca1.cer"; path = "alamofire.org/alamofire-signing-ca1.cer"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -652,6 +657,7 @@
319917B8209CE53A00103A19 /* OperationQueue+Alamofire.swift */,
4C0CB640220CA89400604EDC /* URLRequest+Alamofire.swift */,
31F5085C20B50DC400FE2A0C /* URLSessionConfiguration+Alamofire.swift */,
4196936122FA1E05001EA5D5 /* Result+Alamofire.swift */,
);
name = Extensions;
sourceTree = "<group>";
Expand All @@ -668,7 +674,7 @@
4C7DD7E9224C625500249836 /* Helpers */ = {
isa = PBXGroup;
children = (
4C7DD7EA224C627300249836 /* AFResult+Alamofire.swift */,
4C7DD7EA224C627300249836 /* Result+Alamofire.swift */,
);
name = Helpers;
sourceTree = "<group>";
Expand Down Expand Up @@ -1301,6 +1307,7 @@
319917A7209CDAC400103A19 /* RequestTaskMap.swift in Sources */,
4CF627131BA7CBF60011A099 /* Validation.swift in Sources */,
31F5085F20B50DC400FE2A0C /* URLSessionConfiguration+Alamofire.swift in Sources */,
4196936422FA1E05001EA5D5 /* Result+Alamofire.swift in Sources */,
3172741F218BB1790039FFCC /* ParameterEncoder.swift in Sources */,
319917BB209CE53A00103A19 /* OperationQueue+Alamofire.swift in Sources */,
319917AC209CDCB000103A19 /* HTTPHeaders.swift in Sources */,
Expand Down Expand Up @@ -1344,7 +1351,7 @@
3107EA3A20A11F9700445260 /* ResponseTests.swift in Sources */,
4CFB02921D7CF28F0056F249 /* FileManager+AlamofireTests.swift in Sources */,
4CF627141BA7CC240011A099 /* BaseTestCase.swift in Sources */,
4C7DD7ED224C627300249836 /* AFResult+Alamofire.swift in Sources */,
4C7DD7ED224C627300249836 /* Result+Alamofire.swift in Sources */,
31727424218BB9A50039FFCC /* HTTPBin.swift in Sources */,
31EBD9C320D1D89D00D1FF34 /* ValidationTests.swift in Sources */,
3111CE8620A76370008315E2 /* SessionTests.swift in Sources */,
Expand Down Expand Up @@ -1374,6 +1381,7 @@
319917A6209CDAC400103A19 /* RequestTaskMap.swift in Sources */,
4C1DC8551B68908E00476DE3 /* AFError.swift in Sources */,
31F5085E20B50DC400FE2A0C /* URLSessionConfiguration+Alamofire.swift in Sources */,
4196936322FA1E05001EA5D5 /* Result+Alamofire.swift in Sources */,
3172741E218BB1790039FFCC /* ParameterEncoder.swift in Sources */,
319917BA209CE53A00103A19 /* OperationQueue+Alamofire.swift in Sources */,
319917AB209CDCB000103A19 /* HTTPHeaders.swift in Sources */,
Expand Down Expand Up @@ -1413,6 +1421,7 @@
319917A8209CDAC400103A19 /* RequestTaskMap.swift in Sources */,
4CEC605A1B745C9100E684F4 /* AFError.swift in Sources */,
31F5086020B50DC400FE2A0C /* URLSessionConfiguration+Alamofire.swift in Sources */,
4196936522FA1EAD001EA5D5 /* Result+Alamofire.swift in Sources */,
31727420218BB1790039FFCC /* ParameterEncoder.swift in Sources */,
319917BC209CE53A00103A19 /* OperationQueue+Alamofire.swift in Sources */,
319917AD209CDCB000103A19 /* HTTPHeaders.swift in Sources */,
Expand Down Expand Up @@ -1452,6 +1461,7 @@
319917A5209CDAC400103A19 /* RequestTaskMap.swift in Sources */,
4C1DC8541B68908E00476DE3 /* AFError.swift in Sources */,
31F5085D20B50DC400FE2A0C /* URLSessionConfiguration+Alamofire.swift in Sources */,
4196936222FA1E05001EA5D5 /* Result+Alamofire.swift in Sources */,
3172741D218BB1790039FFCC /* ParameterEncoder.swift in Sources */,
319917B9209CE53A00103A19 /* OperationQueue+Alamofire.swift in Sources */,
319917AA209CDCB000103A19 /* HTTPHeaders.swift in Sources */,
Expand Down Expand Up @@ -1495,7 +1505,7 @@
3107EA3820A11F9600445260 /* ResponseTests.swift in Sources */,
F8858DDD19A96B4300F55F93 /* RequestTests.swift in Sources */,
4C256A531B096C770065714F /* BaseTestCase.swift in Sources */,
4C7DD7EB224C627300249836 /* AFResult+Alamofire.swift in Sources */,
4C7DD7EB224C627300249836 /* Result+Alamofire.swift in Sources */,
31727422218BB9A50039FFCC /* HTTPBin.swift in Sources */,
31EBD9C120D1D89C00D1FF34 /* ValidationTests.swift in Sources */,
3111CE8420A7636E008315E2 /* SessionTests.swift in Sources */,
Expand Down Expand Up @@ -1529,7 +1539,7 @@
3107EA3920A11F9600445260 /* ResponseTests.swift in Sources */,
F829C6BE1A7A950600A2CD59 /* ParameterEncodingTests.swift in Sources */,
F829C6BF1A7A950600A2CD59 /* RequestTests.swift in Sources */,
4C7DD7EC224C627300249836 /* AFResult+Alamofire.swift in Sources */,
4C7DD7EC224C627300249836 /* Result+Alamofire.swift in Sources */,
31727423218BB9A50039FFCC /* HTTPBin.swift in Sources */,
31EBD9C220D1D89C00D1FF34 /* ValidationTests.swift in Sources */,
3111CE8520A7636F008315E2 /* SessionTests.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Example/Source/DetailViewController.swift
Expand Up @@ -76,7 +76,7 @@ class DetailViewController: UITableViewController {

let start = CACurrentMediaTime()

let requestComplete: (HTTPURLResponse?, AFResult<String>) -> Void = { response, result in
let requestComplete: (HTTPURLResponse?, Result<String, Error>) -> Void = { response, result in
let end = CACurrentMediaTime()
self.elapsedTime = end - start

Expand Down
80 changes: 0 additions & 80 deletions Source/AFResult.swift
Expand Up @@ -25,83 +25,3 @@
/// `Result` that always has an `Error` `Failure` type.
public typealias AFResult<T> = Result<T, Error>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's really no reason to have just this typealias in its own file. It can be combined with the Result extensions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved the typealias to Result+Alamofire.swift


// MARK: - Internal APIs

extension AFResult {
/// Returns the associated value if the result is a success, `nil` otherwise.
var success: Success? {
guard case .success(let value) = self else { return nil }
return value
}

/// Returns the associated error value if the result is a failure, `nil` otherwise.
var failure: Failure? {
guard case .failure(let error) = self else { return nil }
return error
}

/// Initializes an `AFResult` from value or error. Returns `.failure` if the error is non-nil, `.success` otherwise.
///
/// - Parameters:
/// - value: A value.
/// - error: An `Error`.
init(value: Success, error: Failure?) {
if let error = error {
self = .failure(error)
} else {
self = .success(value)
}
}

/// Evaluates the specified closure when the `AFResult` is a success, passing the unwrapped value as a parameter.
///
/// Use the `flatMap` method with a closure that may throw an error. For example:
///
/// let possibleData: AFResult<Data> = .success(Data(...))
/// let possibleObject = possibleData.flatMap {
/// try JSONSerialization.jsonObject(with: $0)
/// }
///
/// - parameter transform: A closure that takes the success value of the instance.
///
/// - returns: An `AFResult` containing the result of the given closure. If this instance is a failure, returns the
/// same failure.
func flatMap<T>(_ transform: (Success) throws -> T) -> AFResult<T> {
switch self {
case .success(let value):
do {
return try .success(transform(value))
} catch {
return .failure(error)
}
case .failure(let error):
return .failure(error)
}
}

/// Evaluates the specified closure when the `AFResult` is a failure, passing the unwrapped error as a parameter.
///
/// Use the `flatMapError` function with a closure that may throw an error. For example:
///
/// let possibleData: AFResult<Data> = .success(Data(...))
/// let possibleObject = possibleData.flatMapError {
/// try someFailableFunction(taking: $0)
/// }
///
/// - Parameter transform: A throwing closure that takes the error of the instance.
///
/// - Returns: An `AFResult` instance containing the result of the transform. If this instance is a success, returns
/// the same success.
func flatMapError<T: Error>(_ transform: (Failure) throws -> T) -> AFResult<Success> {
switch self {
case .failure(let error):
do {
return try .failure(transform(error))
} catch {
return .failure(error)
}
case .success(let value):
return .success(value)
}
}
}
10 changes: 5 additions & 5 deletions Source/EventMonitor.swift
Expand Up @@ -179,7 +179,7 @@ public protocol EventMonitor {
// MARK: DownloadRequest Events

/// Event called when a `DownloadRequest`'s `URLSessionDownloadTask` finishes and the temporary file has been moved.
func request(_ request: DownloadRequest, didFinishDownloadingUsing task: URLSessionTask, with result: AFResult<URL>)
func request(_ request: DownloadRequest, didFinishDownloadingUsing task: URLSessionTask, with result: Result<URL, Error>)

/// Event called when a `DownloadRequest`'s `Destination` closure is called and creates the destination URL the
/// downloaded file will be moved to.
Expand Down Expand Up @@ -271,7 +271,7 @@ extension EventMonitor {
public func request(_ request: UploadRequest, didCreateUploadable uploadable: UploadRequest.Uploadable) { }
public func request(_ request: UploadRequest, didFailToCreateUploadableWithError error: Error) { }
public func request(_ request: UploadRequest, didProvideInputStream stream: InputStream) { }
public func request(_ request: DownloadRequest, didFinishDownloadingUsing task: URLSessionTask, with result: AFResult<URL>) { }
public func request(_ request: DownloadRequest, didFinishDownloadingUsing task: URLSessionTask, with result: Result<URL, Error>) { }
public func request(_ request: DownloadRequest, didCreateDestinationURL url: URL) { }
public func request(_ request: DownloadRequest,
didValidateRequest urlRequest: URLRequest?,
Expand Down Expand Up @@ -498,7 +498,7 @@ public final class CompositeEventMonitor: EventMonitor {
performEvent { $0.request(request, didProvideInputStream: stream) }
}

public func request(_ request: DownloadRequest, didFinishDownloadingUsing task: URLSessionTask, with result: AFResult<URL>) {
public func request(_ request: DownloadRequest, didFinishDownloadingUsing task: URLSessionTask, with result: Result<URL, Error>) {
performEvent { $0.request(request, didFinishDownloadingUsing: task, with: result) }
}

Expand Down Expand Up @@ -638,7 +638,7 @@ open class ClosureEventMonitor: EventMonitor {
open var requestDidProvideInputStream: ((UploadRequest, InputStream) -> Void)?

/// Closure called on the `request(_:didFinishDownloadingUsing:with:)` event.
open var requestDidFinishDownloadingUsingTaskWithResult: ((DownloadRequest, URLSessionTask, AFResult<URL>) -> Void)?
open var requestDidFinishDownloadingUsingTaskWithResult: ((DownloadRequest, URLSessionTask, Result<URL, Error>) -> Void)?

/// Closure called on the `request(_:didCreateDestinationURL:)` event.
open var requestDidCreateDestinationURL: ((DownloadRequest, URL) -> Void)?
Expand Down Expand Up @@ -818,7 +818,7 @@ open class ClosureEventMonitor: EventMonitor {
requestDidProvideInputStream?(request, stream)
}

open func request(_ request: DownloadRequest, didFinishDownloadingUsing task: URLSessionTask, with result: AFResult<URL>) {
open func request(_ request: DownloadRequest, didFinishDownloadingUsing task: URLSessionTask, with result: Result<URL, Error>) {
requestDidFinishDownloadingUsingTaskWithResult?(request, task, result)
}

Expand Down
2 changes: 1 addition & 1 deletion Source/MultipartUpload.swift
Expand Up @@ -26,7 +26,7 @@ import Foundation

/// Internal type which encapsulates a `MultipartFormData` upload.
final class MultipartUpload {
lazy var result = AFResult { try build() }
lazy var result = Result { try build() }

let isInBackgroundSession: Bool
let multipartFormData: MultipartFormData
Expand Down
4 changes: 2 additions & 2 deletions Source/ParameterEncoder.swift
Expand Up @@ -160,7 +160,7 @@ open class URLEncodedFormParameterEncoder: ParameterEncoder {

if destination.encodesParametersInURL(for: method),
var components = URLComponents(url: url, resolvingAgainstBaseURL: false) {
let query: String = try AFResult<String> { try encoder.encode(parameters) }
let query: String = try Result<String, Error> { try encoder.encode(parameters) }
.mapError { AFError.parameterEncoderFailed(reason: .encoderFailed(error: $0)) }.get()
let newQueryString = [components.percentEncodedQuery, query].compactMap { $0 }.joinedWithAmpersands()
components.percentEncodedQuery = newQueryString.isEmpty ? nil : newQueryString
Expand All @@ -175,7 +175,7 @@ open class URLEncodedFormParameterEncoder: ParameterEncoder {
request.headers.update(.contentType("application/x-www-form-urlencoded; charset=utf-8"))
}

request.httpBody = try AFResult<Data> { try encoder.encode(parameters) }
request.httpBody = try Result<Data, Error> { try encoder.encode(parameters) }
.mapError { AFError.parameterEncoderFailed(reason: .encoderFailed(error: $0)) }.get()
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Request.swift
Expand Up @@ -1159,8 +1159,8 @@ public class DownloadRequest: Request {
///
/// - Parameters:
/// - task: `URLSessionTask` that finished the download.
/// - result: `AFResult` of the automatic move to `destination`.
func didFinishDownloading(using task: URLSessionTask, with result: AFResult<URL>) {
/// - result: `Result` of the automatic move to `destination`.
func didFinishDownloading(using task: URLSessionTask, with result: Result<URL, Error>) {
eventMonitor?.request(self, didFinishDownloadingUsing: task, with: result)

switch result {
Expand Down